From 4a285988e165525bbd7b4f78e617ba7f7d7cfeb7 Mon Sep 17 00:00:00 2001 From: Paladz Date: Tue, 27 Feb 2018 13:00:02 +0800 Subject: [PATCH] p2sh use RawWitnessKeys (#386) * p2sh use RawWitnessKeys * change test key name for fix delay issue * edit test dir name --- blockchain/account/builder.go | 2 +- protocol/vm/vmutil/script.go | 11 ++++------- protocol/vm/vmutil/script_test.go | 22 ---------------------- test/integration/standard_transaction_test.go | 16 ++++++++-------- 4 files changed, 13 insertions(+), 38 deletions(-) diff --git a/blockchain/account/builder.go b/blockchain/account/builder.go index bd7b4523..8585a900 100755 --- a/blockchain/account/builder.go +++ b/blockchain/account/builder.go @@ -160,7 +160,7 @@ func UtxoToInputs(signer *signers.Signer, u *UTXO, refData []byte) (*legacy.TxIn sigInst.WitnessComponents = append(sigInst.WitnessComponents, txbuilder.DataWitness([]byte(derivedPK))) case *common.AddressWitnessScriptHash: - sigInst.AddWitnessKeys(signer.XPubs, path, signer.Quorum) + sigInst.AddRawWitnessKeys(signer.XPubs, path, signer.Quorum) path := signers.Path(signer, signers.AccountKeySpace, u.ControlProgramIndex) derivedXPubs := chainkd.DeriveXPubs(signer.XPubs, path) derivedPKs := chainkd.XPubKeys(derivedXPubs) diff --git a/protocol/vm/vmutil/script.go b/protocol/vm/vmutil/script.go index a3fcd1a1..57bea29c 100644 --- a/protocol/vm/vmutil/script.go +++ b/protocol/vm/vmutil/script.go @@ -22,16 +22,13 @@ func (b *Builder) addP2SPMultiSig(pubkeys []ed25519.PublicKey, nrequired int) er return err } - b.AddOp(vm.OP_DUP).AddOp(vm.OP_TOALTSTACK) // stash a copy of the predicate - b.AddOp(vm.OP_SHA3) // stack is now [... NARGS SIG SIG SIG PREDICATEHASH] + b.AddOp(vm.OP_TXSIGHASH) // stack is now [... NARGS SIG SIG SIG PREDICATEHASH] for _, p := range pubkeys { b.AddData(p) } - b.AddInt64(int64(nrequired)) // stack is now [... SIG SIG SIG PREDICATEHASH PUB PUB PUB M] - b.AddInt64(int64(len(pubkeys))) // stack is now [... SIG SIG SIG PREDICATEHASH PUB PUB PUB M N] - b.AddOp(vm.OP_CHECKMULTISIG).AddOp(vm.OP_VERIFY) // stack is now [... NARGS] - b.AddOp(vm.OP_FROMALTSTACK) // stack is now [... NARGS PREDICATE] - b.AddInt64(0).AddOp(vm.OP_CHECKPREDICATE) + b.AddInt64(int64(nrequired)) // stack is now [... SIG SIG SIG PREDICATEHASH PUB PUB PUB M] + b.AddInt64(int64(len(pubkeys))) // stack is now [... SIG SIG SIG PREDICATEHASH PUB PUB PUB M N] + b.AddOp(vm.OP_CHECKMULTISIG) // stack is now [... NARGS] return nil } diff --git a/protocol/vm/vmutil/script_test.go b/protocol/vm/vmutil/script_test.go index ae6b47af..020ac1be 100644 --- a/protocol/vm/vmutil/script_test.go +++ b/protocol/vm/vmutil/script_test.go @@ -1,10 +1,7 @@ package vmutil import ( - "bytes" "testing" - - "github.com/bytom/crypto/ed25519" ) // TestIsUnspendable ensures the IsUnspendable function returns the expected @@ -38,22 +35,3 @@ func TestIsUnspendable(t *testing.T) { } } } - -func TestP2SP(t *testing.T) { - pub1, _, _ := ed25519.GenerateKey(nil) - pub2, _, _ := ed25519.GenerateKey(nil) - prog, _ := P2SPMultiSigProgram([]ed25519.PublicKey{pub1, pub2}, 1) - pubs, n, err := ParseP2SPMultiSigProgram(prog) - if err != nil { - t.Fatal(err) - } - if n != 1 { - t.Errorf("expected nrequired=1, got %d", n) - } - if !bytes.Equal(pubs[0], pub1) { - t.Errorf("expected first pubkey to be %x, got %x", pub1, pubs[0]) - } - if !bytes.Equal(pubs[1], pub2) { - t.Errorf("expected second pubkey to be %x, got %x", pub2, pubs[1]) - } -} diff --git a/test/integration/standard_transaction_test.go b/test/integration/standard_transaction_test.go index 75057c4b..30ac7746 100644 --- a/test/integration/standard_transaction_test.go +++ b/test/integration/standard_transaction_test.go @@ -17,7 +17,7 @@ import ( ) func TestP2PKH(t *testing.T) { - dirPath, err := ioutil.TempDir(".", "") + dirPath, err := ioutil.TempDir(".", "TestP2PKH") if err != nil { t.Fatal(err) } @@ -37,7 +37,7 @@ func TestP2PKH(t *testing.T) { t.Fatal(err) } - xpub, err := hsm.XCreate("test_pub", "password") + xpub, err := hsm.XCreate("TestP2PKH", "password") if err != nil { t.Fatal(err) } @@ -72,7 +72,7 @@ func TestP2PKH(t *testing.T) { } func TestP2SH(t *testing.T) { - dirPath, err := ioutil.TempDir(".", "") + dirPath, err := ioutil.TempDir(".", "TestP2SH") if err != nil { t.Fatal(err) } @@ -92,12 +92,12 @@ func TestP2SH(t *testing.T) { t.Fatal(err) } - xpub1, err := hsm.XCreate("test_pub1", "password") + xpub1, err := hsm.XCreate("TestP2SH1", "password") if err != nil { t.Fatal(err) } - xpub2, err := hsm.XCreate("test_pub2", "password") + xpub2, err := hsm.XCreate("TestP2SH2", "password") if err != nil { t.Fatal(err) } @@ -132,7 +132,7 @@ func TestP2SH(t *testing.T) { } func TestMutilNodeSign(t *testing.T) { - dirPath, err := ioutil.TempDir(".", "") + dirPath, err := ioutil.TempDir(".", "TestMutilNodeSign") if err != nil { t.Fatal(err) } @@ -152,12 +152,12 @@ func TestMutilNodeSign(t *testing.T) { t.Fatal(err) } - xpub1, err := hsm.XCreate("test_pub1", "password1") + xpub1, err := hsm.XCreate("TestMutilNodeSign1", "password1") if err != nil { t.Fatal(err) } - xpub2, err := hsm.XCreate("test_pub2", "password2") + xpub2, err := hsm.XCreate("TestMutilNodeSign2", "password2") if err != nil { t.Fatal(err) } -- 2.11.0