Skip to content

Commit

Permalink
Clarify use of redeemScript and witnessScript
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewLM committed Jan 9, 2024
1 parent b4fb192 commit 27ba3f9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions coinlib/lib/src/address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class P2SHAddress extends Base58Address {
: super._(copyCheckBytes(hash, 20), version);

/// Constructs a P2SH address for a redeemScript
P2SHAddress.fromScript(Script script, { required int version })
P2SHAddress.fromRedeemScript(Script script, { required int version })
: super._(hash160(script.compiled), version);

@override
Expand Down Expand Up @@ -263,7 +263,7 @@ class P2WSHAddress extends Bech32Address {
: super._(0, copyCheckBytes(hash, 32), hrp);

/// Constructs a P2WSH address for a witnessScript
P2WSHAddress.fromScript(Script script, { required String hrp })
P2WSHAddress.fromWitnessScript(Script script, { required String hrp })
: super._(0, sha256Hash(script.compiled), hrp);

@override
Expand Down
12 changes: 6 additions & 6 deletions coinlib/lib/src/scripts/programs/p2wsh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import 'package:coinlib/src/scripts/program.dart';
import 'package:coinlib/src/scripts/programs/p2witness.dart';
import 'package:coinlib/src/scripts/script.dart';

/// Pay-to-Witness-Script-Hash program taking a 32-byte script hash for a redeem
/// script whereby the redeem script and other push data is to be provided as
/// witness data.
/// Pay-to-Witness-Script-Hash program taking a 32-byte script hash for a
/// witness script whereby the witness script and other push data is to be
/// provided as witness data.
class P2WSH extends P2Witness {

/// Construct using an output script, not to be confused with the redeem
/// script. For that use [fromRedeemScript].
/// Construct using an output script, not to be confused with the witness
/// script. For that use [fromWitnessScript].
P2WSH.fromScript(super.script) : super.fromScript() {
if (data.length != 32 || version != 0) throw NoProgramMatch();
}
Expand All @@ -24,7 +24,7 @@ class P2WSH extends P2Witness {
P2WSH.fromHash(Uint8List scriptHash)
: super.fromData(0, checkBytes(scriptHash, 32, name: "Script hash"));

P2WSH.fromRedeemScript(Script redeemScript)
P2WSH.fromWitnessScript(Script redeemScript)
: this.fromHash(sha256Hash(redeemScript.compiled));

Uint8List get scriptHash => data;
Expand Down
4 changes: 2 additions & 2 deletions coinlib/test/address_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void main() {
expectValidAddress(
"pL5vkwAVx6Qo1AVm7dzW5XKxP4meAjZQS1",
NetworkParams.mainnet,
P2SHAddress.fromScript(
P2SHAddress.fromRedeemScript(
Script.fromAsm("0"),
version: NetworkParams.mainnet.p2shPrefix,
),
Expand Down Expand Up @@ -173,7 +173,7 @@ void main() {
expectValidAddress(
"pc1qdc6qh88lkdaf3899gnntk7q293ufq8flkvmnsa59zx3sv9a05qwsgzh235",
NetworkParams.mainnet,
P2WSHAddress.fromScript(
P2WSHAddress.fromWitnessScript(
Script.fromAsm("0"),
hrp: NetworkParams.mainnet.bech32Hrp,
),
Expand Down
6 changes: 3 additions & 3 deletions coinlib/test/scripts/programs/p2wsh_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void main() {

setUpAll(loadCoinlib);

final redeemScript = Script.fromAsm("0");
final witnessScript = Script.fromAsm("0");
final scriptHash = "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d";
final asm = "0 $scriptHash";
final script = Script.fromAsm(asm);
Expand All @@ -31,8 +31,8 @@ void main() {
() => expectP2WSH(P2WSH.fromHash(hexToBytes(scriptHash))),
);

test("fromRedeemScript() success", () {
expectP2WSH(P2WSH.fromRedeemScript(redeemScript));
test("fromWitnessScript() success", () {
expectP2WSH(P2WSH.fromWitnessScript(witnessScript));
});

test("Program.match()", () => expectP2WSH(Program.fromAsm(asm) as P2WSH));
Expand Down

0 comments on commit 27ba3f9

Please sign in to comment.