@taquito/signer 23.0.3 → 24.0.0-RC.0
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/dist/lib/derivation-tools/ecdsa.js +43 -22
- package/dist/lib/derivation-tools/ed25519.js +2 -2
- package/dist/lib/derivation-tools/index.js +18 -3
- package/dist/lib/derivation-tools/types.js +4 -0
- package/dist/lib/ec-key.js +78 -61
- package/dist/lib/helpers.js +1 -1
- package/dist/lib/in-memory-signer.js +216 -0
- package/dist/lib/taquito-signer.js +4 -217
- package/dist/lib/version.js +2 -2
- package/dist/taquito-signer.es6.js +197 -208
- package/dist/taquito-signer.es6.js.map +1 -1
- package/dist/taquito-signer.umd.js +196 -209
- package/dist/taquito-signer.umd.js.map +1 -1
- package/dist/types/derivation-tools/ecdsa.d.ts +15 -10
- package/dist/types/derivation-tools/ed25519.d.ts +1 -1
- package/dist/types/derivation-tools/index.d.ts +1 -10
- package/dist/types/derivation-tools/types.d.ts +10 -0
- package/dist/types/ec-key.d.ts +5 -9
- package/dist/types/in-memory-signer.d.ts +60 -0
- package/dist/types/key-interface.d.ts +20 -0
- package/dist/types/taquito-signer.d.ts +5 -61
- package/package.json +8 -10
- package/dist/lib/import-key.js +0 -51
- package/dist/types/import-key.d.ts +0 -13
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PrivateKey = void 0;
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
6
|
+
const nist_1 = require("@noble/curves/nist");
|
|
7
|
+
const types_1 = require("./types");
|
|
7
8
|
const hmac_1 = require("@stablelib/hmac");
|
|
8
9
|
const sha512_1 = require("@stablelib/sha512");
|
|
9
10
|
const bn_js_1 = require("bn.js");
|
|
@@ -21,12 +22,22 @@ const maxSeedSize = 64;
|
|
|
21
22
|
class PrivateKey {
|
|
22
23
|
/**
|
|
23
24
|
*
|
|
24
|
-
* @param priv
|
|
25
|
+
* @param priv { secretKey: BN, curve: 'p256' | 'secp256k1' }
|
|
25
26
|
* @param chainCode slice 32->n HMAC hash key and seedkey (first instance curve default seedKey. after hmac value slice 32->n)
|
|
26
27
|
*/
|
|
27
28
|
constructor(priv, chainCode) {
|
|
29
|
+
this.priv = priv;
|
|
28
30
|
this.chainCode = chainCode;
|
|
29
|
-
|
|
31
|
+
const privateKeyBytes = priv.secretKey.toArray('be', 32);
|
|
32
|
+
const privateKeyUint8 = new Uint8Array(privateKeyBytes);
|
|
33
|
+
const publicKey = priv.curve === 'secp256k1'
|
|
34
|
+
? secp256k1_1.secp256k1.getPublicKey(privateKeyUint8, true) // compressed public key
|
|
35
|
+
: nist_1.p256.getPublicKey(privateKeyUint8, true); // compressed public key
|
|
36
|
+
this.keyPair = {
|
|
37
|
+
curve: priv.curve,
|
|
38
|
+
secretKey: priv.secretKey,
|
|
39
|
+
publicKey: publicKey,
|
|
40
|
+
};
|
|
30
41
|
}
|
|
31
42
|
/**
|
|
32
43
|
* @param seedSrc result of Bip39.mnemonicToSeed
|
|
@@ -35,7 +46,6 @@ class PrivateKey {
|
|
|
35
46
|
* @throws {@link InvalidBitSize} | {@link InvalidCurveError} | {@link InvalidSeedLengthError}
|
|
36
47
|
*/
|
|
37
48
|
static fromSeed(seedSrc, curve) {
|
|
38
|
-
var _a, _b;
|
|
39
49
|
let seed = typeof seedSrc === 'string' ? (0, utils_1.parseHex)(seedSrc) : seedSrc;
|
|
40
50
|
if (seed.length < minSeedSize || seed.length > maxSeedSize) {
|
|
41
51
|
throw new errors_1.InvalidSeedLengthError(seed.length);
|
|
@@ -43,9 +53,16 @@ class PrivateKey {
|
|
|
43
53
|
if (!Object.prototype.hasOwnProperty.call(seedKey, curve)) {
|
|
44
54
|
throw new errors_1.InvalidCurveError(`Unsupported curve "${curve}" expecting either "p256" or "secp256k1"`);
|
|
45
55
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
// Get curve order for validation
|
|
57
|
+
let curveOrder;
|
|
58
|
+
if (curve === 'secp256k1') {
|
|
59
|
+
curveOrder = new bn_js_1.default(secp256k1_1.secp256k1.Point.CURVE().n.toString());
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
curveOrder = new bn_js_1.default(nist_1.p256.Point.CURVE().n.toString());
|
|
63
|
+
}
|
|
64
|
+
if (curveOrder.bitLength() !== 256) {
|
|
65
|
+
throw new errors_1.InvalidBitSize(`Invalid curve "${curve}" with bit size "${curveOrder.bitLength()}" expecting bit size "256"`);
|
|
49
66
|
}
|
|
50
67
|
const key = new TextEncoder().encode(seedKey[curve]);
|
|
51
68
|
let d = null;
|
|
@@ -55,16 +72,14 @@ class PrivateKey {
|
|
|
55
72
|
const sum = new hmac_1.HMAC(sha512_1.SHA512, key).update(seed).digest();
|
|
56
73
|
d = new bn_js_1.default(sum.subarray(0, 32));
|
|
57
74
|
chain = sum.subarray(32);
|
|
58
|
-
if (d.isZero() || d.cmp(
|
|
75
|
+
if (d.isZero() || d.cmp(curveOrder) >= 0) {
|
|
59
76
|
seed = sum;
|
|
60
77
|
}
|
|
61
78
|
else {
|
|
62
79
|
i++;
|
|
63
80
|
}
|
|
64
81
|
}
|
|
65
|
-
|
|
66
|
-
keyPair.priv = d;
|
|
67
|
-
return new PrivateKey(keyPair, chain);
|
|
82
|
+
return new PrivateKey({ curve, secretKey: d }, chain);
|
|
68
83
|
}
|
|
69
84
|
/**
|
|
70
85
|
*
|
|
@@ -73,12 +88,12 @@ class PrivateKey {
|
|
|
73
88
|
*/
|
|
74
89
|
derive(index) {
|
|
75
90
|
const data = new Uint8Array(37);
|
|
76
|
-
if ((index &
|
|
91
|
+
if ((index & types_1.Hard) !== 0) {
|
|
77
92
|
// hardened derivation
|
|
78
|
-
data.set(this.keyPair.
|
|
93
|
+
data.set(this.keyPair.secretKey.toArray(), 1);
|
|
79
94
|
}
|
|
80
95
|
else {
|
|
81
|
-
data.set(this.keyPair.
|
|
96
|
+
data.set(this.keyPair.publicKey, 0);
|
|
82
97
|
}
|
|
83
98
|
new DataView(data.buffer).setUint32(33, index);
|
|
84
99
|
let d = new bn_js_1.default(0);
|
|
@@ -88,8 +103,16 @@ class PrivateKey {
|
|
|
88
103
|
const sum = new hmac_1.HMAC(sha512_1.SHA512, this.chainCode).update(data).digest();
|
|
89
104
|
d = new bn_js_1.default(sum.subarray(0, 32));
|
|
90
105
|
chain = sum.subarray(32);
|
|
91
|
-
|
|
92
|
-
|
|
106
|
+
// Get curve order for comparison
|
|
107
|
+
let curveOrder;
|
|
108
|
+
if (this.keyPair.curve === 'secp256k1') {
|
|
109
|
+
curveOrder = new bn_js_1.default(secp256k1_1.secp256k1.Point.CURVE().n.toString());
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
curveOrder = new bn_js_1.default(nist_1.p256.Point.CURVE().n.toString());
|
|
113
|
+
}
|
|
114
|
+
if (d.cmp(curveOrder) < 0) {
|
|
115
|
+
d = d.add(this.keyPair.secretKey).mod(curveOrder);
|
|
93
116
|
if (!d.isZero()) {
|
|
94
117
|
i++;
|
|
95
118
|
}
|
|
@@ -97,9 +120,7 @@ class PrivateKey {
|
|
|
97
120
|
data.set(chain, 1);
|
|
98
121
|
data[0] = 1;
|
|
99
122
|
}
|
|
100
|
-
|
|
101
|
-
keyPair.priv = d;
|
|
102
|
-
return new PrivateKey(keyPair, chain);
|
|
123
|
+
return new PrivateKey({ curve: this.keyPair.curve, secretKey: d }, chain);
|
|
103
124
|
}
|
|
104
125
|
/**
|
|
105
126
|
*
|
|
@@ -119,11 +140,11 @@ class PrivateKey {
|
|
|
119
140
|
* @throws {@link InvalidKeyError}
|
|
120
141
|
*/
|
|
121
142
|
bytes() {
|
|
122
|
-
if (!this.keyPair.
|
|
143
|
+
if (!this.keyPair.secretKey) {
|
|
123
144
|
throw new core_1.InvalidKeyError('missing private key');
|
|
124
145
|
}
|
|
125
146
|
// pad to 32 bytes as toArray() length argument seems to be ignored (BN bug)
|
|
126
|
-
const src = this.keyPair.
|
|
147
|
+
const src = this.keyPair.secretKey.toArray();
|
|
127
148
|
const out = new Uint8Array(32);
|
|
128
149
|
out.set(src, out.length - src.length);
|
|
129
150
|
return out;
|
|
@@ -5,7 +5,7 @@ exports.PrivateKey = void 0;
|
|
|
5
5
|
const hmac_1 = require("@stablelib/hmac");
|
|
6
6
|
const sha512_1 = require("@stablelib/sha512");
|
|
7
7
|
const ed25519_1 = require("@stablelib/ed25519");
|
|
8
|
-
const
|
|
8
|
+
const types_1 = require("./types");
|
|
9
9
|
const utils_1 = require("./utils");
|
|
10
10
|
const errors_1 = require("../errors");
|
|
11
11
|
const core_1 = require("@taquito/core");
|
|
@@ -51,7 +51,7 @@ class PrivateKey {
|
|
|
51
51
|
* @returns derivation path child of original private key pair
|
|
52
52
|
*/
|
|
53
53
|
derive(index) {
|
|
54
|
-
if ((index &
|
|
54
|
+
if ((index & types_1.Hard) === 0) {
|
|
55
55
|
throw new core_1.InvalidDerivationPathError(index.toString(), ': Non-hardened derivation path.');
|
|
56
56
|
}
|
|
57
57
|
const data = new Uint8Array(37);
|
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Path = exports.
|
|
17
|
+
exports.Path = exports.Ed25519 = exports.ECDSA = void 0;
|
|
4
18
|
const core_1 = require("@taquito/core");
|
|
19
|
+
const types_1 = require("./types");
|
|
5
20
|
exports.ECDSA = require("./ecdsa");
|
|
6
21
|
exports.Ed25519 = require("./ed25519");
|
|
7
|
-
exports
|
|
22
|
+
__exportStar(require("./types"), exports);
|
|
8
23
|
class Path extends Array {
|
|
9
24
|
static from(iterable) {
|
|
10
25
|
return super.from(iterable).map((x) => x >>> 0);
|
|
@@ -30,7 +45,7 @@ class Path extends Array {
|
|
|
30
45
|
let h = 0;
|
|
31
46
|
const last = p[p.length - 1];
|
|
32
47
|
if (last === "'" || last === 'h' || last === 'H') {
|
|
33
|
-
h =
|
|
48
|
+
h = types_1.Hard;
|
|
34
49
|
p = p.slice(0, p.length - 1);
|
|
35
50
|
}
|
|
36
51
|
const index = (parseInt(p, 10) | h) >>> 0;
|
package/dist/lib/ec-key.js
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _ECKey_keyPair, _ECPublicKey_key;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.ECPublicKey = exports.ECKey = void 0;
|
|
4
16
|
const blake2b_1 = require("@stablelib/blake2b");
|
|
5
17
|
const utils_1 = require("@taquito/utils");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
18
|
+
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
19
|
+
const nist_js_1 = require("@noble/curves/nist.js");
|
|
8
20
|
const errors_1 = require("./errors");
|
|
9
21
|
const pref = {
|
|
10
22
|
p256: {
|
|
@@ -22,27 +34,10 @@ const pref = {
|
|
|
22
34
|
tag: 1,
|
|
23
35
|
},
|
|
24
36
|
};
|
|
25
|
-
class ECKeyBase {
|
|
26
|
-
constructor(keyPair) {
|
|
27
|
-
this.keyPair = keyPair;
|
|
28
|
-
}
|
|
29
|
-
curve() {
|
|
30
|
-
switch (this.keyPair.ec.curve) {
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
-
case elliptic_1.default.curves.secp256k1.curve:
|
|
33
|
-
return 'secp256k1';
|
|
34
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
-
case elliptic_1.default.curves.p256.curve:
|
|
36
|
-
return 'p256';
|
|
37
|
-
default:
|
|
38
|
-
throw new errors_1.InvalidCurveError('unknown curve');
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
37
|
/**
|
|
43
38
|
* @description Provide signing logic for elliptic curve based key (tz2, tz3)
|
|
44
39
|
*/
|
|
45
|
-
class ECKey
|
|
40
|
+
class ECKey {
|
|
46
41
|
/**
|
|
47
42
|
*
|
|
48
43
|
* @param key Encoded private key
|
|
@@ -50,6 +45,7 @@ class ECKey extends ECKeyBase {
|
|
|
50
45
|
* @throws {@link InvalidKeyError}
|
|
51
46
|
*/
|
|
52
47
|
constructor(key, decrypt) {
|
|
48
|
+
_ECKey_keyPair.set(this, void 0);
|
|
53
49
|
const [keyData, prefix] = (0, utils_1.b58DecodeAndCheckPrefix)(key, [
|
|
54
50
|
utils_1.PrefixV2.Secp256k1EncryptedSecretKey,
|
|
55
51
|
utils_1.PrefixV2.P256EncryptedSecretKey,
|
|
@@ -75,7 +71,13 @@ class ECKey extends ECKeyBase {
|
|
|
75
71
|
return [keyData, 'p256'];
|
|
76
72
|
}
|
|
77
73
|
})();
|
|
78
|
-
|
|
74
|
+
__classPrivateFieldSet(this, _ECKey_keyPair, {
|
|
75
|
+
curve,
|
|
76
|
+
secretKey: decKey,
|
|
77
|
+
publicKey: curve === 'secp256k1'
|
|
78
|
+
? secp256k1_1.secp256k1.getPublicKey(decKey, true)
|
|
79
|
+
: nist_js_1.p256.getPublicKey(decKey, true),
|
|
80
|
+
}, "f");
|
|
79
81
|
}
|
|
80
82
|
/**
|
|
81
83
|
*
|
|
@@ -84,69 +86,72 @@ class ECKey extends ECKeyBase {
|
|
|
84
86
|
*/
|
|
85
87
|
sign(bytes) {
|
|
86
88
|
const hash = (0, blake2b_1.hash)(bytes, 32);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
89
|
+
let signature;
|
|
90
|
+
if (__classPrivateFieldGet(this, _ECKey_keyPair, "f").curve === 'secp256k1') {
|
|
91
|
+
signature = secp256k1_1.secp256k1
|
|
92
|
+
.sign(hash, __classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, {
|
|
93
|
+
lowS: true, // Use canonical signatures (prevents malleability)
|
|
94
|
+
})
|
|
95
|
+
.toBytes('compact');
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
signature = nist_js_1.p256
|
|
99
|
+
.sign(hash, __classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, {
|
|
100
|
+
lowS: true, // Use canonical signatures (prevents malleability)
|
|
101
|
+
})
|
|
102
|
+
.toBytes('compact');
|
|
103
|
+
}
|
|
93
104
|
return {
|
|
94
105
|
rawSignature: signature,
|
|
95
106
|
sig: (0, utils_1.b58Encode)(signature, utils_1.PrefixV2.GenericSignature),
|
|
96
|
-
prefixSig: (0, utils_1.b58Encode)(signature, pref[this.curve
|
|
107
|
+
prefixSig: (0, utils_1.b58Encode)(signature, pref[__classPrivateFieldGet(this, _ECKey_keyPair, "f").curve].sig),
|
|
97
108
|
};
|
|
98
109
|
}
|
|
99
110
|
/**
|
|
100
111
|
* @returns Encoded public key
|
|
101
112
|
*/
|
|
102
113
|
publicKey() {
|
|
103
|
-
return new ECPublicKey(this.
|
|
114
|
+
return new ECPublicKey(__classPrivateFieldGet(this, _ECKey_keyPair, "f").publicKey, __classPrivateFieldGet(this, _ECKey_keyPair, "f").curve);
|
|
104
115
|
}
|
|
105
116
|
/**
|
|
106
117
|
* @returns Encoded private key
|
|
107
118
|
*/
|
|
108
119
|
secretKey() {
|
|
109
|
-
return (0, utils_1.b58Encode)(
|
|
120
|
+
return (0, utils_1.b58Encode)(__classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, pref[__classPrivateFieldGet(this, _ECKey_keyPair, "f").curve].sk);
|
|
110
121
|
}
|
|
111
122
|
}
|
|
112
123
|
exports.ECKey = ECKey;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
class ECPublicKey extends ECKeyBase {
|
|
124
|
+
_ECKey_keyPair = new WeakMap();
|
|
125
|
+
class ECPublicKey {
|
|
117
126
|
constructor(src, curve) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
127
|
+
_ECPublicKey_key.set(this, void 0);
|
|
128
|
+
const [key, crv] = (() => {
|
|
129
|
+
if (typeof src === 'string') {
|
|
130
|
+
const [key, pre] = (0, utils_1.b58DecodeAndCheckPrefix)(src, [
|
|
131
|
+
utils_1.PrefixV2.Secp256k1PublicKey,
|
|
132
|
+
utils_1.PrefixV2.P256PublicKey,
|
|
133
|
+
]);
|
|
134
|
+
return [key, pre === utils_1.PrefixV2.Secp256k1PublicKey ? 'secp256k1' : 'p256'];
|
|
135
|
+
}
|
|
136
|
+
else if (curve !== undefined) {
|
|
137
|
+
return [src, curve];
|
|
121
138
|
}
|
|
122
139
|
else {
|
|
123
|
-
|
|
124
|
-
if (typeof src === 'string') {
|
|
125
|
-
const [key, pre] = (0, utils_1.b58DecodeAndCheckPrefix)(src, [
|
|
126
|
-
utils_1.PrefixV2.Secp256k1PublicKey,
|
|
127
|
-
utils_1.PrefixV2.P256PublicKey,
|
|
128
|
-
]);
|
|
129
|
-
return [key, pre === utils_1.PrefixV2.Secp256k1PublicKey ? 'secp256k1' : 'p256'];
|
|
130
|
-
}
|
|
131
|
-
else if (curve !== undefined) {
|
|
132
|
-
return [src, curve];
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
throw new errors_1.InvalidCurveError('missing curve type');
|
|
136
|
-
}
|
|
137
|
-
})();
|
|
138
|
-
return new elliptic_1.default.ec(crv).keyFromPublic(key);
|
|
140
|
+
throw new errors_1.InvalidCurveError('missing curve type');
|
|
139
141
|
}
|
|
140
142
|
})();
|
|
141
|
-
|
|
143
|
+
// For ECPublicKey, we don't need the private key, so we pass an empty array
|
|
144
|
+
// The public key is stored separately
|
|
145
|
+
__classPrivateFieldSet(this, _ECPublicKey_key, key, "f");
|
|
146
|
+
this.curve = crv;
|
|
142
147
|
}
|
|
143
148
|
compare(other) {
|
|
144
149
|
if (other instanceof ECPublicKey) {
|
|
145
|
-
if (this.curve
|
|
146
|
-
const compress = this.curve
|
|
150
|
+
if (this.curve === other.curve) {
|
|
151
|
+
const compress = this.curve === 'secp256k1';
|
|
147
152
|
return (0, utils_1.compareArrays)(this.bytes(compress), other.bytes(compress));
|
|
148
153
|
}
|
|
149
|
-
else if (this.curve
|
|
154
|
+
else if (this.curve === 'secp256k1') {
|
|
150
155
|
return -1;
|
|
151
156
|
}
|
|
152
157
|
else {
|
|
@@ -159,21 +164,33 @@ class ECPublicKey extends ECKeyBase {
|
|
|
159
164
|
}
|
|
160
165
|
hash() {
|
|
161
166
|
const key = this.bytes();
|
|
162
|
-
return (0, utils_1.b58Encode)((0, blake2b_1.hash)(key, 20), pref[this.curve
|
|
167
|
+
return (0, utils_1.b58Encode)((0, blake2b_1.hash)(key, 20), pref[this.curve].pkh);
|
|
163
168
|
}
|
|
164
169
|
bytes(compress = true) {
|
|
165
|
-
|
|
170
|
+
// @noble/curves supports both compressed and uncompressed formats
|
|
171
|
+
// We need to convert the stored public key to the requested format
|
|
172
|
+
if (this.curve === 'secp256k1') {
|
|
173
|
+
// For secp256k1, we need to get the public key from the stored bytes and convert format
|
|
174
|
+
const point = secp256k1_1.secp256k1.Point.fromHex(__classPrivateFieldGet(this, _ECPublicKey_key, "f"));
|
|
175
|
+
return point.toBytes(compress);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// For p256, we need to get the public key from the stored bytes and convert format
|
|
179
|
+
const point = nist_js_1.p256.Point.fromHex(__classPrivateFieldGet(this, _ECPublicKey_key, "f"));
|
|
180
|
+
return point.toBytes(compress);
|
|
181
|
+
}
|
|
166
182
|
}
|
|
167
183
|
toProtocol() {
|
|
168
184
|
const key = this.bytes();
|
|
169
185
|
const res = new Uint8Array(key.length + 1);
|
|
170
|
-
res[0] = pref[this.curve
|
|
186
|
+
res[0] = pref[this.curve].tag;
|
|
171
187
|
res.set(key, 1);
|
|
172
188
|
return res;
|
|
173
189
|
}
|
|
174
190
|
toString() {
|
|
175
191
|
const key = this.bytes();
|
|
176
|
-
return (0, utils_1.b58Encode)(key, pref[this.curve
|
|
192
|
+
return (0, utils_1.b58Encode)(key, pref[this.curve].pk);
|
|
177
193
|
}
|
|
178
194
|
}
|
|
179
195
|
exports.ECPublicKey = ECPublicKey;
|
|
196
|
+
_ECPublicKey_key = new WeakMap();
|
package/dist/lib/helpers.js
CHANGED
|
@@ -29,7 +29,7 @@ const generateSecretKey = (seed, derivationPath, curve) => {
|
|
|
29
29
|
const prefixType = curve === 'secp256k1' ? utils_1.PrefixV2.Secp256k1SecretKey : utils_1.PrefixV2.P256SecretKey;
|
|
30
30
|
let privKey = ecdsa_1.PrivateKey.fromSeed(seed, curve);
|
|
31
31
|
privKey = privKey.derivePath(path);
|
|
32
|
-
const uint8arr = new Uint8Array(privKey.keyPair.
|
|
32
|
+
const uint8arr = new Uint8Array(privKey.keyPair.secretKey.toArray());
|
|
33
33
|
const sk = (0, utils_1.b58Encode)(uint8arr, prefixType);
|
|
34
34
|
return sk;
|
|
35
35
|
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var _InMemorySigner_key;
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.InMemorySigner = void 0;
|
|
25
|
+
exports.publicKeyFromString = publicKeyFromString;
|
|
26
|
+
const nacl_1 = require("@stablelib/nacl");
|
|
27
|
+
const utils_1 = require("@taquito/utils");
|
|
28
|
+
const typedarray_to_buffer_1 = require("typedarray-to-buffer");
|
|
29
|
+
const ed_key_1 = require("./ed-key");
|
|
30
|
+
const ec_key_1 = require("./ec-key");
|
|
31
|
+
const pbkdf2_1 = require("pbkdf2");
|
|
32
|
+
const Bip39 = require("bip39");
|
|
33
|
+
const helpers_1 = require("./helpers");
|
|
34
|
+
const errors_1 = require("./errors");
|
|
35
|
+
const core_1 = require("@taquito/core");
|
|
36
|
+
const key_interface_1 = require("./key-interface");
|
|
37
|
+
const bls_key_1 = require("./bls-key");
|
|
38
|
+
/**
|
|
39
|
+
* @description A local implementation of the signer. Will represent a Tezos account and be able to produce signature in its behalf
|
|
40
|
+
*
|
|
41
|
+
* @warn If running in production and dealing with tokens that have real value, it is strongly recommended to use a HSM backed signer so that private key material is not stored in memory or on disk
|
|
42
|
+
* @throws {@link InvalidMnemonicError}
|
|
43
|
+
*/
|
|
44
|
+
class InMemorySigner {
|
|
45
|
+
static fromFundraiser(email, password, mnemonic) {
|
|
46
|
+
if (!Bip39.validateMnemonic(mnemonic)) {
|
|
47
|
+
throw new errors_1.InvalidMnemonicError(mnemonic);
|
|
48
|
+
}
|
|
49
|
+
const seed = Bip39.mnemonicToSeedSync(mnemonic, `${email}${password}`);
|
|
50
|
+
const key = (0, utils_1.b58Encode)(seed.subarray(0, 32), utils_1.PrefixV2.Ed25519Seed);
|
|
51
|
+
return new InMemorySigner(key);
|
|
52
|
+
}
|
|
53
|
+
static fromSecretKey(key, passphrase) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
return new InMemorySigner(key, passphrase);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
* @description Instantiation of an InMemorySigner instance from a mnemonic
|
|
61
|
+
* @param mnemonic 12-24 word mnemonic
|
|
62
|
+
* @param password password used to encrypt the mnemonic to seed value
|
|
63
|
+
* @param derivationPath default 44'/1729'/0'/0' (44'/1729' mandatory)
|
|
64
|
+
* @param curve currently only supported for tz1, tz2, tz3 addresses. soon bip25519
|
|
65
|
+
* @returns InMemorySigner
|
|
66
|
+
* @throws {@link InvalidMnemonicError}
|
|
67
|
+
*/
|
|
68
|
+
static fromMnemonic({ mnemonic, password = '', derivationPath = "44'/1729'/0'/0'", curve = 'ed25519', }) {
|
|
69
|
+
// check if curve is defined if not default tz1
|
|
70
|
+
if (!Bip39.validateMnemonic(mnemonic)) {
|
|
71
|
+
// avoiding exposing mnemonic again in case of mistake making invalid
|
|
72
|
+
throw new errors_1.InvalidMnemonicError(mnemonic);
|
|
73
|
+
}
|
|
74
|
+
const seed = Bip39.mnemonicToSeedSync(mnemonic, password);
|
|
75
|
+
const sk = (0, helpers_1.generateSecretKey)(seed, derivationPath, curve);
|
|
76
|
+
return new InMemorySigner(sk);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
*
|
|
80
|
+
* @param key Encoded private key
|
|
81
|
+
* @param passphrase Passphrase to decrypt the private key if it is encrypted
|
|
82
|
+
* @throws {@link InvalidKeyError}
|
|
83
|
+
*
|
|
84
|
+
*/
|
|
85
|
+
constructor(key, passphrase) {
|
|
86
|
+
_InMemorySigner_key.set(this, void 0);
|
|
87
|
+
const keyPrefixes = [
|
|
88
|
+
utils_1.PrefixV2.Ed25519EncryptedSeed,
|
|
89
|
+
utils_1.PrefixV2.Ed25519Seed,
|
|
90
|
+
utils_1.PrefixV2.Ed25519SecretKey,
|
|
91
|
+
utils_1.PrefixV2.Secp256k1EncryptedSecretKey,
|
|
92
|
+
utils_1.PrefixV2.Secp256k1SecretKey,
|
|
93
|
+
utils_1.PrefixV2.P256EncryptedSecretKey,
|
|
94
|
+
utils_1.PrefixV2.P256SecretKey,
|
|
95
|
+
utils_1.PrefixV2.BLS12_381EncryptedSecretKey,
|
|
96
|
+
utils_1.PrefixV2.BLS12_381SecretKey,
|
|
97
|
+
];
|
|
98
|
+
const pre = (() => {
|
|
99
|
+
try {
|
|
100
|
+
const [, pre] = (0, utils_1.b58DecodeAndCheckPrefix)(key, keyPrefixes);
|
|
101
|
+
return pre;
|
|
102
|
+
}
|
|
103
|
+
catch (_a) {
|
|
104
|
+
throw new core_1.InvalidKeyError(`Invalid private key, expecting one of the following prefixes '${keyPrefixes}'.`);
|
|
105
|
+
}
|
|
106
|
+
})();
|
|
107
|
+
const encrypted = pre === utils_1.PrefixV2.Ed25519EncryptedSeed ||
|
|
108
|
+
pre === utils_1.PrefixV2.Secp256k1EncryptedSecretKey ||
|
|
109
|
+
pre === utils_1.PrefixV2.P256EncryptedSecretKey ||
|
|
110
|
+
pre === utils_1.PrefixV2.BLS12_381EncryptedSecretKey;
|
|
111
|
+
let decrypt;
|
|
112
|
+
if (encrypted) {
|
|
113
|
+
if (!passphrase) {
|
|
114
|
+
throw new errors_1.InvalidPassphraseError('No passphrase provided to decrypt encrypted key');
|
|
115
|
+
}
|
|
116
|
+
decrypt = (data) => {
|
|
117
|
+
const salt = (0, typedarray_to_buffer_1.default)(data.slice(0, 8));
|
|
118
|
+
const encryptedSk = data.slice(8);
|
|
119
|
+
const encryptionKey = pbkdf2_1.default.pbkdf2Sync(passphrase, salt, 32768, 32, 'sha512');
|
|
120
|
+
const res = (0, nacl_1.openSecretBox)(new Uint8Array(encryptionKey), new Uint8Array(24), new Uint8Array(encryptedSk));
|
|
121
|
+
if (!res) {
|
|
122
|
+
throw new Error("can't decrypt secret key");
|
|
123
|
+
}
|
|
124
|
+
return res;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
switch (pre) {
|
|
128
|
+
case utils_1.PrefixV2.Ed25519EncryptedSeed:
|
|
129
|
+
case utils_1.PrefixV2.Ed25519Seed:
|
|
130
|
+
case utils_1.PrefixV2.Ed25519SecretKey:
|
|
131
|
+
__classPrivateFieldSet(this, _InMemorySigner_key, new ed_key_1.EdKey(key, decrypt), "f");
|
|
132
|
+
break;
|
|
133
|
+
case utils_1.PrefixV2.Secp256k1EncryptedSecretKey:
|
|
134
|
+
case utils_1.PrefixV2.Secp256k1SecretKey:
|
|
135
|
+
case utils_1.PrefixV2.P256EncryptedSecretKey:
|
|
136
|
+
case utils_1.PrefixV2.P256SecretKey:
|
|
137
|
+
__classPrivateFieldSet(this, _InMemorySigner_key, new ec_key_1.ECKey(key, decrypt), "f");
|
|
138
|
+
break;
|
|
139
|
+
case utils_1.PrefixV2.BLS12_381EncryptedSecretKey:
|
|
140
|
+
case utils_1.PrefixV2.BLS12_381SecretKey:
|
|
141
|
+
__classPrivateFieldSet(this, _InMemorySigner_key, new bls_key_1.BLSKey(key, decrypt), "f");
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
*
|
|
147
|
+
* @param bytes Bytes to sign
|
|
148
|
+
* @param watermark Watermark to append to the bytes
|
|
149
|
+
*/
|
|
150
|
+
sign(message, watermark) {
|
|
151
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
const msg = typeof message == 'string' ? (0, utils_1.hex2buf)(message) : message;
|
|
153
|
+
const watermarkMsg = watermark !== undefined ? (0, utils_1.mergebuf)(watermark, msg) : msg;
|
|
154
|
+
const { rawSignature, sig: signature, prefixSig: prefixedSignature, } = yield __classPrivateFieldGet(this, _InMemorySigner_key, "f").sign(watermarkMsg);
|
|
155
|
+
return {
|
|
156
|
+
bytes: (0, utils_1.buf2hex)(msg),
|
|
157
|
+
sig: signature,
|
|
158
|
+
prefixSig: prefixedSignature,
|
|
159
|
+
sbytes: (0, utils_1.buf2hex)((0, utils_1.mergebuf)(msg,
|
|
160
|
+
// bls only Signature_prefix ff03 ref:https://octez.tezos.com/docs/shell/p2p_api.html#signature-prefix-tag-255 & https://octez.tezos.com/docs/shell/p2p_api.html#bls-prefix-tag-3
|
|
161
|
+
(0, key_interface_1.isPOP)(__classPrivateFieldGet(this, _InMemorySigner_key, "f")) ? (0, utils_1.mergebuf)(new Uint8Array([255, 3]), rawSignature) : rawSignature)),
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
provePossession() {
|
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
if ((0, key_interface_1.isPOP)(__classPrivateFieldGet(this, _InMemorySigner_key, "f"))) {
|
|
168
|
+
return __classPrivateFieldGet(this, _InMemorySigner_key, "f").provePossession();
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
throw new core_1.ProhibitedActionError('Only BLS keys can prove possession');
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
get canProvePossession() {
|
|
176
|
+
return (0, key_interface_1.isPOP)(__classPrivateFieldGet(this, _InMemorySigner_key, "f"));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* @returns Encoded public key
|
|
180
|
+
*/
|
|
181
|
+
publicKey() {
|
|
182
|
+
return Promise.resolve(String(__classPrivateFieldGet(this, _InMemorySigner_key, "f").publicKey()));
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* @returns Encoded public key hash
|
|
186
|
+
*/
|
|
187
|
+
publicKeyHash() {
|
|
188
|
+
return Promise.resolve(__classPrivateFieldGet(this, _InMemorySigner_key, "f").publicKey().hash());
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @returns Encoded private key
|
|
192
|
+
*/
|
|
193
|
+
secretKey() {
|
|
194
|
+
return Promise.resolve(__classPrivateFieldGet(this, _InMemorySigner_key, "f").secretKey());
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.InMemorySigner = InMemorySigner;
|
|
198
|
+
_InMemorySigner_key = new WeakMap();
|
|
199
|
+
function publicKeyFromString(src) {
|
|
200
|
+
const [keyData, pre] = (0, utils_1.b58DecodeAndCheckPrefix)(src, [
|
|
201
|
+
utils_1.PrefixV2.Ed25519PublicKey,
|
|
202
|
+
utils_1.PrefixV2.Secp256k1PublicKey,
|
|
203
|
+
utils_1.PrefixV2.P256PublicKey,
|
|
204
|
+
utils_1.PrefixV2.BLS12_381PublicKey,
|
|
205
|
+
]);
|
|
206
|
+
switch (pre) {
|
|
207
|
+
case utils_1.PrefixV2.Ed25519PublicKey:
|
|
208
|
+
return new ed_key_1.EdPublicKey(keyData);
|
|
209
|
+
case utils_1.PrefixV2.Secp256k1PublicKey:
|
|
210
|
+
return new ec_key_1.ECPublicKey(keyData, 'secp256k1');
|
|
211
|
+
case utils_1.PrefixV2.P256PublicKey:
|
|
212
|
+
return new ec_key_1.ECPublicKey(keyData, 'p256');
|
|
213
|
+
case utils_1.PrefixV2.BLS12_381PublicKey:
|
|
214
|
+
return new bls_key_1.BLSPublicKey(keyData);
|
|
215
|
+
}
|
|
216
|
+
}
|