-
Notifications
You must be signed in to change notification settings - Fork 16
/
Secp256k1KeyManagerTest.java
78 lines (65 loc) · 3.14 KB
/
Secp256k1KeyManagerTest.java
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
package irita.sdk;
import irita.sdk.key.AlgoEnum;
import irita.sdk.key.KeyInfo;
import irita.sdk.key.KeyManager;
import irita.sdk.key.KeyManagerFactory;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Secp256k1KeyManagerTest {
@Test
public void recoverFromMnemonic() {
String mnemonic = "opera vivid pride shallow brick crew found resist decade neck expect apple chalk belt sick author know try tank detail tree impact hand best";
KeyManager km = KeyManagerFactory.createKeyManager(AlgoEnum.SECP256K1);
km.recover(mnemonic);
KeyInfo keyInfo = km.getCurrentKeyInfo();
assertEquals("iaa1fwgw9ej3yzq6frn8rq7pswfhkdcsq8szsff7ka", keyInfo.getAddress());
assertEquals("3c49175daf981965679bf88d2690e22144424e16c84e9d397ddb58b63603eeec", keyInfo.getPrivKey().toString(16));
}
@Test
public void recoverFromPrivateKey() {
BigInteger privKey = new BigInteger("3c49175daf981965679bf88d2690e22144424e16c84e9d397ddb58b63603eeec", 16);
KeyManager km = KeyManagerFactory.createKeyManager(AlgoEnum.SECP256K1);
km.recover(privKey);
KeyInfo keyInfo = km.getCurrentKeyInfo();
assertEquals("iaa1fwgw9ej3yzq6frn8rq7pswfhkdcsq8szsff7ka", keyInfo.getAddress());
assertEquals("3c49175daf981965679bf88d2690e22144424e16c84e9d397ddb58b63603eeec", keyInfo.getPrivKey().toString(16));
}
@Test
public void export() {
BigInteger privKey = new BigInteger("3c49175daf981965679bf88d2690e22144424e16c84e9d397ddb58b63603eeec", 16);
KeyManager km = KeyManagerFactory.createKeyManager(AlgoEnum.SECP256K1);
km.recover(privKey);
String keystore = km.export("1234567890");
System.out.println(keystore);
}
@Test
@Tag("read keystore from str")
public void recoverFromKeystore() {
String keystore = "-----BEGIN TENDERMINT PRIVATE KEY-----\n" +
"salt: 584CE7CC551DC2F12ED7BE2FC27FB73A\n" +
"type: secp256k1\n" +
"kdf: bcrypt\n" +
"\n" +
"+s3/pD/aNSZ/7nxQBu+80Lal3qGM8nec1XOdNiZFXTbyWt3aordqmTsLuRpE4lpj\n" +
"JK6oeN3acJYtnjtCegZ+j6ROqg42jfIsZrgrpV0=\n" +
"=UwFf\n" +
"-----END TENDERMINT PRIVATE KEY-----";
InputStream input = new ByteArrayInputStream(keystore.getBytes(StandardCharsets.UTF_8));
KeyManager km = KeyManagerFactory.createKeyManager(AlgoEnum.SECP256K1);
km.recover(input, "1234567890");
KeyInfo keyInfo = km.getCurrentKeyInfo();
assertEquals("iaa1fwgw9ej3yzq6frn8rq7pswfhkdcsq8szsff7ka", keyInfo.getAddress());
assertEquals("3c49175daf981965679bf88d2690e22144424e16c84e9d397ddb58b63603eeec", keyInfo.getPrivKey().toString(16));
}
@Test
public void addNewKm() throws Exception {
KeyManager km = KeyManagerFactory.createDefault();
String mnemonic = km.add();
System.out.println(mnemonic);
}
}