Skip to content

Commit

Permalink
Optimize address API
Browse files Browse the repository at this point in the history
  • Loading branch information
lochjin committed Dec 18, 2023
1 parent 69de69c commit db10ce4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
19 changes: 19 additions & 0 deletions services/address/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package address

import (
"encoding/hex"
"testing"
)

func TestNewAddresses(t *testing.T) {
privateKeyHex := "fff2cefe258ca60ae5f5abec99b5d63e2a561c40d784ee50b04eddf8efc84b0d"
privateKey, addr, eaddr, err := NewAddresses(privateKeyHex)
if err != nil {
t.Fatal(err)
}
pkHex := hex.EncodeToString(privateKey.Serialize())
if pkHex != privateKeyHex {
t.Fatalf("%s != %s (expect)", pkHex, privateKeyHex)
}
t.Logf("privateKey:%s addr:%s pkAddr:%s evmAddr:%s", pkHex, addr.PKHAddress().String(), addr.String(), eaddr.String())
}
33 changes: 20 additions & 13 deletions services/address/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/Qitmeer/qng/rpc"
"github.com/Qitmeer/qng/rpc/api"
"github.com/Qitmeer/qng/rpc/client/cmds"
ecommon "github.com/ethereum/go-ethereum/common"
"sync"
)

Expand Down Expand Up @@ -94,30 +95,36 @@ func NewPrivateAddressAPI(ai *AddressApi) *PrivateAddressAPI {
}

func (api *PrivateAddressAPI) GetAddresses(privateKeyHex string) (interface{}, error) {
privkeyByte, err := hex.DecodeString(privateKeyHex)
privateKey, addr, eaddr, err := NewAddresses(privateKeyHex)
if err != nil {
return nil, err
}
result := qjson.OrderedResult{
qjson.KV{Key: "PrivateKey", Val: hex.EncodeToString(privateKey.Serialize())},
qjson.KV{Key: "PKHAddress", Val: addr.PKHAddress().String()},
qjson.KV{Key: "PKAddress", Val: addr.String()},
qjson.KV{Key: "MeerEVM Address", Val: eaddr.String()},
}
return result, nil
}

func NewAddresses(privateKeyHex string) (ecc.PrivateKey, *address.SecpPubKeyAddress, ecommon.Address, error) {
privkeyByte, err := hex.DecodeString(privateKeyHex)
if err != nil {
return nil, nil, ecommon.Address{}, err
}
if len(privkeyByte) != 32 {
return nil, fmt.Errorf("error length:%d", len(privkeyByte))
return nil, nil, ecommon.Address{}, fmt.Errorf("error length:%d", len(privkeyByte))
}
privateKey, pubKey := ecc.Secp256k1.PrivKeyFromBytes(privkeyByte)

serializedKey := pubKey.SerializeCompressed()
addr, err := address.NewSecpPubKeyAddress(serializedKey, params.ActiveNetParams.Params)
if err != nil {
return nil, err
return nil, nil, ecommon.Address{}, err
}
eaddr, err := common.NewMeerEVMAddress(pubKey.SerializeUncompressed())
if err != nil {
return nil, err
}
result := qjson.OrderedResult{
qjson.KV{Key: "PrivateKey", Val: hex.EncodeToString(privateKey.Serialize())},
qjson.KV{Key: "PKHAddress", Val: addr.PKHAddress().String()},
qjson.KV{Key: "PKAddress", Val: addr.String()},
qjson.KV{Key: "MeerEVM Address", Val: eaddr.String()},
return nil, nil, ecommon.Address{}, err
}

return result, nil
return privateKey, addr, eaddr, nil
}

0 comments on commit db10ce4

Please sign in to comment.