@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.
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@stablelib/nacl'), require('@stablelib/blake2b'), require('@taquito/utils'), require('typedarray-to-buffer'), require('@stablelib/ed25519'), require('@taquito/core'), require('elliptic'), require('pbkdf2'), require('bip39'), require('@stablelib/hmac'), require('@stablelib/sha512'), require('bn.js')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@stablelib/nacl', '@stablelib/blake2b', '@taquito/utils', 'typedarray-to-buffer', '@stablelib/ed25519', '@taquito/core', 'elliptic', 'pbkdf2', 'bip39', '@stablelib/hmac', '@stablelib/sha512', 'bn.js'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoSigner = {}, global.nacl, global.blake2b, global.utils, global.toBuffer, global.ed25519$1, global.core, global.elliptic, global.pbkdf2, global.Bip39, global.hmac, global.sha512, global.BN));
5
- })(this, (function (exports, nacl, blake2b, utils, toBuffer, ed25519$1, core, elliptic, pbkdf2, Bip39, hmac, sha512, BN) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@stablelib/nacl'), require('@taquito/utils'), require('typedarray-to-buffer'), require('@stablelib/blake2b'), require('@stablelib/ed25519'), require('elliptic'), require('elliptic/lib/elliptic/ec/key'), require('@taquito/core'), require('pbkdf2'), require('bip39'), require('@stablelib/hmac'), require('@stablelib/sha512'), require('bn.js'), require('@noble/curves/bls12-381')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@stablelib/nacl', '@taquito/utils', 'typedarray-to-buffer', '@stablelib/blake2b', '@stablelib/ed25519', 'elliptic', 'elliptic/lib/elliptic/ec/key', '@taquito/core', 'pbkdf2', 'bip39', '@stablelib/hmac', '@stablelib/sha512', 'bn.js', '@noble/curves/bls12-381'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoSigner = {}, global.nacl, global.utils, global.toBuffer, global.blake2b, global.ed25519$1, global.elliptic, global.KeyPair, global.core, global.pbkdf2, global.Bip39, global.hmac, global.sha512, global.BN, global.bls12381));
5
+ })(this, (function (exports, nacl, utils, toBuffer, blake2b, ed25519$1, elliptic, KeyPair, core, pbkdf2, Bip39, hmac, sha512, BN, bls12381) { 'use strict';
6
6
 
7
7
  function _interopNamespaceDefault(e) {
8
8
  var n = Object.create(null);
@@ -50,15 +50,29 @@
50
50
  });
51
51
  }
52
52
 
53
+ function __classPrivateFieldGet(receiver, state, kind, f) {
54
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
55
+ 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");
56
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
57
+ }
58
+
59
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
60
+ if (kind === "m") throw new TypeError("Private method is not writable");
61
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
62
+ 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");
63
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
64
+ }
65
+
53
66
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
54
67
  var e = new Error(message);
55
68
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
56
69
  };
57
70
 
71
+ var _EdKey_keyPair, _EdPublicKey_key;
58
72
  /**
59
73
  * @description Provide signing logic for ed25519 curve based key (tz1)
60
74
  */
