-
|
Based on btcd/txscript/sign_test.go TestRawTxInTapscriptSignature() case. I modify its script to 2/3 multisign as below. func TestRawTxInTapscriptSignatureScriptPath_2_3(t *testing.T) {
privKey1, err := btcec.NewPrivateKey()
require.NoError(t, err)
privKey2, err := btcec.NewPrivateKey()
require.NoError(t, err)
privKey3, err := btcec.NewPrivateKey()
require.NoError(t, err)
pubKey1 := privKey1.PubKey()
pubKey2 := privKey2.PubKey()
pubKey3 := privKey3.PubKey()
internalKey := pubKey1
//
builder := NewScriptBuilder()
builder.AddData(schnorr.SerializePubKey(pubKey1))
builder.AddOp(OP_CHECKSIG)
builder.AddData(schnorr.SerializePubKey(pubKey2))
builder.AddOp(OP_CHECKSIGADD)
builder.AddData(schnorr.SerializePubKey(pubKey3))
builder.AddOp(OP_CHECKSIGADD)
builder.AddInt64(2)
builder.AddOp(OP_NUMEQUAL)
pkScript, err := builder.Script()
require.NoError(t, err)
tapLeaf := NewBaseTapLeaf(pkScript)
tapScriptTree := AssembleTaprootScriptTree(tapLeaf)
ctrlBlock := tapScriptTree.LeafMerkleProofs[0].ToControlBlock(
internalKey,
)
tapScriptRootHash := tapScriptTree.RootNode.TapHash()
outputKey := ComputeTaprootOutputKey(
internalKey, tapScriptRootHash[:],
)
p2trScript, err := PayToTaprootScript(outputKey)
require.NoError(t, err)
// We'll reuse this simple transaction for the tests below. It ends up
// spending from a bip86 P2TR output.
testTx := wire.NewMsgTx(2)
testTx.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Index: 1,
},
})
txOut := &wire.TxOut{
Value: 1e8, PkScript: p2trScript,
}
testTx.AddTxOut(txOut)
prevFetcher := NewCannedPrevOutputFetcher(
txOut.PkScript, txOut.Value,
)
sigHashes := NewTxSigHashes(testTx, prevFetcher)
sig1, err := RawTxInTapscriptSignature(
testTx, sigHashes, 0, txOut.Value,
txOut.PkScript, tapLeaf, SigHashDefault,
privKey1,
)
require.NoError(t, err)
sig2, err := RawTxInTapscriptSignature(
testTx, sigHashes, 0, txOut.Value,
txOut.PkScript, tapLeaf, SigHashDefault,
privKey2,
)
require.NoError(t, err)
sig3, err := RawTxInTapscriptSignature(
testTx, sigHashes, 0, txOut.Value,
txOut.PkScript, tapLeaf, SigHashDefault,
privKey3,
)
require.NoError(t, err)
// If this isn't sighash default, then a sighash should
// be applied. Otherwise, it should be a normal sig.
expectedLen := schnorr.SignatureSize
require.Len(t, sig1, expectedLen)
require.Len(t, sig2, expectedLen)
require.Len(t, sig3, expectedLen)
// Now that we have the sig, we'll make a valid witness
// including the control block.
ctrlBlockBytes, err := ctrlBlock.ToBytes()
fmt.Printf("control block: %x\n", ctrlBlockBytes)
require.NoError(t, err)
txCopy := testTx.Copy()
txCopy.TxIn[0].Witness = wire.TxWitness{
sig1, sig2, sig3, pkScript, ctrlBlockBytes,
}
// Finally, ensure that the signature produced is valid.
vm, err := NewEngine(
txOut.PkScript, txCopy, 0, StandardVerifyFlags,
nil, sigHashes, txOut.Value, prevFetcher,
)
require.NoError(t, err)
require.NoError(t, vm.Execute())
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
A |
Beta Was this translation helpful? Give feedback.
The stack is worked on from the back toward the front.
For me this works: