@keplr-wallet/crypto 0.9.9 → 0.9.12-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/key.d.ts +4 -0
- package/build/key.js +35 -3
- package/build/key.js.map +1 -1
- package/build/key.spec.js +36 -9
- package/build/key.spec.js.map +1 -1
- package/package.json +2 -3
- package/src/key.spec.ts +38 -7
- package/src/key.ts +54 -3
package/build/key.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { ec } from "elliptic";
|
|
1
2
|
export declare class PrivKeySecp256k1 {
|
|
2
3
|
protected readonly privKey: Uint8Array;
|
|
4
|
+
static generateRandomKey(): PrivKeySecp256k1;
|
|
3
5
|
constructor(privKey: Uint8Array);
|
|
4
6
|
toBytes(): Uint8Array;
|
|
5
7
|
getPubKey(): PubKeySecp256k1;
|
|
@@ -10,4 +12,6 @@ export declare class PubKeySecp256k1 {
|
|
|
10
12
|
constructor(pubKey: Uint8Array);
|
|
11
13
|
toBytes(): Uint8Array;
|
|
12
14
|
getAddress(): Uint8Array;
|
|
15
|
+
toKeyPair(): ec.KeyPair;
|
|
16
|
+
verify(msg: Uint8Array, signature: Uint8Array): boolean;
|
|
13
17
|
}
|
package/build/key.js
CHANGED
|
@@ -4,23 +4,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.PubKeySecp256k1 = exports.PrivKeySecp256k1 = void 0;
|
|
7
|
-
const elliptic_1 =
|
|
7
|
+
const elliptic_1 = require("elliptic");
|
|
8
8
|
const crypto_js_1 = __importDefault(require("crypto-js"));
|
|
9
9
|
const buffer_1 = require("buffer/");
|
|
10
10
|
class PrivKeySecp256k1 {
|
|
11
11
|
constructor(privKey) {
|
|
12
12
|
this.privKey = privKey;
|
|
13
13
|
}
|
|
14
|
+
static generateRandomKey() {
|
|
15
|
+
const secp256k1 = new elliptic_1.ec("secp256k1");
|
|
16
|
+
return new PrivKeySecp256k1(buffer_1.Buffer.from(secp256k1.genKeyPair().getPrivate().toArray()));
|
|
17
|
+
}
|
|
14
18
|
toBytes() {
|
|
15
19
|
return new Uint8Array(this.privKey);
|
|
16
20
|
}
|
|
17
21
|
getPubKey() {
|
|
18
|
-
const secp256k1 = new elliptic_1.
|
|
22
|
+
const secp256k1 = new elliptic_1.ec("secp256k1");
|
|
19
23
|
const key = secp256k1.keyFromPrivate(this.privKey);
|
|
20
24
|
return new PubKeySecp256k1(new Uint8Array(key.getPublic().encodeCompressed("array")));
|
|
21
25
|
}
|
|
22
26
|
sign(msg) {
|
|
23
|
-
const secp256k1 = new elliptic_1.
|
|
27
|
+
const secp256k1 = new elliptic_1.ec("secp256k1");
|
|
24
28
|
const key = secp256k1.keyFromPrivate(this.privKey);
|
|
25
29
|
const hash = crypto_js_1.default.SHA256(crypto_js_1.default.lib.WordArray.create(msg)).toString();
|
|
26
30
|
const signature = key.sign(buffer_1.Buffer.from(hash, "hex"), {
|
|
@@ -42,6 +46,34 @@ class PubKeySecp256k1 {
|
|
|
42
46
|
hash = crypto_js_1.default.RIPEMD160(crypto_js_1.default.enc.Hex.parse(hash)).toString();
|
|
43
47
|
return new Uint8Array(buffer_1.Buffer.from(hash, "hex"));
|
|
44
48
|
}
|
|
49
|
+
toKeyPair() {
|
|
50
|
+
const secp256k1 = new elliptic_1.ec("secp256k1");
|
|
51
|
+
return secp256k1.keyFromPublic(buffer_1.Buffer.from(this.pubKey).toString("hex"), "hex");
|
|
52
|
+
}
|
|
53
|
+
verify(msg, signature) {
|
|
54
|
+
const hash = crypto_js_1.default.SHA256(crypto_js_1.default.lib.WordArray.create(msg)).toString();
|
|
55
|
+
const secp256k1 = new elliptic_1.ec("secp256k1");
|
|
56
|
+
let r = signature.slice(0, 32);
|
|
57
|
+
let s = signature.slice(32);
|
|
58
|
+
const rIsNegative = r[0] >= 0x80;
|
|
59
|
+
const sIsNegative = s[0] >= 0x80;
|
|
60
|
+
if (rIsNegative) {
|
|
61
|
+
r = new Uint8Array([0, ...r]);
|
|
62
|
+
}
|
|
63
|
+
if (sIsNegative) {
|
|
64
|
+
s = new Uint8Array([0, ...s]);
|
|
65
|
+
}
|
|
66
|
+
// Der encoding
|
|
67
|
+
const derData = new Uint8Array([
|
|
68
|
+
0x02,
|
|
69
|
+
r.length,
|
|
70
|
+
...r,
|
|
71
|
+
0x02,
|
|
72
|
+
s.length,
|
|
73
|
+
...s,
|
|
74
|
+
]);
|
|
75
|
+
return secp256k1.verify(buffer_1.Buffer.from(hash, "hex"), new Uint8Array([0x30, derData.length, ...derData]), this.toKeyPair());
|
|
76
|
+
}
|
|
45
77
|
}
|
|
46
78
|
exports.PubKeySecp256k1 = PubKeySecp256k1;
|
|
47
79
|
//# sourceMappingURL=key.js.map
|
package/build/key.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key.js","sourceRoot":"","sources":["../src/key.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"key.js","sourceRoot":"","sources":["../src/key.ts"],"names":[],"mappings":";;;;;;AAAA,uCAA8B;AAC9B,0DAAiC;AAEjC,oCAAiC;AAEjC,MAAa,gBAAgB;IAS3B,YAA+B,OAAmB;QAAnB,YAAO,GAAP,OAAO,CAAY;IAAG,CAAC;IARtD,MAAM,CAAC,iBAAiB;QACtB,MAAM,SAAS,GAAG,IAAI,aAAE,CAAC,WAAW,CAAC,CAAC;QAEtC,OAAO,IAAI,gBAAgB,CACzB,eAAM,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,UAAU,EAAE,CAAC,OAAO,EAAE,CAAC,CAC3D,CAAC;IACJ,CAAC;IAID,OAAO;QACL,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;IAED,SAAS;QACP,MAAM,SAAS,GAAG,IAAI,aAAE,CAAC,WAAW,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AAM,OAAO,GAAG,IAAI,UAAU,CAAC;YAC7B,IAAI;YACJ,CAAC,CAAC,MAAM;YACR,GAAG,CAAC;YACJ,IAAI;YACJ,CAAC,CAAC,MAAM;YACR,GAAG,CAAC;SACL,CAAC,CAAC;QACH,OAAO,SAAS,CAAC,MAAM,CACrB,eAAM,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,EACxB,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC,EAClD,IAAI,CAAC,SAAS,EAAE,CACjB,CAAC;IACJ,CAAC;CACF;AA1DD,0CA0DC"}
|
package/build/key.spec.js
CHANGED
|
@@ -1,20 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const assert_1 = __importDefault(require("assert"));
|
|
7
3
|
const mnemonic_1 = require("./mnemonic");
|
|
8
4
|
const key_1 = require("./key");
|
|
9
|
-
const cosmos_1 = require("@keplr-wallet/cosmos");
|
|
10
5
|
describe("Test priv key", () => {
|
|
11
|
-
it("priv key should generate the valid
|
|
6
|
+
it("priv key should generate the valid pub key", () => {
|
|
12
7
|
const mnemonic = "celery husband drama unaware blue empower jelly twist program say prepare page";
|
|
13
|
-
const expectedAddress = "cosmos1d2kh2xaen7c0zv3h7qnmghhwhsmmassqlmr2nv";
|
|
14
8
|
const privKey = new key_1.PrivKeySecp256k1(mnemonic_1.Mnemonic.generateWalletFromMnemonic(mnemonic));
|
|
15
9
|
const pubKey = privKey.getPubKey();
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
expect(pubKey.toBytes()).toStrictEqual(new Uint8Array([
|
|
11
|
+
2,
|
|
12
|
+
57,
|
|
13
|
+
75,
|
|
14
|
+
197,
|
|
15
|
+
54,
|
|
16
|
+
51,
|
|
17
|
+
54,
|
|
18
|
+
106,
|
|
19
|
+
42,
|
|
20
|
+
185,
|
|
21
|
+
181,
|
|
22
|
+
214,
|
|
23
|
+
151,
|
|
24
|
+
169,
|
|
25
|
+
76,
|
|
26
|
+
140,
|
|
27
|
+
1,
|
|
28
|
+
33,
|
|
29
|
+
204,
|
|
30
|
+
94,
|
|
31
|
+
63,
|
|
32
|
+
13,
|
|
33
|
+
85,
|
|
34
|
+
74,
|
|
35
|
+
99,
|
|
36
|
+
22,
|
|
37
|
+
126,
|
|
38
|
+
219,
|
|
39
|
+
49,
|
|
40
|
+
140,
|
|
41
|
+
234,
|
|
42
|
+
232,
|
|
43
|
+
188,
|
|
44
|
+
]));
|
|
18
45
|
});
|
|
19
46
|
});
|
|
20
47
|
//# sourceMappingURL=key.spec.js.map
|
package/build/key.spec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key.spec.js","sourceRoot":"","sources":["../src/key.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"key.spec.js","sourceRoot":"","sources":["../src/key.spec.ts"],"names":[],"mappings":";;AAAA,yCAAsC;AACtC,+BAAyC;AAEzC,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QACpD,MAAM,QAAQ,GACZ,gFAAgF,CAAC;QAEnF,MAAM,OAAO,GAAG,IAAI,sBAAgB,CAClC,mBAAQ,CAAC,0BAA0B,CAAC,QAAQ,CAAC,CAC9C,CAAC;QACF,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;QACnC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,aAAa,CACpC,IAAI,UAAU,CAAC;YACb,CAAC;YACD,EAAE;YACF,EAAE;YACF,GAAG;YACH,EAAE;YACF,EAAE;YACF,EAAE;YACF,GAAG;YACH,EAAE;YACF,GAAG;YACH,GAAG;YACH,GAAG;YACH,GAAG;YACH,GAAG;YACH,EAAE;YACF,GAAG;YACH,CAAC;YACD,EAAE;YACF,GAAG;YACH,EAAE;YACF,EAAE;YACF,EAAE;YACF,EAAE;YACF,EAAE;YACF,EAAE;YACF,EAAE;YACF,GAAG;YACH,GAAG;YACH,EAAE;YACF,GAAG;YACH,GAAG;YACH,GAAG;YACH,GAAG;SACJ,CAAC,CACH,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keplr-wallet/crypto",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.12-rc.3",
|
|
4
4
|
"main": "build/index.js",
|
|
5
5
|
"author": "chainapsis",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
"lint-fix": "eslint --fix \"src/**/*\" && prettier --write \"src/**/*\""
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@keplr-wallet/cosmos": "^0.9.9",
|
|
21
20
|
"@types/crypto-js": "^4.0.1",
|
|
22
21
|
"@types/elliptic": "^6.4.12",
|
|
23
22
|
"@types/sha.js": "^2.4.0"
|
|
@@ -30,5 +29,5 @@
|
|
|
30
29
|
"elliptic": "^6.5.3",
|
|
31
30
|
"sha.js": "^2.4.11"
|
|
32
31
|
},
|
|
33
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "b9860ae52543cc989e6daaecf467536659be0fe4"
|
|
34
33
|
}
|
package/src/key.spec.ts
CHANGED
|
@@ -1,20 +1,51 @@
|
|
|
1
|
-
import assert from "assert";
|
|
2
1
|
import { Mnemonic } from "./mnemonic";
|
|
3
2
|
import { PrivKeySecp256k1 } from "./key";
|
|
4
|
-
import { Bech32Address } from "@keplr-wallet/cosmos";
|
|
5
3
|
|
|
6
4
|
describe("Test priv key", () => {
|
|
7
|
-
it("priv key should generate the valid
|
|
5
|
+
it("priv key should generate the valid pub key", () => {
|
|
8
6
|
const mnemonic =
|
|
9
7
|
"celery husband drama unaware blue empower jelly twist program say prepare page";
|
|
10
|
-
const expectedAddress = "cosmos1d2kh2xaen7c0zv3h7qnmghhwhsmmassqlmr2nv";
|
|
11
8
|
|
|
12
9
|
const privKey = new PrivKeySecp256k1(
|
|
13
10
|
Mnemonic.generateWalletFromMnemonic(mnemonic)
|
|
14
11
|
);
|
|
15
12
|
const pubKey = privKey.getPubKey();
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
expect(pubKey.toBytes()).toStrictEqual(
|
|
14
|
+
new Uint8Array([
|
|
15
|
+
2,
|
|
16
|
+
57,
|
|
17
|
+
75,
|
|
18
|
+
197,
|
|
19
|
+
54,
|
|
20
|
+
51,
|
|
21
|
+
54,
|
|
22
|
+
106,
|
|
23
|
+
42,
|
|
24
|
+
185,
|
|
25
|
+
181,
|
|
26
|
+
214,
|
|
27
|
+
151,
|
|
28
|
+
169,
|
|
29
|
+
76,
|
|
30
|
+
140,
|
|
31
|
+
1,
|
|
32
|
+
33,
|
|
33
|
+
204,
|
|
34
|
+
94,
|
|
35
|
+
63,
|
|
36
|
+
13,
|
|
37
|
+
85,
|
|
38
|
+
74,
|
|
39
|
+
99,
|
|
40
|
+
22,
|
|
41
|
+
126,
|
|
42
|
+
219,
|
|
43
|
+
49,
|
|
44
|
+
140,
|
|
45
|
+
234,
|
|
46
|
+
232,
|
|
47
|
+
188,
|
|
48
|
+
])
|
|
49
|
+
);
|
|
19
50
|
});
|
|
20
51
|
});
|
package/src/key.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ec } from "elliptic";
|
|
2
2
|
import CryptoJS from "crypto-js";
|
|
3
3
|
|
|
4
4
|
import { Buffer } from "buffer/";
|
|
5
5
|
|
|
6
6
|
export class PrivKeySecp256k1 {
|
|
7
|
+
static generateRandomKey(): PrivKeySecp256k1 {
|
|
8
|
+
const secp256k1 = new ec("secp256k1");
|
|
9
|
+
|
|
10
|
+
return new PrivKeySecp256k1(
|
|
11
|
+
Buffer.from(secp256k1.genKeyPair().getPrivate().toArray())
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
7
15
|
constructor(protected readonly privKey: Uint8Array) {}
|
|
8
16
|
|
|
9
17
|
toBytes(): Uint8Array {
|
|
@@ -11,7 +19,7 @@ export class PrivKeySecp256k1 {
|
|
|
11
19
|
}
|
|
12
20
|
|
|
13
21
|
getPubKey(): PubKeySecp256k1 {
|
|
14
|
-
const secp256k1 = new
|
|
22
|
+
const secp256k1 = new ec("secp256k1");
|
|
15
23
|
|
|
16
24
|
const key = secp256k1.keyFromPrivate(this.privKey);
|
|
17
25
|
|
|
@@ -21,7 +29,7 @@ export class PrivKeySecp256k1 {
|
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
sign(msg: Uint8Array): Uint8Array {
|
|
24
|
-
const secp256k1 = new
|
|
32
|
+
const secp256k1 = new ec("secp256k1");
|
|
25
33
|
const key = secp256k1.keyFromPrivate(this.privKey);
|
|
26
34
|
|
|
27
35
|
const hash = CryptoJS.SHA256(
|
|
@@ -53,4 +61,47 @@ export class PubKeySecp256k1 {
|
|
|
53
61
|
|
|
54
62
|
return new Uint8Array(Buffer.from(hash, "hex"));
|
|
55
63
|
}
|
|
64
|
+
|
|
65
|
+
toKeyPair(): ec.KeyPair {
|
|
66
|
+
const secp256k1 = new ec("secp256k1");
|
|
67
|
+
|
|
68
|
+
return secp256k1.keyFromPublic(
|
|
69
|
+
Buffer.from(this.pubKey).toString("hex"),
|
|
70
|
+
"hex"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
verify(msg: Uint8Array, signature: Uint8Array): boolean {
|
|
75
|
+
const hash = CryptoJS.SHA256(
|
|
76
|
+
CryptoJS.lib.WordArray.create(msg as any)
|
|
77
|
+
).toString();
|
|
78
|
+
|
|
79
|
+
const secp256k1 = new ec("secp256k1");
|
|
80
|
+
|
|
81
|
+
let r = signature.slice(0, 32);
|
|
82
|
+
let s = signature.slice(32);
|
|
83
|
+
const rIsNegative = r[0] >= 0x80;
|
|
84
|
+
const sIsNegative = s[0] >= 0x80;
|
|
85
|
+
if (rIsNegative) {
|
|
86
|
+
r = new Uint8Array([0, ...r]);
|
|
87
|
+
}
|
|
88
|
+
if (sIsNegative) {
|
|
89
|
+
s = new Uint8Array([0, ...s]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Der encoding
|
|
93
|
+
const derData = new Uint8Array([
|
|
94
|
+
0x02,
|
|
95
|
+
r.length,
|
|
96
|
+
...r,
|
|
97
|
+
0x02,
|
|
98
|
+
s.length,
|
|
99
|
+
...s,
|
|
100
|
+
]);
|
|
101
|
+
return secp256k1.verify(
|
|
102
|
+
Buffer.from(hash, "hex"),
|
|
103
|
+
new Uint8Array([0x30, derData.length, ...derData]),
|
|
104
|
+
this.toKeyPair()
|
|
105
|
+
);
|
|
106
|
+
}
|
|
56
107
|
}
|