61
- class Tz1 {
75
+ class EdKey {
62
76
  /**
63
77
  *
64
78
  * @param key Encoded private key
@@ -66,188 +80,97 @@
66
80
  * @param decrypt Decrypt function
67
81
  * @throws {@link InvalidKeyError}
68
82
  */
69
- constructor(key, encrypted, decrypt) {
70
- this.key = key;
71
- const keyPrefix = key.substring(0, encrypted ? 5 : 4);
72
- if (!utils.isValidPrefix(keyPrefix)) {
73
- throw new core.InvalidKeyError(`${utils.invalidDetail(utils.ValidationResult.NO_PREFIX_MATCHED)} expecting either '${utils.Prefix.EDESK}' or '${utils.Prefix.EDSK}'.`);
83
+ constructor(key, decrypt) {
84
+ _EdKey_keyPair.set(this, void 0);
85
+ const tmp = utils.b58DecodeAndCheckPrefix(key, [
86
+ utils.PrefixV2.Ed25519SecretKey,
87
+ utils.PrefixV2.Ed25519EncryptedSeed,
88
+ utils.PrefixV2.Ed25519Seed,
89
+ ]);
90
+ let [keyData] = tmp;
91
+ const [, prefix] = tmp;
92
+ if (prefix === utils.PrefixV2.Ed25519SecretKey) {
93
+ __classPrivateFieldSet(this, _EdKey_keyPair, {
94
+ secretKey: keyData,
95
+ publicKey: keyData.slice(32),
96
+ }, "f");
74
97
  }
75
- this._key = decrypt(utils.b58cdecode(this.key, utils.prefix[keyPrefix]));
76
- this._publicKey = this._key.slice(32);
77
- if (!this._key) {
78
- throw new core.InvalidKeyError('unable to decode');
79
- }
80
- this.isInit = this.init();
81
- }
82
- init() {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- if (this._key.length !== 64) {
85
- const { publicKey, secretKey } = ed25519$1.generateKeyPairFromSeed(new Uint8Array(this._key));
86
- this._publicKey = publicKey;
87
- this._key = secretKey;
98
+ else {
99
+ if (prefix === utils.PrefixV2.Ed25519EncryptedSeed) {
100
+ if (decrypt !== undefined) {
101
+ keyData = decrypt(keyData);
102
+ }
103
+ else {
104
+ throw new Error('decryption function is not provided');
105
+ }
88
106
  }
89
- return true;
90
- });
107
+ __classPrivateFieldSet(this, _EdKey_keyPair, ed25519$1.generateKeyPairFromSeed(keyData), "f");
108
+ }
91
109
  }
92
110
  /**
93
111
  *
94
112
  * @param bytes Bytes to sign
95
113
  * @param bytesHash Blake2b hash of the bytes to sign
96
114
  */
97
- sign(bytes, bytesHash) {
98
- return __awaiter(this, void 0, void 0, function* () {
99
- yield this.isInit;
100
- const signature = ed25519$1.sign(new Uint8Array(this._key), new Uint8Array(bytesHash));
101
- const signatureBuffer = toBuffer(signature);
102
- const sbytes = bytes + utils.buf2hex(signatureBuffer);
103
- return {
104
- bytes,
105
- sig: utils.b58cencode(signature, utils.prefix.sig),
106
- prefixSig: utils.b58cencode(signature, utils.prefix.edsig),
107
- sbytes,
108
- };
109
- });
115
+ sign(bytes) {
116
+ const hash = blake2b.hash(bytes, 32);
117
+ const signature = ed25519$1.sign(__classPrivateFieldGet(this, _EdKey_keyPair, "f").secretKey, hash);
118
+ return {
119
+ rawSignature: signature,
120
+ sig: utils.b58Encode(signature, utils.PrefixV2.GenericSignature),
121
+ prefixSig: utils.b58Encode(signature, utils.PrefixV2.Ed25519Signature),
122
+ };
110
123
  }
111
124
  /**
112
125
  * @returns Encoded public key
113
126
  */
114
127
  publicKey() {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- yield this.isInit;
117
- return utils.b58cencode(this._publicKey, utils.prefix['edpk']);
118
- });
119
- }
120
- /**
121
- * @returns Encoded public key hash
122
- */
123
- publicKeyHash() {
124
- return __awaiter(this, void 0, void 0, function* () {
125
- yield this.isInit;
126
- return utils.b58cencode(blake2b.hash(new Uint8Array(this._publicKey), 20), utils.prefix.tz1);
127
- });
128
+ return new EdPublicKey(__classPrivateFieldGet(this, _EdKey_keyPair, "f").publicKey);
128
129
  }
129
130
  /**
130
131
  * @returns Encoded private key
131
132
  */
132
133
  secretKey() {
133
- return __awaiter(this, void 0, void 0, function* () {
134
- yield this.isInit;
135
- let key = this._key;
136
- const { secretKey } = ed25519$1.generateKeyPairFromSeed(new Uint8Array(key).slice(0, 32));
137
- key = toBuffer(secretKey);
138
- return utils.b58cencode(key, utils.prefix[`edsk`]);
139
- });
134
+ return utils.b58Encode(__classPrivateFieldGet(this, _EdKey_keyPair, "f").secretKey, utils.PrefixV2.Ed25519SecretKey);
140
135
  }
141
136
  }
142
-
143
- const pref = {
144
- p256: {
145
- pk: utils.prefix['p2pk'],
146
- sk: utils.prefix['p2sk'],
147
- pkh: utils.prefix.tz3,
148
- sig: utils.prefix.p2sig,
149
- },
150
- secp256k1: {
151
- pk: utils.prefix['sppk'],
152
- sk: utils.prefix['spsk'],
153
- pkh: utils.prefix.tz2,
154
- sig: utils.prefix.spsig,
155
- },
156
- };
157
- /**
158
- * @description Provide signing logic for elliptic curve based key (tz2, tz3)
159
- */
160
- class ECKey {
161
- /**
162
- *
163
- * @param curve Curve to use with the key
164
- * @param key Encoded private key
165
- * @param encrypted Is the private key encrypted
166
- * @param decrypt Decrypt function
167
- * @throws {@link InvalidKeyError}
168
- */
169
- constructor(curve, key, encrypted, decrypt) {
170
- this.curve = curve;
171
- this.key = key;
172
- const keyPrefix = key.substring(0, encrypted ? 5 : 4);
173
- if (!utils.isValidPrefix(keyPrefix)) {
174
- throw new core.InvalidKeyError(utils.invalidDetail(utils.ValidationResult.NO_PREFIX_MATCHED) +
175
- ` expecting one of the following prefix '${utils.Prefix.SPSK}', '${utils.Prefix.SPESK}', '${utils.Prefix.P2SK}' or '${utils.Prefix.P2ESK}'.`);
176
- }
177
- this._key = decrypt(utils.b58cdecode(this.key, utils.prefix[keyPrefix]));
178
- const keyPair = new elliptic.ec(this.curve).keyFromPrivate(this._key);
179
- const keyPairY = keyPair.getPublic().getY().toArray();
180
- const parityByte = keyPairY.length < 32 ? keyPairY[keyPairY.length - 1] : keyPairY[31];
181
- const pref = parityByte % 2 ? 3 : 2;
182
- const pad = new Array(32).fill(0);
183
- this._publicKey = toBuffer(new Uint8Array([pref].concat(pad.concat(keyPair.getPublic().getX().toArray()).slice(-32))));
137
+ _EdKey_keyPair = new WeakMap();
138
+ class EdPublicKey {
139
+ constructor(src) {
140
+ _EdPublicKey_key.set(this, void 0);
141
+ if (typeof src === 'string') {
142
+ const [key, _] = utils.b58DecodeAndCheckPrefix(src, [utils.PrefixV2.Ed25519PublicKey]);
143
+ __classPrivateFieldSet(this, _EdPublicKey_key, key, "f");
144
+ }
145
+ else {
146
+ __classPrivateFieldSet(this, _EdPublicKey_key, src, "f");
147
+ }
184
148
  }
185
- /**
186
- *
187
- * @param bytes Bytes to sign
188
- * @param bytesHash Blake2b hash of the bytes to sign
189
- */
190
- sign(bytes, bytesHash) {
191
- return __awaiter(this, void 0, void 0, function* () {
192
- const key = new elliptic.ec(this.curve).keyFromPrivate(this._key);
193
- const sig = key.sign(bytesHash, { canonical: true });
194
- const signature = sig.r.toString('hex', 64) + sig.s.toString('hex', 64);
195
- const sbytes = bytes + signature;
196
- return {
197
- bytes,
198
- sig: utils.b58cencode(signature, utils.prefix.sig),
199
- prefixSig: utils.b58cencode(signature, pref[this.curve].sig),
200
- sbytes,
201
- };
202
- });
149
+ compare(other) {
150
+ if (other instanceof EdPublicKey) {
151
+ return utils.compareArrays(this.bytes(), other.bytes());
152
+ }
153
+ else {
154
+ throw new utils.InvalidPublicKeyError('EdDSA key expected');
155
+ }
203
156
  }
204
- /**
205
- * @returns Encoded public key
206
- */
207
- publicKey() {
208
- return __awaiter(this, void 0, void 0, function* () {
209
- return utils.b58cencode(this._publicKey, pref[this.curve].pk);
210
- });
157
+ hash() {
158
+ return utils.b58Encode(blake2b.hash(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), 20), utils.PrefixV2.Ed25519PublicKeyHash);
211
159
  }
212
- /**
213
- * @returns Encoded public key hash
214
- */
215
- publicKeyHash() {
216
- return __awaiter(this, void 0, void 0, function* () {
217
- return utils.b58cencode(blake2b.hash(new Uint8Array(this._publicKey), 20), pref[this.curve].pkh);
218
- });
160
+ bytes() {
161
+ return __classPrivateFieldGet(this, _EdPublicKey_key, "f");
219
162
  }
220
- /**
221
- * @returns Encoded private key
222
- */
223
- secretKey() {
224
- return __awaiter(this, void 0, void 0, function* () {
225
- const key = this._key;
226
- return utils.b58cencode(key, pref[this.curve].sk);
227
- });
163
+ toProtocol() {
164
+ const res = new Uint8Array(__classPrivateFieldGet(this, _EdPublicKey_key, "f").length + 1);
165
+ res[0] = 0;
166
+ res.set(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), 1);
167
+ return res;
228
168
  }
229
- }
230
- /**
231
- * @description Tz3 key class using the p256 curve
232
- */
233
- const Tz3 = ECKey.bind(null, 'p256');
234
- /**
235
- * @description Tz2 key class using the secp256k1 curve
236
- */
237
- const Tz2 = ECKey.bind(null, 'secp256k1');
238
-
239
- function parseHex(s) {
240
- const res = [];
241
- for (let i = 0; i < s.length; i += 2) {
242
- const ss = s.slice(i, i + 2);
243
- const x = parseInt(ss, 16);
244
- if (Number.isNaN(x)) {
245
- throw new core.InvalidHexStringError(ss);
246
- }
247
- res.push(x);
169
+ toString() {
170
+ return utils.b58Encode(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), utils.PrefixV2.Ed25519PublicKey);
248
171
  }
249
- return new Uint8Array(res);
250
172
  }
173
+ _EdPublicKey_key = new WeakMap();
251
174
 
252
175
  /**
253
176
  * @category Error
@@ -318,6 +241,189 @@
318
241
  }
319
242
  }
320
243
 
244
+ const pref = {
245
+ p256: {
246
+ pk: utils.PrefixV2.P256PublicKey,
247
+ sk: utils.PrefixV2.P256SecretKey,
248
+ pkh: utils.PrefixV2.P256PublicKeyHash,
249
+ sig: utils.PrefixV2.P256Signature,
250
+ tag: 2,
251
+ },
252
+ secp256k1: {
253
+ pk: utils.PrefixV2.Secp256k1PublicKey,
254
+ sk: utils.PrefixV2.Secp256k1SecretKey,
255
+ pkh: utils.PrefixV2.Secp256k1PublicKeyHash,
256
+ sig: utils.PrefixV2.Secp256k1Signature,
257
+ tag: 1,
258
+ },
259
+ };
260
+ class ECKeyBase {
261
+ constructor(keyPair) {
262
+ this.keyPair = keyPair;
263
+ }
264
+ curve() {
265
+ switch (this.keyPair.ec.curve) {
266
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
267
+ case elliptic.curves.secp256k1.curve:
268
+ return 'secp256k1';
269
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
270
+ case elliptic.curves.p256.curve:
271
+ return 'p256';
272
+ default:
273
+ throw new InvalidCurveError('unknown curve');
274
+ }
275
+ }
276
+ }
277
+ /**
278
+ * @description Provide signing logic for elliptic curve based key (tz2, tz3)
279
+ */
280
+ class ECKey extends ECKeyBase {
281
+ /**
282
+ *
283
+ * @param key Encoded private key
284
+ * @param decrypt Decrypt function
285
+ * @throws {@link InvalidKeyError}
286
+ */
287
+ constructor(key, decrypt) {
288
+ const [keyData, prefix] = utils.b58DecodeAndCheckPrefix(key, [
289
+ utils.PrefixV2.Secp256k1EncryptedSecretKey,
290
+ utils.PrefixV2.P256EncryptedSecretKey,
291
+ utils.PrefixV2.Secp256k1SecretKey,
292
+ utils.PrefixV2.P256SecretKey,
293
+ ]);
294
+ const [decKey, curve] = (() => {
295
+ switch (prefix) {
296
+ case utils.PrefixV2.Secp256k1EncryptedSecretKey:
297
+ case utils.PrefixV2.P256EncryptedSecretKey:
298
+ if (decrypt === undefined) {
299
+ throw new Error('decryption function is not provided');
300
+ }
301
+ else {
302
+ return [
303
+ decrypt(keyData),
304
+ prefix === utils.PrefixV2.Secp256k1EncryptedSecretKey ? 'secp256k1' : 'p256',
305
+ ];
306
+ }
307
+ case utils.PrefixV2.Secp256k1SecretKey:
308
+ return [keyData, 'secp256k1'];
309
+ default:
310
+ return [keyData, 'p256'];
311
+ }
312
+ })();
313
+ super(new elliptic.ec(curve).keyFromPrivate(decKey));
314
+ }
315
+ /**
316
+ *
317
+ * @param bytes Bytes to sign
318
+ * @param bytesHash Blake2b hash of the bytes to sign
319
+ */
320
+ sign(bytes) {
321
+ const hash = blake2b.hash(bytes, 32);
322
+ const sig = this.keyPair.sign(hash, { canonical: true });
323
+ const signature = new Uint8Array(64);
324
+ const r = sig.r.toArray();
325
+ const s = sig.s.toArray();
326
+ signature.set(r, 32 - r.length);
327
+ signature.set(s, 64 - s.length);
328
+ return {
329
+ rawSignature: signature,
330
+ sig: utils.b58Encode(signature, utils.PrefixV2.GenericSignature),
331
+ prefixSig: utils.b58Encode(signature, pref[this.curve()].sig),
332
+ };
333
+ }
334
+ /**
335
+ * @returns Encoded public key
336
+ */
337
+ publicKey() {
338
+ return new ECPublicKey(this.keyPair.ec.keyFromPublic(this.keyPair));
339
+ }
340
+ /**
341
+ * @returns Encoded private key
342
+ */
343
+ secretKey() {
344
+ return utils.b58Encode(new Uint8Array(this.keyPair.getPrivate().toArray()), pref[this.curve()].sk);
345
+ }
346
+ }
347
+ function isKeyPair(src) {
348
+ return src instanceof KeyPair;
349
+ }
350
+ class ECPublicKey extends ECKeyBase {
351
+ constructor(src, curve) {
352
+ const key = (() => {
353
+ if (isKeyPair(src)) {
354
+ return src;
355
+ }
356
+ else {
357
+ const [key, crv] = (() => {
358
+ if (typeof src === 'string') {
359
+ const [key, pre] = utils.b58DecodeAndCheckPrefix(src, [
360
+ utils.PrefixV2.Secp256k1PublicKey,
361
+ utils.PrefixV2.P256PublicKey,
362
+ ]);
363
+ return [key, pre === utils.PrefixV2.Secp256k1PublicKey ? 'secp256k1' : 'p256'];
364
+ }
365
+ else if (curve !== undefined) {
366
+ return [src, curve];
367
+ }
368
+ else {
369
+ throw new InvalidCurveError('missing curve type');
370
+ }
371
+ })();
372
+ return new elliptic.ec(crv).keyFromPublic(key);
373
+ }
374
+ })();
375
+ super(key);
376
+ }
377
+ compare(other) {
378
+ if (other instanceof ECPublicKey) {
379
+ if (this.curve() === other.curve()) {
380
+ const compress = this.curve() === 'secp256k1';
381
+ return utils.compareArrays(this.bytes(compress), other.bytes(compress));
382
+ }
383
+ else if (this.curve() === 'secp256k1') {
384
+ return -1;
385
+ }
386
+ else {
387
+ return 1;
388
+ }
389
+ }
390
+ else {
391
+ throw new utils.InvalidPublicKeyError('ECDSA key expected');
392
+ }
393
+ }
394
+ hash() {
395
+ const key = this.bytes();
396
+ return utils.b58Encode(blake2b.hash(key, 20), pref[this.curve()].pkh);
397
+ }
398
+ bytes(compress = true) {
399
+ return new Uint8Array(this.keyPair.getPublic(compress, 'array'));
400
+ }
401
+ toProtocol() {
402
+ const key = this.bytes();
403
+ const res = new Uint8Array(key.length + 1);
404
+ res[0] = pref[this.curve()].tag;
405
+ res.set(key, 1);
406
+ return res;
407
+ }
408
+ toString() {
409
+ const key = this.bytes();
410
+ return utils.b58Encode(key, pref[this.curve()].pk);
411
+ }
412
+ }
413
+
414
+ function parseHex(s) {
415
+ const res = [];
416
+ for (let i = 0; i < s.length; i += 2) {
417
+ const ss = s.slice(i, i + 2);
418
+ const x = parseInt(ss, 16);
419
+ if (Number.isNaN(x)) {
420
+ throw new core.InvalidHexStringError(ss);
421
+ }
422
+ res.push(x);
423
+ }
424
+ return new Uint8Array(res);
425
+ }
426
+
321
427
  /* eslint-disable @typescript-eslint/no-this-alias */
322
428
  const seedKey = {
323
429
  p256: 'Nist256p1 seed',
@@ -565,16 +671,16 @@
565
671
  switch (curve) {
566
672
  case 'ed25519': {
567
673
  node = PrivateKey.fromSeed(seed).derivePath(path);
568
- const sk = utils.b58cencode(node.seed().slice(0, 32), utils.prefix.edsk2);
674
+ const sk = utils.b58Encode(node.seed().slice(0, 32), utils.PrefixV2.Ed25519Seed);
569
675
  return sk;
570
676
  }
571
677
  case 'secp256k1':
572
678
  case 'p256': {
573
- const prefixType = curve === 'secp256k1' ? utils.prefix.spsk : utils.prefix.p2sk;
679
+ const prefixType = curve === 'secp256k1' ? utils.PrefixV2.Secp256k1SecretKey : utils.PrefixV2.P256SecretKey;
574
680
  let privKey = PrivateKey$1.fromSeed(seed, curve);
575
681
  privKey = privKey.derivePath(path);
576
682
  const uint8arr = new Uint8Array(privKey.keyPair.getPrivate().toArray());
577
- const sk = utils.b58cencode(uint8arr, prefixType);
683
+ const sk = utils.b58Encode(uint8arr, prefixType);
578
684
  return sk;
579
685
  }
580
686
  case 'bip25519': {
@@ -586,6 +692,96 @@
586
692
  }
587
693
  };
588
694
 
695
+ function isPOP(k) {
696
+ return 'provePossession' in k;
697
+ }
698
+
699
+ var _BLSKey_key, _BLSKey_publicKey, _BLSPublicKey_key;
700
+ const bls = bls12381.bls12_381.longSignatures; // AKA MinPK
701
+ class BLSKey {
702
+ constructor(key, decrypt) {
703
+ _BLSKey_key.set(this, void 0);
704
+ _BLSKey_publicKey.set(this, void 0);
705
+ const tmp = utils.b58DecodeAndCheckPrefix(key, [
706
+ utils.PrefixV2.BLS12_381EncryptedSecretKey,
707
+ utils.PrefixV2.BLS12_381SecretKey,
708
+ ]);
709
+ let [keyData] = tmp;
710
+ const [, prefix] = tmp;
711
+ if (prefix === utils.PrefixV2.BLS12_381EncryptedSecretKey) {
712
+ if (decrypt !== undefined) {
713
+ keyData = decrypt(keyData);
714
+ }
715
+ else {
716
+ throw new Error('decryption function is not provided');
717
+ }
718
+ }
719
+ __classPrivateFieldSet(this, _BLSKey_key, keyData, "f");
720
+ __classPrivateFieldSet(this, _BLSKey_publicKey, bls.getPublicKey(this.sk()).toBytes(), "f");
721
+ }
722
+ sk() {
723
+ return new Uint8Array(__classPrivateFieldGet(this, _BLSKey_key, "f")).reverse();
724
+ }
725
+ signDst(message, dst) {
726
+ const point = bls.hash(message, dst);
727
+ const sig = bls.sign(point, this.sk()).toBytes();
728
+ return {
729
+ rawSignature: sig,
730
+ sig: utils.b58Encode(sig, utils.PrefixV2.GenericSignature),
731
+ prefixSig: utils.b58Encode(sig, utils.PrefixV2.BLS12_381Signature),
732
+ };
733
+ }
734
+ sign(message) {
735
+ return this.signDst(message, utils.BLS12_381_DST);
736
+ }
737
+ provePossession() {
738
+ return this.signDst(__classPrivateFieldGet(this, _BLSKey_publicKey, "f"), utils.POP_DST);
739
+ }
740
+ publicKey() {
741
+ return new BLSPublicKey(__classPrivateFieldGet(this, _BLSKey_publicKey, "f"));
742
+ }
743
+ secretKey() {
744
+ return utils.b58Encode(__classPrivateFieldGet(this, _BLSKey_key, "f"), utils.PrefixV2.BLS12_381SecretKey);
745
+ }
746
+ }
747
+ _BLSKey_key = new WeakMap(), _BLSKey_publicKey = new WeakMap();
748
+ class BLSPublicKey {
749
+ constructor(src) {
750
+ _BLSPublicKey_key.set(this, void 0);
751
+ if (typeof src === 'string') {
752
+ const [key, _] = utils.b58DecodeAndCheckPrefix(src, [utils.PrefixV2.BLS12_381PublicKey]);
753
+ __classPrivateFieldSet(this, _BLSPublicKey_key, key, "f");
754
+ }
755
+ else {
756
+ __classPrivateFieldSet(this, _BLSPublicKey_key, src, "f");
757
+ }
758
+ }
759
+ compare(other) {
760
+ if (other instanceof BLSPublicKey) {
761
+ return utils.compareArrays(this.bytes(), other.bytes());
762
+ }
763
+ else {
764
+ throw new utils.InvalidPublicKeyError('BLS key expected');
765
+ }
766
+ }
767
+ hash() {
768
+ return utils.b58Encode(blake2b.hash(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), 20), utils.PrefixV2.BLS12_381PublicKeyHash);
769
+ }
770
+ bytes() {
771
+ return __classPrivateFieldGet(this, _BLSPublicKey_key, "f");
772
+ }
773
+ toProtocol() {
774
+ const res = new Uint8Array(__classPrivateFieldGet(this, _BLSPublicKey_key, "f").length + 1);
775
+ res[0] = 3;
776
+ res.set(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), 1);
777
+ return res;
778
+ }
779
+ toString() {
780
+ return utils.b58Encode(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), utils.PrefixV2.BLS12_381PublicKey);
781
+ }
782
+ }
783
+ _BLSPublicKey_key = new WeakMap();
784
+
589
785
  /**
590
786
  *
591
787
  * @description Import a key to sign operation with the side-effect of setting the Tezos instance to use the InMemorySigner provider
@@ -627,10 +823,11 @@
627
823
 
628
824
  // IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
629
825
  const VERSION = {
630
- "commitHash": "7af2138a9e5c5b230c4b4c726f35c2f2e67b721c",
631
- "version": "23.0.0-beta.0"
826
+ "commitHash": "9065acc1b41ec205a49e64b54ef89f50bafa6210",
827
+ "version": "23.0.0"
632
828
  };
633
829
 
830
+ var _InMemorySigner_key;
634
831
  /**
635
832
  * @description A local implementation of the signer. Will represent a Tezos account and be able to produce signature in its behalf
636
833
  *
@@ -643,7 +840,7 @@
643
840
  throw new InvalidMnemonicError(mnemonic);
644
841
  }
645
842
  const seed = Bip39__namespace.mnemonicToSeedSync(mnemonic, `${email}${password}`);
646
- const key = utils.b58cencode(seed.slice(0, 32), utils.prefix.edsk2);
843
+ const key = utils.b58Encode(seed.subarray(0, 32), utils.PrefixV2.Ed25519Seed);
647
844
  return new InMemorySigner(key);
648
845
  }
649
846
  static fromSecretKey(key, passphrase) {
@@ -679,34 +876,63 @@
679
876
  *
680
877
  */
681
878
  constructor(key, passphrase) {
682
- const encrypted = key.substring(2, 3) === 'e';
683
- let decrypt = (k) => k;
879
+ _InMemorySigner_key.set(this, void 0);
880
+ const keyPrefixes = [
881
+ utils.PrefixV2.Ed25519EncryptedSeed,
882
+ utils.PrefixV2.Ed25519Seed,
883
+ utils.PrefixV2.Ed25519SecretKey,
884
+ utils.PrefixV2.Secp256k1EncryptedSecretKey,
885
+ utils.PrefixV2.Secp256k1SecretKey,
886
+ utils.PrefixV2.P256EncryptedSecretKey,
887
+ utils.PrefixV2.P256SecretKey,
888
+ utils.PrefixV2.BLS12_381EncryptedSecretKey,
889
+ utils.PrefixV2.BLS12_381SecretKey,
890
+ ];
891
+ const pre = (() => {
892
+ try {
893
+ const [, pre] = utils.b58DecodeAndCheckPrefix(key, keyPrefixes);
894
+ return pre;
895
+ }
896
+ catch (_a) {
897
+ throw new core.InvalidKeyError(`Invalid private key, expecting one of the following prefixes '${keyPrefixes}'.`);
898
+ }
899
+ })();
900
+ const encrypted = pre === utils.PrefixV2.Ed25519EncryptedSeed ||
901
+ pre === utils.PrefixV2.Secp256k1EncryptedSecretKey ||
902
+ pre === utils.PrefixV2.P256EncryptedSecretKey ||
903
+ pre === utils.PrefixV2.BLS12_381EncryptedSecretKey;
904
+ let decrypt;
684
905
  if (encrypted) {
685
906
  if (!passphrase) {
686
907
  throw new InvalidPassphraseError('No passphrase provided to decrypt encrypted key');
687
908
  }
688
- decrypt = (constructedKey) => {
689
- const salt = toBuffer(constructedKey.slice(0, 8));
690
- const encryptedSk = constructedKey.slice(8);
909
+ decrypt = (data) => {
910
+ const salt = toBuffer(data.slice(0, 8));
911
+ const encryptedSk = data.slice(8);
691
912
  const encryptionKey = pbkdf2.pbkdf2Sync(passphrase, salt, 32768, 32, 'sha512');
692
- return nacl.openSecretBox(new Uint8Array(encryptionKey), new Uint8Array(24), new Uint8Array(encryptedSk));
913
+ const res = nacl.openSecretBox(new Uint8Array(encryptionKey), new Uint8Array(24), new Uint8Array(encryptedSk));
914
+ if (!res) {
915
+ throw new Error("can't decrypt secret key");
916
+ }
917
+ return res;
693
918
  };
694
919
  }
695
- switch (key.substring(0, 4)) {
696
- case 'edes':
697
- case 'edsk':
698
- this._key = new Tz1(key, encrypted, decrypt);
920
+ switch (pre) {
921
+ case utils.PrefixV2.Ed25519EncryptedSeed:
922
+ case utils.PrefixV2.Ed25519Seed:
923
+ case utils.PrefixV2.Ed25519SecretKey:
924
+ __classPrivateFieldSet(this, _InMemorySigner_key, new EdKey(key, decrypt), "f");
699
925
  break;
700
- case 'spsk':
701
- case 'spes':
702
- this._key = new Tz2(key, encrypted, decrypt);
926
+ case utils.PrefixV2.Secp256k1EncryptedSecretKey:
927
+ case utils.PrefixV2.Secp256k1SecretKey:
928
+ case utils.PrefixV2.P256EncryptedSecretKey:
929
+ case utils.PrefixV2.P256SecretKey:
930
+ __classPrivateFieldSet(this, _InMemorySigner_key, new ECKey(key, decrypt), "f");
703
931
  break;
704
- case 'p2sk':
705
- case 'p2es':
706
- this._key = new Tz3(key, encrypted, decrypt);
932
+ case utils.PrefixV2.BLS12_381EncryptedSecretKey:
933
+ case utils.PrefixV2.BLS12_381SecretKey:
934
+ __classPrivateFieldSet(this, _InMemorySigner_key, new BLSKey(key, decrypt), "f");
707
935
  break;
708
- default:
709
- throw new core.InvalidKeyError(`${utils.invalidDetail(utils.ValidationResult.NO_PREFIX_MATCHED)} expecting one of the following '${utils.Prefix.EDESK}', '${utils.Prefix.EDSK}', '${utils.Prefix.SPSK}', '${utils.Prefix.SPESK}', '${utils.Prefix.P2SK}' or '${utils.Prefix.P2ESK}'.`);
710
936
  }
711
937
  }
712
938
  /**
@@ -714,39 +940,70 @@
714
940
  * @param bytes Bytes to sign
715
941
  * @param watermark Watermark to append to the bytes
716
942
  */
717
- sign(bytes, watermark) {
943
+ sign(message, watermark) {
944
+ return __awaiter(this, void 0, void 0, function* () {
945
+ const msg = typeof message == 'string' ? utils.hex2buf(message) : message;
946
+ const watermarkMsg = watermark !== undefined ? utils.mergebuf(watermark, msg) : msg;
947
+ const { rawSignature, sig: signature, prefixSig: prefixedSignature, } = yield __classPrivateFieldGet(this, _InMemorySigner_key, "f").sign(watermarkMsg);
948
+ return {
949
+ bytes: utils.buf2hex(msg),
950
+ sig: signature,
951
+ prefixSig: prefixedSignature,
952
+ sbytes: utils.buf2hex(utils.mergebuf(msg,
953
+ // 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
954
+ isPOP(__classPrivateFieldGet(this, _InMemorySigner_key, "f")) ? utils.mergebuf(new Uint8Array([255, 3]), rawSignature) : rawSignature)),
955
+ };
956
+ });
957
+ }
958
+ provePossession() {
718
959
  return __awaiter(this, void 0, void 0, function* () {
719
- let bb = utils.hex2buf(bytes);
720
- if (typeof watermark !== 'undefined') {
721
- bb = utils.mergebuf(watermark, bb);
960
+ if (isPOP(__classPrivateFieldGet(this, _InMemorySigner_key, "f"))) {
961
+ return __classPrivateFieldGet(this, _InMemorySigner_key, "f").provePossession();
962
+ }
963
+ else {
964
+ throw new core.ProhibitedActionError('Only BLS keys can prove possession');
722
965
  }
723
- const bytesHash = blake2b.hash(bb, 32);
724
- return this._key.sign(bytes, bytesHash);
725
966
  });
726
967
  }
968
+ get canProvePossession() {
969
+ return isPOP(__classPrivateFieldGet(this, _InMemorySigner_key, "f"));
970
+ }
727
971
  /**
728
972
  * @returns Encoded public key
729
973
  */
730
974
  publicKey() {
731
- return __awaiter(this, void 0, void 0, function* () {
732
- return this._key.publicKey();
733
- });
975
+ return Promise.resolve(String(__classPrivateFieldGet(this, _InMemorySigner_key, "f").publicKey()));
734
976
  }
735
977
  /**
736
978
  * @returns Encoded public key hash
737
979
  */
738
980
  publicKeyHash() {
739
- return __awaiter(this, void 0, void 0, function* () {
740
- return this._key.publicKeyHash();
741
- });
981
+ return Promise.resolve(__classPrivateFieldGet(this, _InMemorySigner_key, "f").publicKey().hash());
742
982
  }
743
983
  /**
744
984
  * @returns Encoded private key
745
985
  */
746
986
  secretKey() {
747
- return __awaiter(this, void 0, void 0, function* () {
748
- return this._key.secretKey();
749
- });
987
+ return Promise.resolve(__classPrivateFieldGet(this, _InMemorySigner_key, "f").secretKey());
988
+ }
989
+ }
990
+ _InMemorySigner_key = new WeakMap();
991
+ function publicKeyFromString(src) {
992
+ const [keyData, pre] = utils.b58DecodeAndCheckPrefix(src, [
993
+ utils.PrefixV2.Ed25519PublicKey,
994
+ utils.PrefixV2.Secp256k1PublicKey,
995
+ utils.PrefixV2.P256PublicKey,
996
+ utils.PrefixV2.BLS12_381PublicKey,
997
+ ]);
998
+ switch (pre) {
999
+ case utils.PrefixV2.Ed25519PublicKey:
1000
+ return new EdPublicKey(keyData);
1001
+ case utils.PrefixV2.Secp256k1PublicKey:
1002
+ return new ECPublicKey(keyData, 'secp256k1');
1003
+ case utils.PrefixV2.P256PublicKey:
1004
+ return new ECPublicKey(keyData, 'p256');
1005
+ case utils.PrefixV2.BLS12_381PublicKey:
1006
+ return new BLSPublicKey(keyData);
750
1007
  }
751
1008
  }
752
1009
 
@@ -759,6 +1016,7 @@
759
1016
  exports.VERSION = VERSION;
760
1017
  exports.generateSecretKey = generateSecretKey;
761
1018
  exports.importKey = importKey;
1019
+ exports.publicKeyFromString = publicKeyFromString;
762
1020
 
763
1021
  }));
764
1022
  //# sourceMappingURL=taquito-signer.umd.js.map