From df207a76bdab2766d577ed96a5e0a18371ddf09c Mon Sep 17 00:00:00 2001 From: Yukai Date: Tue, 7 Jun 2022 11:25:31 -0700 Subject: [PATCH] Add test case --- service/helper.go | 3 ++- service/service_construction.go | 2 +- service/service_construction_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/service/helper.go b/service/helper.go index 59456643..20d98b3a 100644 --- a/service/helper.go +++ b/service/helper.go @@ -109,7 +109,8 @@ func ChecksumAddress(address string) (string, bool) { // isPChain checks network identifier to make sure sub-network identifier set to "P" func isPChain(networkIdentifier *types.NetworkIdentifier) bool { - if networkIdentifier.SubNetworkIdentifier != nil && + if networkIdentifier != nil && + networkIdentifier.SubNetworkIdentifier != nil && networkIdentifier.SubNetworkIdentifier.Network == mapper.PChainNetworkIdentifier { return true } diff --git a/service/service_construction.go b/service/service_construction.go index 56fc8852..a3aefe9d 100644 --- a/service/service_construction.go +++ b/service/service_construction.go @@ -233,7 +233,7 @@ func (s ConstructionService) ConstructionDerive( } chainIDAlias, hrp, getErr := getAliasAndHRP(req.NetworkIdentifier) - if err != nil { + if getErr != nil { return nil, getErr } diff --git a/service/service_construction_test.go b/service/service_construction_test.go index 065b30da..d8be8a61 100644 --- a/service/service_construction_test.go +++ b/service/service_construction_test.go @@ -285,6 +285,33 @@ func TestConstructionDerive(t *testing.T) { resp.AccountIdentifier.Address, ) }) + + t.Run("p-chain address", func(t *testing.T) { + src := "02e0d4392cfa224d4be19db416b3cf62e90fb2b7015e7b62a95c8cb490514943f6" + b, _ := hex.DecodeString(src) + + resp, err := service.ConstructionDerive( + context.Background(), + &types.ConstructionDeriveRequest{ + NetworkIdentifier: &types.NetworkIdentifier{ + Network: mapper.FujiNetwork, + SubNetworkIdentifier: &types.SubNetworkIdentifier{ + Network: mapper.PChainNetworkIdentifier, + }, + }, + PublicKey: &types.PublicKey{ + Bytes: b, + CurveType: types.Secp256k1, + }, + }, + ) + assert.Nil(t, err) + assert.Equal( + t, + "P-fuji15f9g0h5xkr5cp47n6u3qxj6yjtzzzrdr23a3tl", + resp.AccountIdentifier.Address, + ) + }) } func forceMarshalMap(t *testing.T, i interface{}) map[string]interface{} {