forked from polkascan/py-sr25519-bindings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
91 lines (72 loc) · 3.06 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Python SR25519 Bindings
#
# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import bip39
import sr25519
class MyTestCase(unittest.TestCase):
message = b"test"
seed = bip39.bip39_to_mini_secret('daughter song common combine misery cotton audit morning stuff weasel flee field','')
chain_code = bytes.fromhex('7eadeb0f985ffcab9e50f25a19c1e4c8c2f4cd742049fc35e07c684040057e9a')
child_index = b"\x01\x02\x03\x04"
def test_sign_and_verify_message(self):
# Get private and public key from seed
public_key, private_key = sr25519.pair_from_seed(bytes(self.seed))
# Generate signature
signature = sr25519.sign(
(public_key, private_key),
self.message
)
# Verify message with signature
self.assertTrue(sr25519.verify(signature, self.message, public_key))
def test_derive_soft(self):
# Get private and public key from seed
public_key, private_key = sr25519.pair_from_seed(bytes(self.seed))
# Private derivation
child_chain_priv, child_pubkey_priv, child_privkey = sr25519.derive_keypair(
(self.chain_code, public_key, private_key),
self.child_index
)
# Public derivation
child_chain_pub, child_pubkey_pub = sr25519.derive_pubkey(
(self.chain_code, public_key),
self.child_index
)
# Assert that the chain code and public key are the same regardless of
# derivation method
self.assertEqual(child_chain_priv, child_chain_pub)
self.assertEqual(child_pubkey_priv, child_pubkey_pub)
# Test that signatures with the derived private key are valid
signature = sr25519.sign(
(child_pubkey_priv, child_privkey),
self.message
)
self.assertTrue(sr25519.verify(signature, self.message, child_pubkey_pub))
def test_derive_hard(self):
# Get private and public key from seed
public_key, private_key = sr25519.pair_from_seed(bytes(self.seed))
# Private derivation
_, child_pubkey, child_privkey = sr25519.hard_derive_keypair(
(self.chain_code, public_key, private_key),
self.child_index
)
# Test that signatures with the derived private key are valid
signature = sr25519.sign(
(child_pubkey, child_privkey),
self.message
)
self.assertTrue(sr25519.verify(signature, self.message, child_pubkey))
if __name__ == '__main__':
unittest.main()