From 78991390399ef1f48a331b01b82019b964f9cc91 Mon Sep 17 00:00:00 2001 From: fjchen7 Date: Thu, 5 May 2022 18:23:43 +0800 Subject: [PATCH] Add type `FullBech32m` for `parsedAddress` (#126) --- README.md | 2 +- address/address.go | 13 +++++++------ address/address_test.go | 27 ++++++++++++++++----------- address/address_tools_test.go | 6 +++--- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1c3ad1cc..509fce2c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/address/address.go b/address/address.go index 9c581a10..75dcdc6e 100644 --- a/address/address.go +++ b/address/address.go @@ -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" @@ -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), @@ -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:]), diff --git a/address/address_test.go b/address/address_test.go index 809bf904..86580dfa 100644 --- a/address/address_test.go +++ b/address/address_test.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) { @@ -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) @@ -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) @@ -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) @@ -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) @@ -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, @@ -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, diff --git a/address/address_tools_test.go b/address/address_tools_test.go index 7a297991..33d4d9f2 100644 --- a/address/address_tools_test.go +++ b/address/address_tools_test.go @@ -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) @@ -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) @@ -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)