Skip to content

Commit 305d63b

Browse files
authored
Add JSON unmarshal for subkey inspect (#98)
* Revert "Add ExtrinsicSignatureV5 (#68)" This reverts commit 461cf42. * Add JSON unmarshal for subkey inspect
1 parent fee0d0e commit 305d63b

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ This client is modelled after [polkadot-js/api](https://github.com/polkadot-js/a
1313

1414
This package is feature complete, but it is relatively new and might still contain bugs. We advice to use it with caution in production. It comes without any warranties, please refer to LICENCE for details.
1515

16+
## Requirements
17+
Substrate Key Management requires `subkey` to be present in your PATH: https://substrate.dev/docs/en/knowledgebase/integrate/subkey
18+
19+
The `subkey` recommended version: https://github.com/paritytech/substrate/releases/tag/v2.0.0-rc6
20+
1621
## Documentation & Usage Examples
1722

1823
Please refer to https://godoc.org/github.com/centrifuge/go-substrate-rpc-client

signature/signature.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package signature
1818

1919
import (
2020
"encoding/hex"
21+
"encoding/json"
2122
"fmt"
2223
"os"
2324
"os/exec"
24-
"regexp"
2525
"strings"
2626

2727
"golang.org/x/crypto/blake2b"
@@ -38,9 +38,14 @@ type KeyringPair struct {
3838
PublicKey []byte
3939
}
4040

41-
var rePubKey = regexp.MustCompile(`Public key \(hex\): 0x([a-f0-9]*)\n`)
42-
var reAddressOld = regexp.MustCompile(`Address \(SS58\): ([a-zA-Z0-9]*)\n`)
43-
var reAddressNew = regexp.MustCompile(`SS58 Address:\s+([a-zA-Z0-9]*)\n`)
41+
// InspectKeyInfo type is used as target from `subkey` inspect JSON output
42+
type InspectKeyInfo struct {
43+
AccountID string `json:"accountId"`
44+
PublicKey string `json:"publicKey"`
45+
SecretPhrase string `json:"secretPhrase"`
46+
SecretSeed string `json:"secretSeed"`
47+
SS58Address string `json:"ss58Address"`
48+
}
4449

4550
// KeyringPairFromSecret creates KeyPair based on seed/phrase and network
4651
// Leave network empty for default behavior
@@ -49,7 +54,7 @@ func KeyringPairFromSecret(seedOrPhrase, network string) (KeyringPair, error) {
4954
if network != "" {
5055
args = []string{"--network", network}
5156
}
52-
args = append([]string{"inspect", seedOrPhrase}, args...)
57+
args = append([]string{"inspect", "--output-type", "Json", seedOrPhrase}, args...)
5358

5459
// use "subkey" command for creation of public key and address
5560
cmd := exec.Command(subkeyCmd, args...)
@@ -64,29 +69,21 @@ func KeyringPairFromSecret(seedOrPhrase, network string) (KeyringPair, error) {
6469
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret: invalid phrase/URI given")
6570
}
6671

67-
// find the pub key
68-
resPk := rePubKey.FindStringSubmatch(string(out))
69-
if len(resPk) != 2 {
70-
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, pubkey not found in output: %v", resPk)
71-
}
72-
pk, err := hex.DecodeString(resPk[1])
72+
var keyInfo InspectKeyInfo
73+
err = json.Unmarshal(out, &keyInfo)
7374
if err != nil {
74-
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, could not hex decode pubkey: %v",
75-
resPk[1])
75+
return KeyringPair{}, fmt.Errorf("failed to deserialize key info JSON output: %v", err.Error())
7676
}
7777

78-
// find the address
79-
addr := reAddressNew.FindStringSubmatch(string(out))
80-
if len(addr) != 2 {
81-
addr = reAddressOld.FindStringSubmatch(string(out))
82-
}
83-
if len(addr) != 2 {
84-
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, address not found in output: %v", addr)
78+
pk, err := hex.DecodeString(strings.Replace(keyInfo.PublicKey, "0x", "", 1))
79+
if err != nil {
80+
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, could not hex decode pubkey: "+
81+
"%v with error: %v", keyInfo.PublicKey, err.Error())
8582
}
8683

8784
return KeyringPair{
8885
URI: seedOrPhrase,
89-
Address: addr[1],
86+
Address: keyInfo.SS58Address,
9087
PublicKey: pk,
9188
}, nil
9289
}

0 commit comments

Comments
 (0)