Skip to content

Commit 136763b

Browse files
authored
Merge pull request #46 from rocklabs-io/mnemonic
Add identity from mnemonic and clean code
2 parents 4220bc4 + 72ef617 commit 136763b

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ build/
88
*.egg-info
99

1010
.tox/
11+
.DS_Store
1112

1213
.antlr
1314
dist/

ic/agent.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ def sign_request(req, iden):
1919
}
2020
return req_id, cbor2.dumps(envelop)
2121

22-
# According to did, get the method returned param type
23-
def getType(method:str):
24-
if method == 'totalSupply':
25-
return Types.Nat
26-
elif method == 'name':
27-
return Types.Text
28-
elif method == 'balanceOf':
29-
return Types.Nat
30-
elif method == 'transfer':
31-
return Types.Variant({'ok': Types.Nat, 'err': Types.Variant})
32-
else:
33-
# pass
34-
return Types.Nat
35-
3622
class Agent:
3723
def __init__(self, identity, client, nonce_factory=None, ingress_expiry=300, root_key=IC_ROOT_KEY):
3824
self.identity = identity
@@ -113,6 +99,10 @@ def read_state_raw(self, canister_id, paths):
11399
}
114100
_, data = sign_request(req, self.identity)
115101
ret = self.read_state_endpoint(canister_id, data)
102+
if ret == b'Invalid path requested.':
103+
raise ValueError('Invalid path requested!')
104+
elif ret == b'Could not parse body as read request: invalid type: byte array, expected a sequence':
105+
raise ValueError('Could not parse body as read request: invalid type: byte array, expected a sequence')
116106
d = cbor2.loads(ret)
117107
cert = cbor2.loads(d['certificate'])
118108
return cert

ic/certificate.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ def flatten_forks(t):
7979
if t[0] == NodeId.Empty.value:
8080
return []
8181
elif t[0] == NodeId.Fork.value:
82-
return flatten_forks(t[1]) + (flatten_forks(t[2]))
82+
val1 = flatten_forks(t[1])
83+
val2 = flatten_forks(t[2])
84+
val1.extend(val2)
85+
return val1
8386
else:
8487
return [t]
8588

@@ -96,4 +99,4 @@ def find_label(l, trees):
9699
tree = [1, [4, b'W\xb4\x1b\x00\xc9x\xc0\xcb\\\xf4\xb6\xa1\xbbE\\\x9fr\xe2\x1a8\xd2bE\x14\x11\xab:\xb5\x1b`\x98\x9d'], [1, [4, b'\xac>_\x80\xeb.$\x9c\x00\xbc\x12\xce&!^\xa8,i\x08\xaeH\x8e\x9ce9\x87\xbahGPo\xe6'], [2, b'time', [3, b'\xd2\xac\xd3\x8a\xfc\xa0\xd0\xe0\x16']]]]
97100
tree2 = [1, [4, b'5J\xe2\x98A\x8d5\xc8\xe6\x94V\xc9\x90\x87\x00\xc9:\xe1\xb3i\x91fS\xc0udD\x19mQ\x1c\x85'], [1, [4, b'\xac>_\x80\xeb.$\x9c\x00\xbc\x12\xce&!^\xa8,i\x08\xaeH\x8e\x9ce9\x87\xbahGPo\xe6'], [2, b'time', [3, b'\xe7\xfc\xcf\x90\x87\x85\xd0\xe0\x16']]]]
98101
path = b'time'
99-
print(lookup_path([path], tree))
102+
print(lookup_path([path], tree))

ic/identity.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ecdsa.curves import Ed25519, SECP256k1
44
from .principal import Principal
55
import ecdsa
6+
from mnemonic import Mnemonic
67

78
class Identity:
89
def __init__(self, privkey = "", type = "ed25519", anonymous = False):
@@ -32,6 +33,14 @@ def __init__(self, privkey = "", type = "ed25519", anonymous = False):
3233
else:
3334
raise 'unsupported identity type'
3435

36+
@staticmethod
37+
def from_seed(mnemonic: str):
38+
mnemo = Mnemonic('english')
39+
seed = mnemo.to_seed(mnemonic).hex()
40+
privkey = seed[:64]
41+
type = "ed25519"
42+
return Identity(privkey=privkey, type=type)
43+
3544
@staticmethod
3645
def from_pem(pem: str):
3746
key = ecdsa.SigningKey.from_pem(pem)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
'cbor2>=5.4.2',
2323
'leb128>=1.0.4',
2424
'waiter>=1.2',
25-
'antlr4-python3-runtime==4.9.3'
25+
'antlr4-python3-runtime==4.9.3',
26+
'mnemonic==0.20'
2627
],
2728
py_modules = ['ic'],
2829
package_dir = { 'ic': "ic" },

tests/test_identity.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ def test_ed25519_frompem(self):
2222
assert iden.pubkey == 'ab3b98863398672d6ac76afcb0a177a40285cfa5846e0740ebe838d917a15992'
2323

2424
def test_secp256k1_frompem(self):
25-
pass
25+
pass
26+
27+
def test_ed25519_from_seed(self):
28+
mnemonic = 'fence dragon soft spoon embrace bronze regular hawk more remind detect slam'
29+
iden = Identity.from_seed(mnemonic)
30+
assert iden.key_type == 'ed25519'
31+
assert iden.privkey == '97cc884647e7e0ef58c36b57448269ba6a123521a7f234fa5fdc5816d824ef50'

0 commit comments

Comments
 (0)