|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as utxolib from "@bitgo/utxo-lib"; |
| 3 | +import { Descriptor } from "../js"; |
| 4 | +import { finalizePsbt, updateInputWithDescriptor } from "./psbt.util"; |
| 5 | +import { getFixture } from "./fixtures"; |
| 6 | + |
| 7 | +const rootWalletKeys = new utxolib.bitgo.RootWalletKeys(utxolib.testutil.getKeyTriple("wasm")); |
| 8 | + |
| 9 | +function getDescriptorOpDropP2ms(locktime: number, keys: utxolib.BIP32Interface[]) { |
| 10 | + const xpubs = keys.map((key) => key.toBase58() + "/*"); |
| 11 | + // the `r:` prefix is a custom BitGo modification of miniscript to allow OP_DROP |
| 12 | + return `wsh(and_v(r:after(${locktime}),multi(2,${xpubs.join(",")})))`; |
| 13 | +} |
| 14 | + |
| 15 | +describe("CLV with OP_DROP", () => { |
| 16 | + const locktime = 1024; |
| 17 | + const descriptor = Descriptor.fromString( |
| 18 | + getDescriptorOpDropP2ms(locktime, rootWalletKeys.triple), |
| 19 | + "derivable", |
| 20 | + ); |
| 21 | + it("has expected AST", () => { |
| 22 | + assert.deepStrictEqual(descriptor.node(), { |
| 23 | + Wsh: { |
| 24 | + Ms: { |
| 25 | + AndV: [ |
| 26 | + { |
| 27 | + Drop: { |
| 28 | + After: { |
| 29 | + absLockTime: 1024, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + Multi: [ |
| 35 | + 2, |
| 36 | + { |
| 37 | + XPub: "xpub661MyMwAqRbcFNusVUbSN3nbanHMtJjLgZGrs1wxH6f77kKQd6Vq4HfkZQNPC1vSbN6RTiBWJJV6FwJtCfBon2SgaT2J3MSkydukstKjwbJ/*", |
| 38 | + }, |
| 39 | + { |
| 40 | + XPub: "xpub661MyMwAqRbcFo3t7PUqvbgvAcEuuoeVib5aapsg52inrG6KGF5aNtR5ey1FNCt1zJpMQiNec5XpofQmLNRhHvQRbhkc8UsWwwMwsXW6ogU/*", |
| 41 | + }, |
| 42 | + { |
| 43 | + XPub: "xpub661MyMwAqRbcGg7f22Kcg2gy1F4jBjWR3xQTECVeJPHmxvhg5gUAZC6EYFtnyi6aMDQir1kV8HzCqC2FzTowGgEZqRh7rinqUCDeNDdmYzH/*", |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + }, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("has expected asm", () => { |
| 54 | + assert.deepStrictEqual(descriptor.atDerivationIndex(0).toAsmString().split(" "), [ |
| 55 | + "OP_PUSHBYTES_2", |
| 56 | + "0004", |
| 57 | + "OP_CLTV", |
| 58 | + "OP_DROP", |
| 59 | + "OP_PUSHNUM_2", |
| 60 | + "OP_PUSHBYTES_33", |
| 61 | + "02ae7c3c0ebc315a33151a1985ebb1fdcae72b3b91c38e3193c40ebabfffe9c343", |
| 62 | + "OP_PUSHBYTES_33", |
| 63 | + "0260ba2407f7c75d525db9f171e9b2f3cf5ba3f0d7fc6067b20d4b91585432f974", |
| 64 | + "OP_PUSHBYTES_33", |
| 65 | + "03eadd6e4300dac62f1d4cf1131a06c5e140911f04245c64934c27510e93dbe843", |
| 66 | + "OP_PUSHNUM_3", |
| 67 | + "OP_CHECKMULTISIG", |
| 68 | + ]); |
| 69 | + }); |
| 70 | + |
| 71 | + it("can be signed", async function () { |
| 72 | + const psbt = Object.assign(new utxolib.Psbt({ network: utxolib.networks.bitcoin }), { |
| 73 | + locktime, |
| 74 | + }); |
| 75 | + const signers = rootWalletKeys.triple.slice(0, 2); |
| 76 | + const descriptorAt0 = descriptor.atDerivationIndex(0); |
| 77 | + const script = Buffer.from(descriptorAt0.scriptPubkey()); |
| 78 | + psbt.addInput({ |
| 79 | + hash: Buffer.alloc(32), |
| 80 | + index: 0, |
| 81 | + sequence: 0xfffffffe, |
| 82 | + witnessUtxo: { script, value: BigInt(1e8) }, |
| 83 | + }); |
| 84 | + psbt.addOutput({ script, value: BigInt(1e8) }); |
| 85 | + updateInputWithDescriptor(psbt, 0, descriptorAt0); |
| 86 | + for (const signer of signers) { |
| 87 | + psbt.signAllInputsHD(signer); |
| 88 | + } |
| 89 | + finalizePsbt(psbt); |
| 90 | + const signedTx = psbt.extractTransaction().toBuffer(); |
| 91 | + assert.strictEqual( |
| 92 | + signedTx.toString("hex"), |
| 93 | + await getFixture("test/fixtures/opdrop.json", signedTx.toString("hex")), |
| 94 | + ); |
| 95 | + }); |
| 96 | +}); |
0 commit comments