@taquito/signer 23.0.0-beta.0 → 23.0.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/bls-key.js +104 -0
- package/dist/lib/ec-key.js +140 -74
- package/dist/lib/ed-key.js +86 -66
- package/dist/lib/helpers.js +3 -3
- package/dist/lib/key-interface.js +6 -0
- package/dist/lib/signer.js +6 -0
- package/dist/lib/taquito-signer.js +109 -35
- package/dist/lib/version.js +2 -2
- package/dist/taquito-signer.es6.js +458 -199
- package/dist/taquito-signer.es6.js.map +1 -1
- package/dist/taquito-signer.umd.js +457 -199
- package/dist/taquito-signer.umd.js.map +1 -1
- package/dist/types/bls-key.d.ts +21 -0
- package/dist/types/ec-key.d.ts +25 -28
- package/dist/types/ed-key.d.ts +17 -19
- package/dist/types/key-interface.d.ts +17 -0
- package/dist/types/signer.d.ts +16 -0
- package/dist/types/taquito-signer.d.ts +9 -8
- package/package.json +6 -5
|
@@ -0,0 +1,104 @@
|
|
|
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 _BLSKey_key, _BLSKey_publicKey, _BLSPublicKey_key;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.BLSPublicKey = exports.BLSKey = void 0;
|
|
16
|
+
const utils_1 = require("@taquito/utils");
|
|
17
|
+
const bls12_381_1 = require("@noble/curves/bls12-381");
|
|
18
|
+
const blake2b_1 = require("@stablelib/blake2b");
|
|
19
|
+
const bls = bls12_381_1.bls12_381.longSignatures; // AKA MinPK
|
|
20
|
+
class BLSKey {
|
|
21
|
+
constructor(key, decrypt) {
|
|
22
|
+
_BLSKey_key.set(this, void 0);
|
|
23
|
+
_BLSKey_publicKey.set(this, void 0);
|
|
24
|
+
const tmp = (0, utils_1.b58DecodeAndCheckPrefix)(key, [
|
|
25
|
+
utils_1.PrefixV2.BLS12_381EncryptedSecretKey,
|
|
26
|
+
utils_1.PrefixV2.BLS12_381SecretKey,
|
|
27
|
+
]);
|
|
28
|
+
let [keyData] = tmp;
|
|
29
|
+
const [, prefix] = tmp;
|
|
30
|
+
if (prefix === utils_1.PrefixV2.BLS12_381EncryptedSecretKey) {
|
|
31
|
+
if (decrypt !== undefined) {
|
|
32
|
+
keyData = decrypt(keyData);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw new Error('decryption function is not provided');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
__classPrivateFieldSet(this, _BLSKey_key, keyData, "f");
|
|
39
|
+
__classPrivateFieldSet(this, _BLSKey_publicKey, bls.getPublicKey(this.sk()).toBytes(), "f");
|
|
40
|
+
}
|
|
41
|
+
sk() {
|
|
42
|
+
return new Uint8Array(__classPrivateFieldGet(this, _BLSKey_key, "f")).reverse();
|
|
43
|
+
}
|
|
44
|
+
signDst(message, dst) {
|
|
45
|
+
const point = bls.hash(message, dst);
|
|
46
|
+
const sig = bls.sign(point, this.sk()).toBytes();
|
|
47
|
+
return {
|
|
48
|
+
rawSignature: sig,
|
|
49
|
+
sig: (0, utils_1.b58Encode)(sig, utils_1.PrefixV2.GenericSignature),
|
|
50
|
+
prefixSig: (0, utils_1.b58Encode)(sig, utils_1.PrefixV2.BLS12_381Signature),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
sign(message) {
|
|
54
|
+
return this.signDst(message, utils_1.BLS12_381_DST);
|
|
55
|
+
}
|
|
56
|
+
provePossession() {
|
|
57
|
+
return this.signDst(__classPrivateFieldGet(this, _BLSKey_publicKey, "f"), utils_1.POP_DST);
|
|
58
|
+
}
|
|
59
|
+
publicKey() {
|
|
60
|
+
return new BLSPublicKey(__classPrivateFieldGet(this, _BLSKey_publicKey, "f"));
|
|
61
|
+
}
|
|
62
|
+
secretKey() {
|
|
63
|
+
return (0, utils_1.b58Encode)(__classPrivateFieldGet(this, _BLSKey_key, "f"), utils_1.PrefixV2.BLS12_381SecretKey);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.BLSKey = BLSKey;
|
|
67
|
+
_BLSKey_key = new WeakMap(), _BLSKey_publicKey = new WeakMap();
|
|
68
|
+
class BLSPublicKey {
|
|
69
|
+
constructor(src) {
|
|
70
|
+
_BLSPublicKey_key.set(this, void 0);
|
|
71
|
+
if (typeof src === 'string') {
|
|
72
|
+
const [key, _] = (0, utils_1.b58DecodeAndCheckPrefix)(src, [utils_1.PrefixV2.BLS12_381PublicKey]);
|
|
73
|
+
__classPrivateFieldSet(this, _BLSPublicKey_key, key, "f");
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
__classPrivateFieldSet(this, _BLSPublicKey_key, src, "f");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
compare(other) {
|
|
80
|
+
if (other instanceof BLSPublicKey) {
|
|
81
|
+
return (0, utils_1.compareArrays)(this.bytes(), other.bytes());
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
throw new utils_1.InvalidPublicKeyError('BLS key expected');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
hash() {
|
|
88
|
+
return (0, utils_1.b58Encode)((0, blake2b_1.hash)(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), 20), utils_1.PrefixV2.BLS12_381PublicKeyHash);
|
|
89
|
+
}
|
|
90
|
+
bytes() {
|
|
91
|
+
return __classPrivateFieldGet(this, _BLSPublicKey_key, "f");
|
|
92
|
+
}
|
|
93
|
+
toProtocol() {
|
|
94
|
+
const res = new Uint8Array(__classPrivateFieldGet(this, _BLSPublicKey_key, "f").length + 1);
|
|
95
|
+
res[0] = 3;
|
|
96
|
+
res.set(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), 1);
|
|
97
|
+
return res;
|
|
98
|
+
}
|
|
99
|
+
toString() {
|
|
100
|
+
return (0, utils_1.b58Encode)(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), utils_1.PrefixV2.BLS12_381PublicKey);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.BLSPublicKey = BLSPublicKey;
|
|
104
|
+
_BLSPublicKey_key = new WeakMap();
|
package/dist/lib/ec-key.js
CHANGED
|
@@ -1,113 +1,179 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
3
|
+
exports.ECPublicKey = exports.ECKey = void 0;
|
|
13
4
|
const blake2b_1 = require("@stablelib/blake2b");
|
|
14
5
|
const utils_1 = require("@taquito/utils");
|
|
15
|
-
const typedarray_to_buffer_1 = require("typedarray-to-buffer");
|
|
16
6
|
const elliptic_1 = require("elliptic");
|
|
17
|
-
const
|
|
7
|
+
const key_1 = require("elliptic/lib/elliptic/ec/key");
|
|
8
|
+
const errors_1 = require("./errors");
|
|
18
9
|
const pref = {
|
|
19
10
|
p256: {
|
|
20
|
-
pk: utils_1.
|
|
21
|
-
sk: utils_1.
|
|
22
|
-
pkh: utils_1.
|
|
23
|
-
sig: utils_1.
|
|
11
|
+
pk: utils_1.PrefixV2.P256PublicKey,
|
|
12
|
+
sk: utils_1.PrefixV2.P256SecretKey,
|
|
13
|
+
pkh: utils_1.PrefixV2.P256PublicKeyHash,
|
|
14
|
+
sig: utils_1.PrefixV2.P256Signature,
|
|
15
|
+
tag: 2,
|
|
24
16
|
},
|
|
25
17
|
secp256k1: {
|
|
26
|
-
pk: utils_1.
|
|
27
|
-
sk: utils_1.
|
|
28
|
-
pkh: utils_1.
|
|
29
|
-
sig: utils_1.
|
|
18
|
+
pk: utils_1.PrefixV2.Secp256k1PublicKey,
|
|
19
|
+
sk: utils_1.PrefixV2.Secp256k1SecretKey,
|
|
20
|
+
pkh: utils_1.PrefixV2.Secp256k1PublicKeyHash,
|
|
21
|
+
sig: utils_1.PrefixV2.Secp256k1Signature,
|
|
22
|
+
tag: 1,
|
|
30
23
|
},
|
|
31
24
|
};
|
|
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
|
+
}
|
|
32
42
|
/**
|
|
33
43
|
* @description Provide signing logic for elliptic curve based key (tz2, tz3)
|
|
34
44
|
*/
|
|
35
|
-
class ECKey {
|
|
45
|
+
class ECKey extends ECKeyBase {
|
|
36
46
|
/**
|
|
37
47
|
*
|
|
38
|
-
* @param curve Curve to use with the key
|
|
39
48
|
* @param key Encoded private key
|
|
40
|
-
* @param encrypted Is the private key encrypted
|
|
41
49
|
* @param decrypt Decrypt function
|
|
42
50
|
* @throws {@link InvalidKeyError}
|
|
43
51
|
*/
|
|
44
|
-
constructor(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
constructor(key, decrypt) {
|
|
53
|
+
const [keyData, prefix] = (0, utils_1.b58DecodeAndCheckPrefix)(key, [
|
|
54
|
+
utils_1.PrefixV2.Secp256k1EncryptedSecretKey,
|
|
55
|
+
utils_1.PrefixV2.P256EncryptedSecretKey,
|
|
56
|
+
utils_1.PrefixV2.Secp256k1SecretKey,
|
|
57
|
+
utils_1.PrefixV2.P256SecretKey,
|
|
58
|
+
]);
|
|
59
|
+
const [decKey, curve] = (() => {
|
|
60
|
+
switch (prefix) {
|
|
61
|
+
case utils_1.PrefixV2.Secp256k1EncryptedSecretKey:
|
|
62
|
+
case utils_1.PrefixV2.P256EncryptedSecretKey:
|
|
63
|
+
if (decrypt === undefined) {
|
|
64
|
+
throw new Error('decryption function is not provided');
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
return [
|
|
68
|
+
decrypt(keyData),
|
|
69
|
+
prefix === utils_1.PrefixV2.Secp256k1EncryptedSecretKey ? 'secp256k1' : 'p256',
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
case utils_1.PrefixV2.Secp256k1SecretKey:
|
|
73
|
+
return [keyData, 'secp256k1'];
|
|
74
|
+
default:
|
|
75
|
+
return [keyData, 'p256'];
|
|
76
|
+
}
|
|
77
|
+
})();
|
|
78
|
+
super(new elliptic_1.default.ec(curve).keyFromPrivate(decKey));
|
|
59
79
|
}
|
|
60
80
|
/**
|
|
61
81
|
*
|
|
62
82
|
* @param bytes Bytes to sign
|
|
63
83
|
* @param bytesHash Blake2b hash of the bytes to sign
|
|
64
84
|
*/
|
|
65
|
-
sign(bytes
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
85
|
+
sign(bytes) {
|
|
86
|
+
const hash = (0, blake2b_1.hash)(bytes, 32);
|
|
87
|
+
const sig = this.keyPair.sign(hash, { canonical: true });
|
|
88
|
+
const signature = new Uint8Array(64);
|
|
89
|
+
const r = sig.r.toArray();
|
|
90
|
+
const s = sig.s.toArray();
|
|
91
|
+
signature.set(r, 32 - r.length);
|
|
92
|
+
signature.set(s, 64 - s.length);
|
|
93
|
+
return {
|
|
94
|
+
rawSignature: signature,
|
|
95
|
+
sig: (0, utils_1.b58Encode)(signature, utils_1.PrefixV2.GenericSignature),
|
|
96
|
+
prefixSig: (0, utils_1.b58Encode)(signature, pref[this.curve()].sig),
|
|
97
|
+
};
|
|
78
98
|
}
|
|
79
99
|
/**
|
|
80
100
|
* @returns Encoded public key
|
|
81
101
|
*/
|
|
82
102
|
publicKey() {
|
|
83
|
-
return
|
|
84
|
-
return (0, utils_1.b58cencode)(this._publicKey, pref[this.curve].pk);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* @returns Encoded public key hash
|
|
89
|
-
*/
|
|
90
|
-
publicKeyHash() {
|
|
91
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
-
return (0, utils_1.b58cencode)((0, blake2b_1.hash)(new Uint8Array(this._publicKey), 20), pref[this.curve].pkh);
|
|
93
|
-
});
|
|
103
|
+
return new ECPublicKey(this.keyPair.ec.keyFromPublic(this.keyPair));
|
|
94
104
|
}
|
|
95
105
|
/**
|
|
96
106
|
* @returns Encoded private key
|
|
97
107
|
*/
|
|
98
108
|
secretKey() {
|
|
99
|
-
return
|
|
100
|
-
const key = this._key;
|
|
101
|
-
return (0, utils_1.b58cencode)(key, pref[this.curve].sk);
|
|
102
|
-
});
|
|
109
|
+
return (0, utils_1.b58Encode)(new Uint8Array(this.keyPair.getPrivate().toArray()), pref[this.curve()].sk);
|
|
103
110
|
}
|
|
104
111
|
}
|
|
105
112
|
exports.ECKey = ECKey;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
function isKeyPair(src) {
|
|
114
|
+
return src instanceof key_1.default;
|
|
115
|
+
}
|
|
116
|
+
class ECPublicKey extends ECKeyBase {
|
|
117
|
+
constructor(src, curve) {
|
|
118
|
+
const key = (() => {
|
|
119
|
+
if (isKeyPair(src)) {
|
|
120
|
+
return src;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const [key, crv] = (() => {
|
|
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);
|
|
139
|
+
}
|
|
140
|
+
})();
|
|
141
|
+
super(key);
|
|
142
|
+
}
|
|
143
|
+
compare(other) {
|
|
144
|
+
if (other instanceof ECPublicKey) {
|
|
145
|
+
if (this.curve() === other.curve()) {
|
|
146
|
+
const compress = this.curve() === 'secp256k1';
|
|
147
|
+
return (0, utils_1.compareArrays)(this.bytes(compress), other.bytes(compress));
|
|
148
|
+
}
|
|
149
|
+
else if (this.curve() === 'secp256k1') {
|
|
150
|
+
return -1;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
throw new utils_1.InvalidPublicKeyError('ECDSA key expected');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
hash() {
|
|
161
|
+
const key = this.bytes();
|
|
162
|
+
return (0, utils_1.b58Encode)((0, blake2b_1.hash)(key, 20), pref[this.curve()].pkh);
|
|
163
|
+
}
|
|
164
|
+
bytes(compress = true) {
|
|
165
|
+
return new Uint8Array(this.keyPair.getPublic(compress, 'array'));
|
|
166
|
+
}
|
|
167
|
+
toProtocol() {
|
|
168
|
+
const key = this.bytes();
|
|
169
|
+
const res = new Uint8Array(key.length + 1);
|
|
170
|
+
res[0] = pref[this.curve()].tag;
|
|
171
|
+
res.set(key, 1);
|
|
172
|
+
return res;
|
|
173
|
+
}
|
|
174
|
+
toString() {
|
|
175
|
+
const key = this.bytes();
|
|
176
|
+
return (0, utils_1.b58Encode)(key, pref[this.curve()].pk);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
exports.ECPublicKey = ECPublicKey;
|
package/dist/lib/ed-key.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
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;
|
|
10
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 _EdKey_keyPair, _EdPublicKey_key;
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
15
|
+
exports.EdPublicKey = exports.EdKey = void 0;
|
|
13
16
|
const blake2b_1 = require("@stablelib/blake2b");
|
|
14
17
|
const ed25519_1 = require("@stablelib/ed25519");
|
|
15
18
|
const utils_1 = require("@taquito/utils");
|
|
16
|
-
const typedarray_to_buffer_1 = require("typedarray-to-buffer");
|
|
17
|
-
const core_1 = require("@taquito/core");
|
|
18
19
|
/**
|
|
19
20
|
* @description Provide signing logic for ed25519 curve based key (tz1)
|
|
20
21
|
*/
|
|
21
|
-
class
|
|
22
|
+
class EdKey {
|
|
22
23
|
/**
|
|
23
24
|
*
|
|
24
25
|
* @param key Encoded private key
|
|
@@ -26,77 +27,96 @@ class Tz1 {
|
|
|
26
27
|
* @param decrypt Decrypt function
|
|
27
28
|
* @throws {@link InvalidKeyError}
|
|
28
29
|
*/
|
|
29
|
-
constructor(key,
|
|
30
|
-
this
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
constructor(key, decrypt) {
|
|
31
|
+
_EdKey_keyPair.set(this, void 0);
|
|
32
|
+
const tmp = (0, utils_1.b58DecodeAndCheckPrefix)(key, [
|
|
33
|
+
utils_1.PrefixV2.Ed25519SecretKey,
|
|
34
|
+
utils_1.PrefixV2.Ed25519EncryptedSeed,
|
|
35
|
+
utils_1.PrefixV2.Ed25519Seed,
|
|
36
|
+
]);
|
|
37
|
+
let [keyData] = tmp;
|
|
38
|
+
const [, prefix] = tmp;
|
|
39
|
+
if (prefix === utils_1.PrefixV2.Ed25519SecretKey) {
|
|
40
|
+
__classPrivateFieldSet(this, _EdKey_keyPair, {
|
|
41
|
+
secretKey: keyData,
|
|
42
|
+
publicKey: keyData.slice(32),
|
|
43
|
+
}, "f");
|
|
39
44
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
else {
|
|
46
|
+
if (prefix === utils_1.PrefixV2.Ed25519EncryptedSeed) {
|
|
47
|
+
if (decrypt !== undefined) {
|
|
48
|
+
keyData = decrypt(keyData);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
throw new Error('decryption function is not provided');
|
|
52
|
+
}
|
|
48
53
|
}
|
|
49
|
-
|
|
50
|
-
}
|
|
54
|
+
__classPrivateFieldSet(this, _EdKey_keyPair, (0, ed25519_1.generateKeyPairFromSeed)(keyData), "f");
|
|
55
|
+
}
|
|
51
56
|
}
|
|
52
57
|
/**
|
|
53
58
|
*
|
|
54
59
|
* @param bytes Bytes to sign
|
|
55
60
|
* @param bytesHash Blake2b hash of the bytes to sign
|
|
56
61
|
*/
|
|
57
|
-
sign(bytes
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
sig: (0, utils_1.b58cencode)(signature, utils_1.prefix.sig),
|
|
66
|
-
prefixSig: (0, utils_1.b58cencode)(signature, utils_1.prefix.edsig),
|
|
67
|
-
sbytes,
|
|
68
|
-
};
|
|
69
|
-
});
|
|
62
|
+
sign(bytes) {
|
|
63
|
+
const hash = (0, blake2b_1.hash)(bytes, 32);
|
|
64
|
+
const signature = (0, ed25519_1.sign)(__classPrivateFieldGet(this, _EdKey_keyPair, "f").secretKey, hash);
|
|
65
|
+
return {
|
|
66
|
+
rawSignature: signature,
|
|
67
|
+
sig: (0, utils_1.b58Encode)(signature, utils_1.PrefixV2.GenericSignature),
|
|
68
|
+
prefixSig: (0, utils_1.b58Encode)(signature, utils_1.PrefixV2.Ed25519Signature),
|
|
69
|
+
};
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
72
|
* @returns Encoded public key
|
|
73
73
|
*/
|
|
74
74
|
publicKey() {
|
|
75
|
-
return
|
|
76
|
-
yield this.isInit;
|
|
77
|
-
return (0, utils_1.b58cencode)(this._publicKey, utils_1.prefix['edpk']);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* @returns Encoded public key hash
|
|
82
|
-
*/
|
|
83
|
-
publicKeyHash() {
|
|
84
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
yield this.isInit;
|
|
86
|
-
return (0, utils_1.b58cencode)((0, blake2b_1.hash)(new Uint8Array(this._publicKey), 20), utils_1.prefix.tz1);
|
|
87
|
-
});
|
|
75
|
+
return new EdPublicKey(__classPrivateFieldGet(this, _EdKey_keyPair, "f").publicKey);
|
|
88
76
|
}
|
|
89
77
|
/**
|
|
90
78
|
* @returns Encoded private key
|
|
91
79
|
*/
|
|
92
80
|
secretKey() {
|
|
93
|
-
return
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
81
|
+
return (0, utils_1.b58Encode)(__classPrivateFieldGet(this, _EdKey_keyPair, "f").secretKey, utils_1.PrefixV2.Ed25519SecretKey);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.EdKey = EdKey;
|
|
85
|
+
_EdKey_keyPair = new WeakMap();
|
|
86
|
+
class EdPublicKey {
|
|
87
|
+
constructor(src) {
|
|
88
|
+
_EdPublicKey_key.set(this, void 0);
|
|
89
|
+
if (typeof src === 'string') {
|
|
90
|
+
const [key, _] = (0, utils_1.b58DecodeAndCheckPrefix)(src, [utils_1.PrefixV2.Ed25519PublicKey]);
|
|
91
|
+
__classPrivateFieldSet(this, _EdPublicKey_key, key, "f");
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
__classPrivateFieldSet(this, _EdPublicKey_key, src, "f");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
compare(other) {
|
|
98
|
+
if (other instanceof EdPublicKey) {
|
|
99
|
+
return (0, utils_1.compareArrays)(this.bytes(), other.bytes());
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
throw new utils_1.InvalidPublicKeyError('EdDSA key expected');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
hash() {
|
|
106
|
+
return (0, utils_1.b58Encode)((0, blake2b_1.hash)(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), 20), utils_1.PrefixV2.Ed25519PublicKeyHash);
|
|
107
|
+
}
|
|
108
|
+
bytes() {
|
|
109
|
+
return __classPrivateFieldGet(this, _EdPublicKey_key, "f");
|
|
110
|
+
}
|
|
111
|
+
toProtocol() {
|
|
112
|
+
const res = new Uint8Array(__classPrivateFieldGet(this, _EdPublicKey_key, "f").length + 1);
|
|
113
|
+
res[0] = 0;
|
|
114
|
+
res.set(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), 1);
|
|
115
|
+
return res;
|
|
116
|
+
}
|
|
117
|
+
toString() {
|
|
118
|
+
return (0, utils_1.b58Encode)(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), utils_1.PrefixV2.Ed25519PublicKey);
|
|
100
119
|
}
|
|
101
120
|
}
|
|
102
|
-
exports.
|
|
121
|
+
exports.EdPublicKey = EdPublicKey;
|
|
122
|
+
_EdPublicKey_key = new WeakMap();
|
package/dist/lib/helpers.js
CHANGED
|
@@ -21,16 +21,16 @@ const generateSecretKey = (seed, derivationPath, curve) => {
|
|
|
21
21
|
switch (curve) {
|
|
22
22
|
case 'ed25519': {
|
|
23
23
|
node = ed25519_1.PrivateKey.fromSeed(seed).derivePath(path);
|
|
24
|
-
const sk = (0, utils_1.
|
|
24
|
+
const sk = (0, utils_1.b58Encode)(node.seed().slice(0, 32), utils_1.PrefixV2.Ed25519Seed);
|
|
25
25
|
return sk;
|
|
26
26
|
}
|
|
27
27
|
case 'secp256k1':
|
|
28
28
|
case 'p256': {
|
|
29
|
-
const prefixType = curve === 'secp256k1' ? utils_1.
|
|
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
32
|
const uint8arr = new Uint8Array(privKey.keyPair.getPrivate().toArray());
|
|
33
|
-
const sk = (0, utils_1.
|
|
33
|
+
const sk = (0, utils_1.b58Encode)(uint8arr, prefixType);
|
|
34
34
|
return sk;
|
|
35
35
|
}
|
|
36
36
|
case 'bip25519': {
|