Skip to content

Commit

Permalink
stdaddr: Use txscript for opcode definitions.
Browse files Browse the repository at this point in the history
This modifies the code to use the opcode definitions in txscript now
that the previous cyclic dependency no longer applies since txscript no
longer depends on stdaddr.
  • Loading branch information
davecgh committed Nov 18, 2021
1 parent 67d1e95 commit c0fe15f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 66 deletions.
1 change: 1 addition & 0 deletions database/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7I
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/siphash v1.2.2 h1:9DFz8tQwl9pTVt5iok/9zKyzA1Q6bRGiF3HPiEEVr9I=
github.com/dchest/siphash v1.2.2/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/decred/base58 v1.0.3 h1:KGZuh8d1WEMIrK0leQRM47W85KqCAdl2N+uagbctdDI=
github.com/decred/base58 v1.0.3/go.mod h1:pXP9cXCfM2sFLb2viz2FNIdeMWmZDBKG3ZBYbiSM78E=
Expand Down
2 changes: 2 additions & 0 deletions dcrutil/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7I
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/siphash v1.2.2 h1:9DFz8tQwl9pTVt5iok/9zKyzA1Q6bRGiF3HPiEEVr9I=
github.com/dchest/siphash v1.2.2/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/decred/base58 v1.0.3 h1:KGZuh8d1WEMIrK0leQRM47W85KqCAdl2N+uagbctdDI=
github.com/decred/base58 v1.0.3/go.mod h1:pXP9cXCfM2sFLb2viz2FNIdeMWmZDBKG3ZBYbiSM78E=
Expand All @@ -19,4 +20,5 @@ github.com/decred/dcrd/dcrec/edwards/v2 v2.0.1 h1:V6eqU1crZzuoFT4KG2LhaU5xDSdkHu
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.1/go.mod h1:d0H8xGMWbiIQP7gN3v2rByWUcuZPm9YsgmnfoxgbINc=
github.com/decred/dcrd/wire v1.4.0 h1:KmSo6eTQIvhXS0fLBQ/l7hG7QLcSJQKSwSyzSqJYDk0=
github.com/decred/dcrd/wire v1.4.0/go.mod h1:WxC/0K+cCAnBh+SKsRjIX9YPgvrjhmE+6pZlel1G7Ro=
github.com/decred/slog v1.1.0 h1:uz5ZFfmaexj1rEDgZvzQ7wjGkoSPjw2LCh8K+K1VrW4=
github.com/decred/slog v1.1.0/go.mod h1:kVXlGnt6DHy2fV5OjSeuvCJ0OmlmTF6LFpEPMu/fOY0=
2 changes: 2 additions & 0 deletions hdkeychain/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7I
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/siphash v1.2.2 h1:9DFz8tQwl9pTVt5iok/9zKyzA1Q6bRGiF3HPiEEVr9I=
github.com/dchest/siphash v1.2.2/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/decred/base58 v1.0.3 h1:KGZuh8d1WEMIrK0leQRM47W85KqCAdl2N+uagbctdDI=
github.com/decred/base58 v1.0.3/go.mod h1:pXP9cXCfM2sFLb2viz2FNIdeMWmZDBKG3ZBYbiSM78E=
Expand All @@ -19,4 +20,5 @@ github.com/decred/dcrd/dcrec/edwards/v2 v2.0.1 h1:V6eqU1crZzuoFT4KG2LhaU5xDSdkHu
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.1/go.mod h1:d0H8xGMWbiIQP7gN3v2rByWUcuZPm9YsgmnfoxgbINc=
github.com/decred/dcrd/wire v1.4.0 h1:KmSo6eTQIvhXS0fLBQ/l7hG7QLcSJQKSwSyzSqJYDk0=
github.com/decred/dcrd/wire v1.4.0/go.mod h1:WxC/0K+cCAnBh+SKsRjIX9YPgvrjhmE+6pZlel1G7Ro=
github.com/decred/slog v1.1.0 h1:uz5ZFfmaexj1rEDgZvzQ7wjGkoSPjw2LCh8K+K1VrW4=
github.com/decred/slog v1.1.0/go.mod h1:kVXlGnt6DHy2fV5OjSeuvCJ0OmlmTF6LFpEPMu/fOY0=
107 changes: 41 additions & 66 deletions txscript/stdaddr/addressv0.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,17 @@ import (
"github.com/decred/dcrd/dcrec"
"github.com/decred/dcrd/dcrec/edwards/v2"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)

