mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 12:14:21 +01:00
Drop useless encoding (#110)
* drop asec private key encoding * remove pubkey/relaykey/url encoding in common pkg * fix pubkey encoding * remove SecKey
This commit is contained in:
@@ -119,12 +119,12 @@ func getWalletPublicKey() (*secp256k1.PublicKey, error) {
|
|||||||
return nil, fmt.Errorf("public key not found")
|
return nil, fmt.Errorf("public key not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, publicKey, err := common.DecodePubKey(publicKeyString)
|
publicKeyBytes, err := hex.DecodeString(publicKeyString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return publicKey, nil
|
return secp256k1.ParsePubKey(publicKeyBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServiceProviderPublicKey() (*secp256k1.PublicKey, error) {
|
func getServiceProviderPublicKey() (*secp256k1.PublicKey, error) {
|
||||||
@@ -138,12 +138,12 @@ func getServiceProviderPublicKey() (*secp256k1.PublicKey, error) {
|
|||||||
return nil, fmt.Errorf("ark public key not found")
|
return nil, fmt.Errorf("ark public key not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, pubKey, err := common.DecodePubKey(arkPubKey)
|
pubKeyBytes, err := hex.DecodeString(arkPubKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return pubKey, nil
|
return secp256k1.ParsePubKey(pubKeyBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func coinSelect(vtxos []vtxo, amount uint64) ([]vtxo, uint64, error) {
|
func coinSelect(vtxos []vtxo, amount uint64) ([]vtxo, uint64, error) {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
||||||
"github.com/ark-network/ark/common"
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@@ -108,16 +107,7 @@ func initWallet(ctx *cli.Context, key, password string) error {
|
|||||||
privateKey = secp256k1.PrivKeyFromBytes(privKeyBytes)
|
privateKey = secp256k1.PrivKeyFromBytes(privKeyBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
cypher := NewAES128Cypher()
|
encryptedPrivateKey, err := NewAES128Cypher().Encrypt(privateKey.Serialize(), []byte(password))
|
||||||
|
|
||||||
arkNetwork, _ := getNetwork()
|
|
||||||
|
|
||||||
publicKey, err := common.EncodePubKey(arkNetwork.PubKey, privateKey.PubKey())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
encryptedPrivateKey, err := cypher.Encrypt(privateKey.Serialize(), []byte(password))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -127,7 +117,7 @@ func initWallet(ctx *cli.Context, key, password string) error {
|
|||||||
state := map[string]string{
|
state := map[string]string{
|
||||||
"encrypted_private_key": hex.EncodeToString(encryptedPrivateKey),
|
"encrypted_private_key": hex.EncodeToString(encryptedPrivateKey),
|
||||||
"password_hash": hex.EncodeToString(passwordHash),
|
"password_hash": hex.EncodeToString(passwordHash),
|
||||||
"public_key": publicKey,
|
"public_key": hex.EncodeToString(privateKey.PubKey().SerializeCompressed()),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := setState(state); err != nil {
|
if err := setState(state); err != nil {
|
||||||
|
|||||||
@@ -2,95 +2,11 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcutil/bech32"
|
"github.com/btcsuite/btcd/btcutil/bech32"
|
||||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
ProtoKey = "ark"
|
|
||||||
RelayKey = "relays"
|
|
||||||
RelaySep = "-"
|
|
||||||
)
|
|
||||||
|
|
||||||
func EncodeSecKey(hrp string, key *secp256k1.PrivateKey) (seckey string, err error) {
|
|
||||||
if key == nil {
|
|
||||||
return "", fmt.Errorf("missing secret key")
|
|
||||||
}
|
|
||||||
if hrp != MainNet.SecKey && hrp != TestNet.SecKey {
|
|
||||||
return "", fmt.Errorf("invalid prefix")
|
|
||||||
}
|
|
||||||
grp, err := bech32.ConvertBits(key.Serialize(), 8, 5, true)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
seckey, err = bech32.EncodeM(hrp, grp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func DecodeSecKey(key string) (hrp string, seckey *secp256k1.PrivateKey, err error) {
|
|
||||||
prefix, buf, err := bech32.DecodeNoLimit(key)
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("invalid secret key: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if prefix != MainNet.SecKey && prefix != TestNet.SecKey {
|
|
||||||
err = fmt.Errorf("invalid prefix")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
grp, err := bech32.ConvertBits(buf, 5, 8, false)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hrp = prefix
|
|
||||||
seckey = secp256k1.PrivKeyFromBytes(grp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func EncodePubKey(hrp string, key *secp256k1.PublicKey) (pubkey string, err error) {
|
|
||||||
if key == nil {
|
|
||||||
err = fmt.Errorf("missing public key")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if hrp != MainNet.PubKey && hrp != TestNet.PubKey {
|
|
||||||
err = fmt.Errorf("invalid prefix")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
grp, err := bech32.ConvertBits(key.SerializeCompressed(), 8, 5, true)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
pubkey, err = bech32.EncodeM(hrp, grp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func DecodePubKey(key string) (hrp string, pubkey *secp256k1.PublicKey, err error) {
|
|
||||||
prefix, buf, err := bech32.DecodeNoLimit(key)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if prefix != MainNet.PubKey && prefix != TestNet.PubKey {
|
|
||||||
err = fmt.Errorf("invalid prefix")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
grp, err := bech32.ConvertBits(buf, 5, 8, false)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(grp) < 32 {
|
|
||||||
err = fmt.Errorf("invalid public key length")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
pubkey, err = secp256k1.ParsePubKey(grp)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hrp = prefix
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func EncodeAddress(hrp string, userKey, aspKey *secp256k1.PublicKey) (addr string, err error) {
|
func EncodeAddress(hrp string, userKey, aspKey *secp256k1.PublicKey) (addr string, err error) {
|
||||||
if userKey == nil {
|
if userKey == nil {
|
||||||
err = fmt.Errorf("missing public key")
|
err = fmt.Errorf("missing public key")
|
||||||
@@ -141,96 +57,3 @@ func DecodeAddress(addr string) (hrp string, userKey *secp256k1.PublicKey, aspKe
|
|||||||
aspKey = aKey
|
aspKey = aKey
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func EncodeRelayKey(hrp string, key *secp256k1.PublicKey) (pubkey string, err error) {
|
|
||||||
if key == nil {
|
|
||||||
err = fmt.Errorf("missing relay key")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if hrp != MainNet.RelayKey && hrp != TestNet.RelayKey {
|
|
||||||
err = fmt.Errorf("invalid prefix")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
grp, err := bech32.ConvertBits(key.SerializeCompressed(), 8, 5, true)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
pubkey, err = bech32.EncodeM(hrp, grp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func DecodeRelayKey(key string) (hrp string, pubkey *secp256k1.PublicKey, err error) {
|
|
||||||
prefix, buf, err := bech32.DecodeNoLimit(key)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if prefix != MainNet.RelayKey && prefix != TestNet.RelayKey {
|
|
||||||
err = fmt.Errorf("invalid prefix")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
grp, err := bech32.ConvertBits(buf, 5, 8, false)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(grp) < 32 {
|
|
||||||
err = fmt.Errorf("invalid public key length")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
pubkey, err = secp256k1.ParsePubKey(grp)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hrp = prefix
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func EncodeUrl(host string, relays ...string) (arkurl string, err error) {
|
|
||||||
_, _, err = DecodePubKey(host)
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("invalid public key: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, r := range relays {
|
|
||||||
_, _, err = DecodeRelayKey(r)
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("invalid relay public key: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
u := url.URL{Scheme: ProtoKey, Host: host}
|
|
||||||
q := u.Query()
|
|
||||||
if len(relays) > 0 {
|
|
||||||
q.Add(RelayKey, strings.Join(relays, RelaySep))
|
|
||||||
}
|
|
||||||
u.RawQuery = q.Encode()
|
|
||||||
arkurl = u.String()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func DecodeUrl(arkurl string) (host string, relays []string, err error) {
|
|
||||||
u, err := url.Parse(arkurl)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if u.Scheme != ProtoKey {
|
|
||||||
err = fmt.Errorf("invalid proto")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, _, err = DecodePubKey(u.Host)
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("invalid public key: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
list := strings.Split(u.Query().Get(RelayKey), RelaySep)
|
|
||||||
for _, r := range list {
|
|
||||||
_, _, err = DecodeRelayKey(r)
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("invalid relay public key: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host = u.Host
|
|
||||||
relays = make([]string, len(list))
|
|
||||||
copy(relays, list)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -21,90 +21,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSecretKeyEncoding(t *testing.T) {
|
|
||||||
fixtures := struct {
|
|
||||||
SecretKey struct {
|
|
||||||
Valid []struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
Expected string `json:"expected"`
|
|
||||||
} `json:"valid"`
|
|
||||||
Invalid []struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
ExpectedError string `json:"expectedError"`
|
|
||||||
} `json:"invalid"`
|
|
||||||
} `json:"secretKey"`
|
|
||||||
}{}
|
|
||||||
err := json.Unmarshal(f, &fixtures)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
t.Run("valid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.SecretKey.Valid {
|
|
||||||
hrp, key, err := common.DecodeSecKey(f.Key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEmpty(t, hrp)
|
|
||||||
require.NotNil(t, key)
|
|
||||||
|
|
||||||
keyHex := hex.EncodeToString(key.Serialize())
|
|
||||||
require.Equal(t, f.Expected, keyHex)
|
|
||||||
|
|
||||||
keyStr, err := common.EncodeSecKey(hrp, key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, f.Key, keyStr)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("invalid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.SecretKey.Invalid {
|
|
||||||
hrp, key, err := common.DecodeSecKey(f.Key)
|
|
||||||
require.EqualError(t, err, f.ExpectedError)
|
|
||||||
require.Empty(t, hrp)
|
|
||||||
require.Nil(t, key)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPublicKeyEncoding(t *testing.T) {
|
|
||||||
fixtures := struct {
|
|
||||||
PublicKey struct {
|
|
||||||
Valid []struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
Expected string `json:"expected"`
|
|
||||||
} `json:"valid"`
|
|
||||||
Invalid []struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
ExpectedError string `json:"expectedError"`
|
|
||||||
} `json:"invalid"`
|
|
||||||
} `json:"publicKey"`
|
|
||||||
}{}
|
|
||||||
err := json.Unmarshal(f, &fixtures)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
t.Run("valid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.PublicKey.Valid {
|
|
||||||
hrp, key, err := common.DecodePubKey(f.Key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEmpty(t, hrp)
|
|
||||||
require.NotNil(t, key)
|
|
||||||
|
|
||||||
keyHex := hex.EncodeToString(key.SerializeCompressed())
|
|
||||||
require.Equal(t, f.Expected, keyHex)
|
|
||||||
|
|
||||||
keyStr, err := common.EncodePubKey(hrp, key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, f.Key, keyStr)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("invalid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.PublicKey.Invalid {
|
|
||||||
hrp, key, err := common.DecodePubKey(f.Key)
|
|
||||||
require.EqualError(t, err, f.ExpectedError)
|
|
||||||
require.Empty(t, hrp)
|
|
||||||
require.Nil(t, key)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAddressEncoding(t *testing.T) {
|
func TestAddressEncoding(t *testing.T) {
|
||||||
fixtures := struct {
|
fixtures := struct {
|
||||||
Address struct {
|
Address struct {
|
||||||
@@ -130,13 +46,11 @@ func TestAddressEncoding(t *testing.T) {
|
|||||||
require.NotNil(t, userKey)
|
require.NotNil(t, userKey)
|
||||||
require.NotNil(t, aspKey)
|
require.NotNil(t, aspKey)
|
||||||
|
|
||||||
userKeyStr, err := common.EncodePubKey(common.MainNet.PubKey, userKey)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, f.ExpectedUserKey, userKeyStr)
|
require.Equal(t, f.ExpectedUserKey, hex.EncodeToString(userKey.SerializeCompressed()))
|
||||||
|
|
||||||
aspKeyStr, err := common.EncodePubKey(common.MainNet.PubKey, aspKey)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, f.ExpectedAspKey, aspKeyStr)
|
require.Equal(t, f.ExpectedAspKey, hex.EncodeToString(aspKey.SerializeCompressed()))
|
||||||
|
|
||||||
addr, err := common.EncodeAddress(hrp, userKey, aspKey)
|
addr, err := common.EncodeAddress(hrp, userKey, aspKey)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -154,89 +68,3 @@ func TestAddressEncoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRelayKeyEncoding(t *testing.T) {
|
|
||||||
fixtures := struct {
|
|
||||||
RelayKey struct {
|
|
||||||
Valid []struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
Expected string `json:"expected"`
|
|
||||||
} `json:"valid"`
|
|
||||||
Invalid []struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
ExpectedError string `json:"expectedError"`
|
|
||||||
} `json:"invalid"`
|
|
||||||
} `json:"relayKey"`
|
|
||||||
}{}
|
|
||||||
err := json.Unmarshal(f, &fixtures)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
t.Run("valid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.RelayKey.Valid {
|
|
||||||
hrp, key, err := common.DecodeRelayKey(f.Key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEmpty(t, hrp)
|
|
||||||
require.NotNil(t, key)
|
|
||||||
|
|
||||||
keyHex := hex.EncodeToString(key.SerializeCompressed())
|
|
||||||
require.Equal(t, f.Expected, keyHex)
|
|
||||||
|
|
||||||
keyStr, err := common.EncodeRelayKey(hrp, key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, f.Key, keyStr)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("invalid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.RelayKey.Invalid {
|
|
||||||
hrp, key, err := common.DecodeRelayKey(f.Key)
|
|
||||||
require.EqualError(t, err, f.ExpectedError)
|
|
||||||
require.Empty(t, hrp)
|
|
||||||
require.Nil(t, key)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUrlEncoding(t *testing.T) {
|
|
||||||
fixtures := struct {
|
|
||||||
Url struct {
|
|
||||||
Valid []struct {
|
|
||||||
Url string `json:"url"`
|
|
||||||
ExpectedPubkey string `json:"expectedPubkey"`
|
|
||||||
ExpectedRelays []string `json:"expectedRelays"`
|
|
||||||
} `json:"valid"`
|
|
||||||
Invalid []struct {
|
|
||||||
Url string `json:"url"`
|
|
||||||
ExpectedError string `json:"expectedError"`
|
|
||||||
} `json:"invalid"`
|
|
||||||
} `json:"url"`
|
|
||||||
}{}
|
|
||||||
err := json.Unmarshal(f, &fixtures)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
t.Run("valid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.Url.Valid {
|
|
||||||
pubkey, relays, err := common.DecodeUrl(f.Url)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEmpty(t, pubkey)
|
|
||||||
require.NotNil(t, relays)
|
|
||||||
|
|
||||||
require.Equal(t, f.ExpectedPubkey, pubkey)
|
|
||||||
require.Exactly(t, relays, f.ExpectedRelays)
|
|
||||||
|
|
||||||
url, err := common.EncodeUrl(pubkey, relays...)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, f.Url, url)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("invalid", func(t *testing.T) {
|
|
||||||
for _, f := range fixtures.Url.Invalid {
|
|
||||||
pubkey, relays, err := common.DecodeUrl(f.Url)
|
|
||||||
require.Error(t, err)
|
|
||||||
require.Contains(t, err.Error(), f.ExpectedError)
|
|
||||||
require.Empty(t, pubkey)
|
|
||||||
require.Nil(t, relays)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,38 +1,10 @@
|
|||||||
{
|
{
|
||||||
"secretKey": {
|
|
||||||
"valid": [
|
|
||||||
{
|
|
||||||
"key": "asec1n9grggypds323l5fkw4t6kpf6trz26an8wv44qqr8ctp4t3dp52q5zkzz4",
|
|
||||||
"expected": "99503420816c22a8fe89b3aabd5829d2c6256bb33b995a80033e161aae2d0d14"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"invalid": [
|
|
||||||
{
|
|
||||||
"key": "wrongprefix1c02kjhr4egxvh0ajua0ylv9vl3kyegxmrl2djh4pn63m948ecs4qchx9zx",
|
|
||||||
"expectedError": "invalid prefix"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"publicKey": {
|
|
||||||
"valid": [
|
|
||||||
{
|
|
||||||
"key": "apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x",
|
|
||||||
"expected": "0218d5ca8b58797b7dbd65c075dd7ba7784b3f38ab71b1a5a8e3f94ba0257654a6"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"invalid": [
|
|
||||||
{
|
|
||||||
"key": "wrongprefix1q0yn8cskp7lv0lxq3unfynmju68smh69lu90yyv37wzetu6upp76vg4ef6n",
|
|
||||||
"expectedError": "invalid prefix"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"address": {
|
"address": {
|
||||||
"valid": [
|
"valid": [
|
||||||
{
|
{
|
||||||
"addr": "ark1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22vqa7mdkrrulzu48law4zzvzz8k59hul0ayl2urt905we5wf6gee68sfrfj35",
|
"addr": "ark1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22vqa7mdkrrulzu48law4zzvzz8k59hul0ayl2urt905we5wf6gee68sfrfj35",
|
||||||
"expectedUserKey": "apub1qwldkmp3703w2nl7h23pxpprm2zm70h7j04wp4jh68v68yayvuarcc28uv5",
|
"expectedUserKey": "03bedb6c31f3e2e54ffebaa2130423da85bf3efe93eae0d657d1d9a393a4673a3c",
|
||||||
"expectedAspKey": "apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x"
|
"expectedAspKey": "0218d5ca8b58797b7dbd65c075dd7ba7784b3f38ab71b1a5a8e3f94ba0257654a6"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"invalid": [
|
"invalid": [
|
||||||
@@ -41,45 +13,5 @@
|
|||||||
"expectedError": "invalid prefix"
|
"expectedError": "invalid prefix"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
|
||||||
"relayKey": {
|
|
||||||
"valid": [
|
|
||||||
{
|
|
||||||
"key": "arelay1qt6f8p7h5f6tm7fv2z5wg92sz92rn9desfhd5733se4lkrptqtdrq65987l",
|
|
||||||
"expected": "02f49387d7a274bdf92c50a8e4155011543995b9826eda7a31866bfb0c2b02da30"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"invalid": [
|
|
||||||
{
|
|
||||||
"key": "wrongprefix1q2g64uehct5zdkhdull6ultevfmuu62nzwucec6q8su85eqpezxdvsf2mfd",
|
|
||||||
"expectedError": "invalid prefix"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"url": {
|
|
||||||
"valid": [
|
|
||||||
{
|
|
||||||
"url": "ark://apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x?relays=arelay1qt6f8p7h5f6tm7fv2z5wg92sz92rn9desfhd5733se4lkrptqtdrq65987l-arelay1qt6f8p7h5f6tm7fv2z5wg92sz92rn9desfhd5733se4lkrptqtdrq65987l",
|
|
||||||
"expectedPubkey": "apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x",
|
|
||||||
"expectedRelays": [
|
|
||||||
"arelay1qt6f8p7h5f6tm7fv2z5wg92sz92rn9desfhd5733se4lkrptqtdrq65987l",
|
|
||||||
"arelay1qt6f8p7h5f6tm7fv2z5wg92sz92rn9desfhd5733se4lkrptqtdrq65987l"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"invalid": [
|
|
||||||
{
|
|
||||||
"url": "wrong://apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x?relays=arelay1qt6f8p7h5f6tm7fv2z5wg92sz92rn9desfhd5733se4lkrptqtdrq65987l-arelay1qt6f8p7h5f6tm7fv2z5wg92sz92rn9desfhd5733se4lkrptqtdrq65987l",
|
|
||||||
"expectedError": "invalid proto"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"url": "ark://asec1n9grggypds323l5fkw4t6kpf6trz26an8wv44qqr8ctp4t3dp52qun9kjh",
|
|
||||||
"expectedError": "invalid public key"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"url": "ark://apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x?relays=apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x",
|
|
||||||
"expectedError": "invalid relay public key"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,24 +2,15 @@ package common
|
|||||||
|
|
||||||
type Network struct {
|
type Network struct {
|
||||||
Name string
|
Name string
|
||||||
SecKey string
|
|
||||||
PubKey string
|
|
||||||
RelayKey string
|
|
||||||
Addr string
|
Addr string
|
||||||
}
|
}
|
||||||
|
|
||||||
var MainNet = Network{
|
var MainNet = Network{
|
||||||
Name: "mainnet",
|
Name: "mainnet",
|
||||||
SecKey: "asec",
|
|
||||||
PubKey: "apub",
|
|
||||||
RelayKey: "arelay",
|
|
||||||
Addr: "ark",
|
Addr: "ark",
|
||||||
}
|
}
|
||||||
|
|
||||||
var TestNet = Network{
|
var TestNet = Network{
|
||||||
Name: "testnet",
|
Name: "testnet",
|
||||||
SecKey: "tasec",
|
|
||||||
PubKey: "tapub",
|
|
||||||
RelayKey: "tarelay",
|
|
||||||
Addr: "tark",
|
Addr: "tark",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,11 +219,7 @@ func (s *service) GetRoundByTxid(ctx context.Context, poolTxid string) (*domain.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) GetPubkey(ctx context.Context) (string, error) {
|
func (s *service) GetPubkey(ctx context.Context) (string, error) {
|
||||||
pubkey, err := common.EncodePubKey(s.network.PubKey, s.pubkey)
|
return hex.EncodeToString(s.pubkey.SerializeCompressed()), nil
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return pubkey, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) start() {
|
func (s *service) start() {
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ark-network/ark/common"
|
|
||||||
"github.com/ark-network/ark/common/tree"
|
"github.com/ark-network/ark/common/tree"
|
||||||
"github.com/ark-network/ark/internal/core/domain"
|
"github.com/ark-network/ark/internal/core/domain"
|
||||||
"github.com/ark-network/ark/internal/core/ports"
|
"github.com/ark-network/ark/internal/core/ports"
|
||||||
@@ -21,7 +20,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
testingKey = "apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x"
|
testingKey = "0218d5ca8b58797b7dbd65c075dd7ba7784b3f38ab71b1a5a8e3f94ba0257654a6"
|
||||||
minRelayFee = uint64(30)
|
minRelayFee = uint64(30)
|
||||||
roundLifetime = int64(1209344)
|
roundLifetime = int64(1209344)
|
||||||
)
|
)
|
||||||
@@ -38,7 +37,8 @@ func TestMain(m *testing.M) {
|
|||||||
wallet.On("SelectUtxos", mock.Anything, mock.Anything, mock.Anything).
|
wallet.On("SelectUtxos", mock.Anything, mock.Anything, mock.Anything).
|
||||||
Return(randomInput, uint64(0), nil)
|
Return(randomInput, uint64(0), nil)
|
||||||
|
|
||||||
_, pubkey, _ = common.DecodePubKey(testingKey)
|
pubkeyBytes, _ := hex.DecodeString(testingKey)
|
||||||
|
pubkey, _ = secp256k1.ParsePubKey(pubkeyBytes)
|
||||||
|
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ark-network/ark/common"
|
|
||||||
"github.com/ark-network/ark/internal/core/domain"
|
"github.com/ark-network/ark/internal/core/domain"
|
||||||
"github.com/ark-network/ark/internal/core/ports"
|
"github.com/ark-network/ark/internal/core/ports"
|
||||||
txbuilder "github.com/ark-network/ark/internal/infrastructure/tx-builder/dummy"
|
txbuilder "github.com/ark-network/ark/internal/infrastructure/tx-builder/dummy"
|
||||||
@@ -18,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
testingKey = "apub1qgvdtj5ttpuhkldavhq8thtm5auyk0ec4dcmrfdgu0u5hgp9we22v3hrs4x"
|
testingKey = "0218d5ca8b58797b7dbd65c075dd7ba7784b3f38ab71b1a5a8e3f94ba0257654a6"
|
||||||
fakePoolTx = "cHNldP8BAgQCAAAAAQQBAQEFAQMBBgEDAfsEAgAAAAABDiDk7dXxh4KQzgLO8i1ABtaLCe4aPL12GVhN1E9zM1ePLwEPBAAAAAABEAT/////AAEDCOgDAAAAAAAAAQQWABSNnpy01UJqd99eTg2M1IpdKId11gf8BHBzZXQCICWyUQcOKcoZBDzzPM1zJOLdqwPsxK4LXnfE/A5c9slaB/wEcHNldAgEAAAAAAABAwh4BQAAAAAAAAEEFgAUjZ6ctNVCanffXk4NjNSKXSiHddYH/ARwc2V0AiAlslEHDinKGQQ88zzNcyTi3asD7MSuC153xPwOXPbJWgf8BHBzZXQIBAAAAAAAAQMI9AEAAAAAAAABBAAH/ARwc2V0AiAlslEHDinKGQQ88zzNcyTi3asD7MSuC153xPwOXPbJWgf8BHBzZXQIBAAAAAAA"
|
fakePoolTx = "cHNldP8BAgQCAAAAAQQBAQEFAQMBBgEDAfsEAgAAAAABDiDk7dXxh4KQzgLO8i1ABtaLCe4aPL12GVhN1E9zM1ePLwEPBAAAAAABEAT/////AAEDCOgDAAAAAAAAAQQWABSNnpy01UJqd99eTg2M1IpdKId11gf8BHBzZXQCICWyUQcOKcoZBDzzPM1zJOLdqwPsxK4LXnfE/A5c9slaB/wEcHNldAgEAAAAAAABAwh4BQAAAAAAAAEEFgAUjZ6ctNVCanffXk4NjNSKXSiHddYH/ARwc2V0AiAlslEHDinKGQQ88zzNcyTi3asD7MSuC153xPwOXPbJWgf8BHBzZXQIBAAAAAAAAQMI9AEAAAAAAAABBAAH/ARwc2V0AiAlslEHDinKGQQ88zzNcyTi3asD7MSuC153xPwOXPbJWgf8BHBzZXQIBAAAAAAA"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -238,13 +237,11 @@ func TestBuildCongestionTree(t *testing.T) {
|
|||||||
expectedLeavesNum: 6,
|
expectedLeavesNum: 6,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
pubkeyBytes, _ := hex.DecodeString(testingKey)
|
||||||
_, key, err := common.DecodePubKey(testingKey)
|
pubkey, _ := secp256k1.ParsePubKey(pubkeyBytes)
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, key)
|
|
||||||
|
|
||||||
for _, f := range fixtures {
|
for _, f := range fixtures {
|
||||||
poolTx, tree, err := builder.BuildPoolTx(key, f.payments, 30)
|
poolTx, tree, err := builder.BuildPoolTx(pubkey, f.payments, 30)
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, f.expectedNodesNum, tree.NumberOfNodes())
|
require.Equal(t, f.expectedNodesNum, tree.NumberOfNodes())
|
||||||
@@ -352,13 +349,12 @@ func TestBuildForfeitTxs(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, key, err := common.DecodePubKey(testingKey)
|
pubkeyBytes, _ := hex.DecodeString(testingKey)
|
||||||
require.NoError(t, err)
|
pubkey, _ := secp256k1.ParsePubKey(pubkeyBytes)
|
||||||
require.NotNil(t, key)
|
|
||||||
|
|
||||||
for _, f := range fixtures {
|
for _, f := range fixtures {
|
||||||
connectors, forfeitTxs, err := builder.BuildForfeitTxs(
|
connectors, forfeitTxs, err := builder.BuildForfeitTxs(
|
||||||
key, fakePoolTx, f.payments,
|
pubkey, fakePoolTx, f.payments,
|
||||||
)
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, connectors, f.expectedNumOfConnectors)
|
require.Len(t, connectors, f.expectedNumOfConnectors)
|
||||||
|
|||||||
Reference in New Issue
Block a user