Skip to content

Commit

Permalink
Add type FullBech32m for parsedAddress (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjchen7 authored May 5, 2022
1 parent e19b75c commit 7899139
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func main() {
Index: 1,
},
},
}, uint(len(transaction.Secp256k1EmptyWitnessArgPlaceholder)))
}, uint(len(transaction.Secp256k1SignaturePlaceholder)))

if err != nil {
log.Fatalf("add inputs to transaction error: %v", err)
Expand Down
13 changes: 7 additions & 6 deletions address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const (
Mainnet Mode = "ckb"
Testnet Mode = "ckt"

TypeFull Type = "Full"
TypeShort Type = "Short"
Short Type = "Short"
FullBech32 Type = "FullBech32"
FullBech32m Type = "FullBech32m"

TYPE_FULL_WITH_BECH32M = "00"
ShortFormat = "01"
Expand Down Expand Up @@ -180,7 +181,7 @@ func Parse(address string) (*ParsedAddress, error) {
var addressType Type
var script types.Script
if strings.HasPrefix(payload, "01") {
addressType = TypeShort
addressType = Short
if CodeHashIndexSingleSig == payload[2:4] {
script = types.Script{
CodeHash: types.HexToHash(transaction.SECP256K1_BLAKE160_SIGHASH_ALL_TYPE_HASH),
Expand All @@ -205,21 +206,21 @@ func Parse(address string) (*ParsedAddress, error) {
}
}
} else if strings.HasPrefix(payload, "02") {
addressType = TypeFull
addressType = FullBech32
script = types.Script{
CodeHash: types.HexToHash(payload[2:66]),
HashType: types.HashTypeData,
Args: common.Hex2Bytes(payload[66:]),
}
} else if strings.HasPrefix(payload, "04") {
addressType = TypeFull
addressType = FullBech32
script = types.Script{
CodeHash: types.HexToHash(payload[2:66]),
HashType: types.HashTypeType,
Args: common.Hex2Bytes(payload[66:]),
}
} else if strings.HasPrefix(payload, "00") {
addressType = TypeFull
addressType = FullBech32m
script = types.Script{
CodeHash: types.HexToHash(payload[2:66]),
Args: common.Hex2Bytes(payload[68:]),
Expand Down
27 changes: 16 additions & 11 deletions address/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestParse(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, Mainnet, mnAddress.Mode)
assert.Equal(t, TypeShort, mnAddress.Type)
assert.Equal(t, Short, mnAddress.Type)
assert.Equal(t, script.CodeHash, mnAddress.Script.CodeHash)
assert.Equal(t, script.HashType, mnAddress.Script.HashType)
assert.Equal(t, script.Args, mnAddress.Script.Args)
Expand All @@ -142,7 +142,7 @@ func TestParse(t *testing.T) {
mParsedAddress, err := Parse("ckb1qypylv479ewscx3ms620sv34pgeuz6zagaaqvrugu7")
assert.Nil(t, err)
assert.Equal(t, Mainnet, mParsedAddress.Mode)
assert.Equal(t, TypeShort, mParsedAddress.Type)
assert.Equal(t, Short, mParsedAddress.Type)
assert.Equal(t, mAcpLock.CodeHash, mParsedAddress.Script.CodeHash)
assert.Equal(t, mAcpLock.HashType, mParsedAddress.Script.HashType)
assert.Equal(t, mAcpLock.Args, mParsedAddress.Script.Args)
Expand All @@ -156,7 +156,7 @@ func TestParse(t *testing.T) {
tParsedAddress, err := Parse("ckt1qypylv479ewscx3ms620sv34pgeuz6zagaaq3xzhsz")
assert.Nil(t, err)
assert.Equal(t, Testnet, tParsedAddress.Mode)
assert.Equal(t, TypeShort, tParsedAddress.Type)
assert.Equal(t, Short, tParsedAddress.Type)
assert.Equal(t, tAcpLock.CodeHash, tParsedAddress.Script.CodeHash)
assert.Equal(t, tAcpLock.HashType, tParsedAddress.Script.HashType)
assert.Equal(t, tAcpLock.Args, tParsedAddress.Script.Args)
Expand All @@ -172,7 +172,7 @@ func TestParse(t *testing.T) {
mParsedAddress, err := Parse("ckb1qypylv479ewscx3ms620sv34pgeuz6zagaaqcehzz9g")
assert.Nil(t, err)
assert.Equal(t, Mainnet, mParsedAddress.Mode)
assert.Equal(t, TypeShort, mParsedAddress.Type)
assert.Equal(t, Short, mParsedAddress.Type)
assert.Equal(t, mAcpLock.CodeHash, mParsedAddress.Script.CodeHash)
assert.Equal(t, mAcpLock.HashType, mParsedAddress.Script.HashType)
assert.Equal(t, mAcpLock.Args, mParsedAddress.Script.Args)
Expand All @@ -186,10 +186,15 @@ func TestParse(t *testing.T) {
tParsedAddress, err := Parse("ckt1qypylv479ewscx3ms620sv34pgeuz6zagaaqc9q8fqw")
assert.Nil(t, err)
assert.Equal(t, Testnet, tParsedAddress.Mode)
assert.Equal(t, TypeShort, tParsedAddress.Type)
assert.Equal(t, Short, tParsedAddress.Type)
assert.Equal(t, tAcpLock.CodeHash, tParsedAddress.Script.CodeHash)
assert.Equal(t, tAcpLock.HashType, tParsedAddress.Script.HashType)
assert.Equal(t, tAcpLock.Args, tParsedAddress.Script.Args)

tParsedAddress, err = Parse("ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4")
assert.Nil(t, err)
assert.Equal(t, Testnet, tParsedAddress.Mode)
assert.Equal(t, FullBech32m, tParsedAddress.Type)
})

t.Run("parse short payload acp address with ckb minimum limit and udt minimum limit", func(t *testing.T) {
Expand All @@ -202,7 +207,7 @@ func TestParse(t *testing.T) {
mParsedAddress, err := Parse("ckb1qypylv479ewscx3ms620sv34pgeuz6zagaaqcqgzc5xlw")
assert.Nil(t, err)
assert.Equal(t, Mainnet, mParsedAddress.Mode)
assert.Equal(t, TypeShort, mParsedAddress.Type)
assert.Equal(t, Short, mParsedAddress.Type)
assert.Equal(t, mAcpLock.CodeHash, mParsedAddress.Script.CodeHash)
assert.Equal(t, mAcpLock.HashType, mParsedAddress.Script.HashType)
assert.Equal(t, mAcpLock.Args, mParsedAddress.Script.Args)
Expand All @@ -216,7 +221,7 @@ func TestParse(t *testing.T) {
tParsedAddress, err := Parse("ckt1qypylv479ewscx3ms620sv34pgeuz6zagaaqcqgr072sz")
assert.Nil(t, err)
assert.Equal(t, Testnet, tParsedAddress.Mode)
assert.Equal(t, TypeShort, tParsedAddress.Type)
assert.Equal(t, Short, tParsedAddress.Type)
assert.Equal(t, tAcpLock.CodeHash, tParsedAddress.Script.CodeHash)
assert.Equal(t, tAcpLock.HashType, tParsedAddress.Script.HashType)
assert.Equal(t, tAcpLock.Args, tParsedAddress.Script.Args)
Expand All @@ -232,7 +237,7 @@ func TestParse(t *testing.T) {
mParsedAddress, err := Parse("ckb1qnfkjktl73ljn77q637judm4xux3y59c29qvvu8ywx90wy5c8g34gnajhch96rq68wrff7pjx59r8stgt4rh5rqpqy532xj3")
assert.Nil(t, err)
assert.Equal(t, Mainnet, mParsedAddress.Mode)
assert.Equal(t, TypeFull, mParsedAddress.Type)
assert.Equal(t, FullBech32, mParsedAddress.Type)
assert.Equal(t, mAcpLock.CodeHash, mParsedAddress.Script.CodeHash)
assert.Equal(t, mAcpLock.HashType, mParsedAddress.Script.HashType)
assert.Equal(t, mAcpLock.Args, mParsedAddress.Script.Args)
Expand All @@ -246,7 +251,7 @@ func TestParse(t *testing.T) {
tParsedAddress, err := Parse("ckt1qs6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vnajhch96rq68wrff7pjx59r8stgt4rh5rqpqy2a9ak4")
assert.Nil(t, err)
assert.Equal(t, Testnet, tParsedAddress.Mode)
assert.Equal(t, TypeFull, tParsedAddress.Type)
assert.Equal(t, FullBech32, tParsedAddress.Type)
assert.Equal(t, tAcpLock.CodeHash, tParsedAddress.Script.CodeHash)
assert.Equal(t, tAcpLock.HashType, tParsedAddress.Script.HashType)
assert.Equal(t, tAcpLock.Args, tParsedAddress.Script.Args)
Expand Down Expand Up @@ -289,7 +294,7 @@ func TestValidateChequeAddress(t *testing.T) {
},
&ParsedAddress{
Mode: Testnet,
Type: TypeShort,
Type: Short,
Script: &types.Script{
CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"),
HashType: types.HashTypeType,
Expand All @@ -306,7 +311,7 @@ func TestValidateChequeAddress(t *testing.T) {
},
&ParsedAddress{
Mode: Mainnet,
Type: TypeShort,
Type: Short,
Script: &types.Script{
CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"),
HashType: types.HashTypeType,
Expand Down
6 changes: 3 additions & 3 deletions address/address_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestBech32mTypeFullMainnetAddressGenerate(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, Mainnet, mnAddress.Mode)
assert.Equal(t, TypeFull, mnAddress.Type)
assert.Equal(t, FullBech32, mnAddress.Type)
assert.Equal(t, script.CodeHash, mnAddress.Script.CodeHash)
assert.Equal(t, script.HashType, mnAddress.Script.HashType)
assert.Equal(t, script.Args, mnAddress.Script.Args)
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestBech32mDataFullMainnetAddressGenerate(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, Mainnet, mnAddress.Mode)
assert.Equal(t, TypeFull, mnAddress.Type)
assert.Equal(t, FullBech32, mnAddress.Type)
assert.Equal(t, script.CodeHash, mnAddress.Script.CodeHash)
assert.Equal(t, script.HashType, mnAddress.Script.HashType)
assert.Equal(t, script.Args, mnAddress.Script.Args)
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestBech32mData1FullMainnetAddressGenerate(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, Mainnet, mnAddress.Mode)
assert.Equal(t, TypeFull, mnAddress.Type)
assert.Equal(t, FullBech32, mnAddress.Type)
assert.Equal(t, script.CodeHash, mnAddress.Script.CodeHash)
assert.Equal(t, script.HashType, mnAddress.Script.HashType)
assert.Equal(t, script.Args, mnAddress.Script.Args)
Expand Down

0 comments on commit 7899139

Please sign in to comment.