@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.
@@ -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();
@@ -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.Tz2 = exports.Tz3 = exports.ECKey = void 0;
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 core_1 = require("@taquito/core");
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.prefix['p2pk'],
21
- sk: utils_1.prefix['p2sk'],
22
- pkh: utils_1.prefix.tz3,
23
- sig: utils_1.prefix.p2sig,
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.prefix['sppk'],
27
- sk: utils_1.prefix['spsk'],
28
- pkh: utils_1.prefix.tz2,
29
- sig: utils_1.prefix.spsig,
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(curve, key, encrypted, decrypt) {
45
- this.curve = curve;
46
- this.key = key;
47
- const keyPrefix = key.substring(0, encrypted ? 5 : 4);
48
- if (!(0, utils_1.isValidPrefix)(keyPrefix)) {
49
- throw new core_1.InvalidKeyError((0, utils_1.invalidDetail)(utils_1.ValidationResult.NO_PREFIX_MATCHED) +
50
- ` expecting one of the following prefix '${utils_1.Prefix.SPSK}', '${utils_1.Prefix.SPESK}', '${utils_1.Prefix.P2SK}' or '${utils_1.Prefix.P2ESK}'.`);
51
- }
52
- this._key = decrypt((0, utils_1.b58cdecode)(this.key, utils_1.prefix[keyPrefix]));
53
- const keyPair = new elliptic_1.default.ec(this.curve).keyFromPrivate(this._key);
54
- const keyPairY = keyPair.getPublic().getY().toArray();
55
- const parityByte = keyPairY.length < 32 ? keyPairY[keyPairY.length - 1] : keyPairY[31];
56
- const pref = parityByte % 2 ? 3 : 2;
57
- const pad = new Array(32).fill(0);
58
- this._publicKey = (0, typedarray_to_buffer_1.default)(new Uint8Array([pref].concat(pad.concat(keyPair.getPublic().getX().toArray()).slice(-32))));
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, bytesHash) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- const key = new elliptic_1.default.ec(this.curve).keyFromPrivate(this._key);
68
- const sig = key.sign(bytesHash, { canonical: true });
69
- const signature = sig.r.toString('hex', 64) + sig.s.toString('hex', 64);
70
- const sbytes = bytes + signature;
71
- return {
72
- bytes,
73
- sig: (0, utils_1.b58cencode)(signature, utils_1.prefix.sig),
74
- prefixSig: (0, utils_1.b58cencode)(signature, pref[this.curve].sig),
75
- sbytes,
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 __awaiter(this, void 0, void 0, function* () {
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 __awaiter(this, void 0, void 0, function* () {
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
- * @description Tz3 key class using the p256 curve
108
- */
109
- exports.Tz3 = ECKey.bind(null, 'p256');
110
- /**
111
- * @description Tz2 key class using the secp256k1 curve
112
- */
113
- exports.Tz2 = ECKey.bind(null, 'secp256k1');
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;
@@ -1,24 +1,25 @@
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
- });
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.Tz1 = void 0;
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 Tz1 {
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, encrypted, decrypt) {
30
- this.key = key;
31
- const keyPrefix = key.substring(0, encrypted ? 5 : 4);
32
- if (!(0, utils_1.isValidPrefix)(keyPrefix)) {
33
- throw new core_1.InvalidKeyError(`${(0, utils_1.invalidDetail)(utils_1.ValidationResult.NO_PREFIX_MATCHED)} expecting either '${utils_1.Prefix.EDESK}' or '${utils_1.Prefix.EDSK}'.`);
34
- }
35
- this._key = decrypt((0, utils_1.b58cdecode)(this.key, utils_1.prefix[keyPrefix]));
36
- this._publicKey = this._key.slice(32);
37
- if (!this._key) {
38
- throw new core_1.InvalidKeyError('unable to decode');
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
- this.isInit = this.init();
41
- }
42
- init() {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- if (this._key.length !== 64) {
45
- const { publicKey, secretKey } = (0, ed25519_1.generateKeyPairFromSeed)(new Uint8Array(this._key));
46
- this._publicKey = publicKey;
47
- this._key = secretKey;
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
- return true;
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, bytesHash) {
58
- return __awaiter(this, void 0, void 0, function* () {
59
- yield this.isInit;
60
- const signature = (0, ed25519_1.sign)(new Uint8Array(this._key), new Uint8Array(bytesHash));
61
- const signatureBuffer = (0, typedarray_to_buffer_1.default)(signature);
62
- const sbytes = bytes + (0, utils_1.buf2hex)(signatureBuffer);
63
- return {
64
- bytes,
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 __awaiter(this, void 0, void 0, function* () {
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 __awaiter(this, void 0, void 0, function* () {
94
- yield this.isInit;
95
- let key = this._key;
96
- const { secretKey } = (0, ed25519_1.generateKeyPairFromSeed)(new Uint8Array(key).slice(0, 32));
97
- key = (0, typedarray_to_buffer_1.default)(secretKey);
98
- return (0, utils_1.b58cencode)(key, utils_1.prefix[`edsk`]);
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.Tz1 = Tz1;
121
+ exports.EdPublicKey = EdPublicKey;
122
+ _EdPublicKey_key = new WeakMap();
@@ -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.b58cencode)(node.seed().slice(0, 32), utils_1.prefix.edsk2);
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.prefix.spsk : utils_1.prefix.p2sk;
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.b58cencode)(uint8arr, prefixType);
33
+ const sk = (0, utils_1.b58Encode)(uint8arr, prefixType);
34
34
  return sk;
35
35
  }
36
36
  case 'bip25519': {
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPOP = isPOP;
4
+ function isPOP(k) {
5
+ return 'provePossession' in k;
6
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPOP = isPOP;
4
+ function isPOP(k) {
5
+ return 'provePossession' in k;
6
+ }