// These are redefinitions of version 0 opcodes in the txscript package that are
// used in this package to generate payment scripts. Ultimately, this should be
// using the constant definitions from txscript instead, but it would currently
// create a cyclic dependency since txscript will need to depend on this package
// for signing.
const (
opData20 = 0x14
opData30 = 0x1e
opData32 = 0x20
opData33 = 0x21
op1 = 0x51
op2 = 0x52
opReturn = 0x6a
opDup = 0x76
opEqual = 0x87
opEqualVerify = 0x88
opHash160 = 0xa9
opCheckSig = 0xac
opSSTx = 0xba
opSSGen = 0xbb
opSSRTx = 0xbc
opSSTxChange = 0xbd
opCheckSigAlt = 0xbe
opTGen = 0xc3
"github.com/decred/dcrd/txscript/v4"
)

const (
// opPushSTEd25519 is the dcrec.STEd25519 signature type converted to the
// associated small integer data push opcode.
opPushSTEd25519 = op1
opPushSTEd25519 = txscript.OP_1

// opPushSTSchnorrSecp256k1 is the dcrec.STSchnorrSecp256k1 signature type
// converted to the associated small integer data push opcode.
opPushSTSchnorrSecp256k1 = op2
opPushSTSchnorrSecp256k1 = txscript.OP_2

// sigTypeSecp256k1PubKeyCompOddFlag specifies the bitmask to apply to the
// pubkey address signature type byte for those that deal with compressed
Expand Down Expand Up @@ -236,9 +211,9 @@ func (addr *AddressPubKeyEcdsaSecp256k1V0) PaymentScript() (uint16, []byte) {
// However, this address type intentionally only supports the compressed
// form.
var script [35]byte
script[0] = opData33
script[0] = txscript.OP_DATA_33
copy(script[1:34], addr.serializedPubKey)
script[34] = opCheckSig
script[34] = txscript.OP_CHECKSIG
return 0, script[:]
}

Expand Down Expand Up @@ -354,10 +329,10 @@ func (addr *AddressPubKeyEd25519V0) PaymentScript() (uint16, []byte) {
//
// Since the signature type is 1, it is pushed as a small integer.
var script [35]byte
script[0] = opData32
script[0] = txscript.OP_DATA_32
copy(script[1:33], addr.serializedPubKey)
script[33] = opPushSTEd25519
script[34] = opCheckSigAlt
script[34] = txscript.OP_CHECKSIGALT
return 0, script[:]
}

Expand Down Expand Up @@ -491,10 +466,10 @@ func (addr *AddressPubKeySchnorrSecp256k1V0) PaymentScript() (uint16, []byte) {
//
// Since the signature type is 2, it is pushed as a small integer.
var script [36]byte
script[0] = opData33
script[0] = txscript.OP_DATA_33
copy(script[1:34], addr.serializedPubKey)
script[34] = opPushSTSchnorrSecp256k1
script[35] = opCheckSigAlt
script[35] = txscript.OP_CHECKSIGALT
return 0, script[:]
}

Expand Down Expand Up @@ -590,12 +565,12 @@ func (addr *AddressPubKeyHashEcdsaSecp256k1V0) String() string {
func (addr *AddressPubKeyHashEcdsaSecp256k1V0) putPaymentScript(script []byte) {
// A pay-to-pubkey-hash-ecdsa-secp256k1 script is of the form:
// DUP HASH160 <20-byte hash> EQUALVERIFY CHECKSIG
script[0] = opDup
script[1] = opHash160
script[2] = opData20
script[0] = txscript.OP_DUP
script[1] = txscript.OP_HASH160
script[2] = txscript.OP_DATA_20
copy(script[3:23], addr.hash[:])
script[23] = opEqualVerify
script[24] = opCheckSig
script[23] = txscript.OP_EQUALVERIFY
script[24] = txscript.OP_CHECKSIG
}

// PaymentScript returns the script version associated with the address along
Expand All @@ -620,7 +595,7 @@ func (addr *AddressPubKeyHashEcdsaSecp256k1V0) VotingRightsScript() (uint16, []b
// of the form:
// SSTX [standard pay-to-pubkey-hash-ecdsa-secp256k1 script]
var script [p2pkhPaymentScriptLen + 1]byte
script[0] = opSSTx
script[0] = txscript.OP_SSTX
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand Down Expand Up @@ -669,8 +644,8 @@ func (addr *AddressPubKeyHashEcdsaSecp256k1V0) RewardCommitmentScript(amount, vo
// is NOT set for a public key hash.
limits := calcRewardCommitScriptLimits(voteFeeLimit, revocationFeeLimit)
var script [32]byte
script[0] = opReturn
script[1] = opData30
script[0] = txscript.OP_RETURN
script[1] = txscript.OP_DATA_30
copy(script[2:22], addr.hash[:])
binary.LittleEndian.PutUint64(script[22:30], uint64(amount) & ^commitP2SHFlag)
binary.LittleEndian.PutUint16(script[30:32], limits)
Expand All @@ -686,7 +661,7 @@ func (addr *AddressPubKeyHashEcdsaSecp256k1V0) StakeChangeScript() (uint16, []by
// A stake change script to this address type is of the form:
// SSTXCHANGE [standard pay-to-pubkey-hash-ecdsa-secp256k1 script]
var script [p2pkhPaymentScriptLen + 1]byte
script[0] = opSSTxChange
script[0] = txscript.OP_SSTXCHANGE
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand All @@ -703,7 +678,7 @@ func (addr *AddressPubKeyHashEcdsaSecp256k1V0) PayVoteCommitmentScript() (uint16
// type is of the form:
// SSGEN [standard pay-to-pubkey-hash-ecdsa-secp256k1 script]
var script [p2pkhPaymentScriptLen + 1]byte
script[0] = opSSGen
script[0] = txscript.OP_SSGEN
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand All @@ -720,7 +695,7 @@ func (addr *AddressPubKeyHashEcdsaSecp256k1V0) PayRevokeCommitmentScript() (uint
// A ticket revocation script to this address type is of the form:
// SSRTX [standard pay-to-pubkey-hash-ecdsa-secp256k1 script]
var script [p2pkhPaymentScriptLen + 1]byte
script[0] = opSSRTx
script[0] = txscript.OP_SSRTX
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand All @@ -735,7 +710,7 @@ func (addr *AddressPubKeyHashEcdsaSecp256k1V0) PayFromTreasuryScript() (uint16,
// this address type is of the form:
// TGEN [standard pay-to-pubkey-hash-ecdsa-secp256k1 script]
var script [p2pkhPaymentScriptLen + 1]byte
script[0] = opTGen
script[0] = txscript.OP_TGEN
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand Down Expand Up @@ -810,13 +785,13 @@ func (addr *AddressPubKeyHashEd25519V0) PaymentScript() (uint16, []byte) {
//
// Since the signature type is 1, it is pushed as a small integer.
var script [26]byte
script[0] = opDup
script[1] = opHash160
script[2] = opData20
script[0] = txscript.OP_DUP
script[1] = txscript.OP_HASH160
script[2] = txscript.OP_DATA_20
copy(script[3:23], addr.hash[:])
script[23] = opEqualVerify
script[23] = txscript.OP_EQUALVERIFY
script[24] = opPushSTEd25519
script[25] = opCheckSigAlt
script[25] = txscript.OP_CHECKSIGALT
return 0, script[:]
}

Expand Down Expand Up @@ -897,13 +872,13 @@ func (addr *AddressPubKeyHashSchnorrSecp256k1V0) PaymentScript() (uint16, []byte
//
// Since the signature type is 2, it is pushed as a small integer.
var script [26]byte
script[0] = opDup
script[1] = opHash160
script[2] = opData20
script[0] = txscript.OP_DUP
script[1] = txscript.OP_HASH160
script[2] = txscript.OP_DATA_20
copy(script[3:23], addr.hash[:])
script[23] = opEqualVerify
script[23] = txscript.OP_EQUALVERIFY
script[24] = opPushSTSchnorrSecp256k1
script[25] = opCheckSigAlt
script[25] = txscript.OP_CHECKSIGALT
return 0, script[:]
}

Expand Down Expand Up @@ -993,10 +968,10 @@ func (addr *AddressScriptHashV0) String() string {
func (addr *AddressScriptHashV0) putPaymentScript(script []byte) {
// A pay-to-script-hash script is of the form:
// HASH160 <20-byte hash> EQUAL
script[0] = opHash160
script[1] = opData20
script[0] = txscript.OP_HASH160
script[1] = txscript.OP_DATA_20
copy(script[2:22], addr.hash[:])
script[22] = opEqual
script[22] = txscript.OP_EQUAL
}

// PaymentScript returns the script version associated with the address along
Expand All @@ -1021,7 +996,7 @@ func (addr *AddressScriptHashV0) VotingRightsScript() (uint16, []byte) {
// of the form:
// SSTX [standard pay-to-script-hash script]
var script [p2shPaymentScriptLen + 1]byte
script[0] = opSSTx
script[0] = txscript.OP_SSTX
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand Down Expand Up @@ -1050,8 +1025,8 @@ func (addr *AddressScriptHashV0) RewardCommitmentScript(amount, voteFeeLimit, re
// is set for a script hash.
limits := calcRewardCommitScriptLimits(voteFeeLimit, revocationFeeLimit)
var script [32]byte
script[0] = opReturn
script[1] = opData30
script[0] = txscript.OP_RETURN
script[1] = txscript.OP_DATA_30
copy(script[2:22], addr.hash[:])
binary.LittleEndian.PutUint64(script[22:30], uint64(amount)|commitP2SHFlag)
binary.LittleEndian.PutUint16(script[30:32], limits)
Expand All @@ -1067,7 +1042,7 @@ func (addr *AddressScriptHashV0) StakeChangeScript() (uint16, []byte) {
// A stake change script to this address type is of the form:
// SSTXCHANGE [standard pay-to-script-hash script]
var script [p2shPaymentScriptLen + 1]byte
script[0] = opSSTxChange
script[0] = txscript.OP_SSTXCHANGE
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand All @@ -1084,7 +1059,7 @@ func (addr *AddressScriptHashV0) PayVoteCommitmentScript() (uint16, []byte) {
// type is of the form:
// SSGEN [standard pay-to-script-hash script]
var script [p2shPaymentScriptLen + 1]byte
script[0] = opSSGen
script[0] = txscript.OP_SSGEN
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand All @@ -1101,7 +1076,7 @@ func (addr *AddressScriptHashV0) PayRevokeCommitmentScript() (uint16, []byte) {
// A ticket revocation script to this address type is of the form:
// SSRTX [standard pay-to-script-hash script]
var script [p2shPaymentScriptLen + 1]byte
script[0] = opSSRTx
script[0] = txscript.OP_SSRTX
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand All @@ -1116,7 +1091,7 @@ func (addr *AddressScriptHashV0) PayFromTreasuryScript() (uint16, []byte) {
// this address type is of the form:
// TGEN [standard pay-to-script-hash script]
var script [p2shPaymentScriptLen + 1]byte
script[0] = opTGen
script[0] = txscript.OP_TGEN
addr.putPaymentScript(script[1:])
return 0, script[:]
}
Expand Down

0 comments on commit c0fe15f

Please sign in to comment.