@prosopo/keyring 2.6.4 → 2.8.7

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/README.md +8 -0
  3. package/dist/accounts/getPair.js +35 -39
  4. package/dist/accounts/index.js +10 -3
  5. package/dist/accounts/mnemonic.js +16 -15
  6. package/dist/accounts/testAccounts.js +44 -0
  7. package/dist/cjs/accounts/getPair.cjs +6 -12
  8. package/dist/cjs/accounts/index.cjs +3 -1
  9. package/dist/cjs/accounts/mnemonic.cjs +7 -9
  10. package/dist/cjs/accounts/testAccounts.cjs +44 -0
  11. package/dist/cjs/index.cjs +11 -1
  12. package/dist/cjs/keyring/index.cjs +7 -0
  13. package/dist/cjs/keyring/keyring.cjs +271 -0
  14. package/dist/cjs/keyring/pairs.cjs +28 -0
  15. package/dist/cjs/pair/decode.cjs +32 -0
  16. package/dist/cjs/pair/defaults.cjs +31 -0
  17. package/dist/cjs/pair/encode.cjs +18 -0
  18. package/dist/cjs/pair/index.cjs +156 -0
  19. package/dist/cjs/pair/toJson.cjs +14 -0
  20. package/dist/index.js +19 -2
  21. package/dist/keyring/index.js +7 -0
  22. package/dist/keyring/keyring.js +271 -0
  23. package/dist/keyring/pairs.js +28 -0
  24. package/dist/pair/decode.js +32 -0
  25. package/dist/pair/defaults.js +31 -0
  26. package/dist/pair/encode.js +18 -0
  27. package/dist/pair/index.js +156 -0
  28. package/dist/pair/toJson.js +14 -0
  29. package/package.json +28 -18
  30. package/vite.cjs.config.ts +4 -1
  31. package/vite.esm.config.ts +20 -0
  32. package/vite.test.config.ts +32 -0
  33. package/dist/accounts/getPair.d.ts +0 -6
  34. package/dist/accounts/getPair.d.ts.map +0 -1
  35. package/dist/accounts/getPair.js.map +0 -1
  36. package/dist/accounts/index.d.ts +0 -3
  37. package/dist/accounts/index.d.ts.map +0 -1
  38. package/dist/accounts/index.js.map +0 -1
  39. package/dist/accounts/mnemonic.d.ts +0 -5
  40. package/dist/accounts/mnemonic.d.ts.map +0 -1
  41. package/dist/accounts/mnemonic.js.map +0 -1
  42. package/dist/index.d.ts +0 -2
  43. package/dist/index.d.ts.map +0 -1
  44. package/dist/index.js.map +0 -1
@@ -0,0 +1,271 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const util = require("@polkadot/util");
4
+ const hex = require("@polkadot/util/hex");
5
+ const is = require("@polkadot/util/is");
6
+ const utilCrypto = require("@prosopo/util-crypto");
7
+ const index = require("../pair/index.cjs");
8
+ const pairs = require("./pairs.cjs");
9
+ const DEV_PHRASE = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
10
+ const PairFromSeed = {
11
+ sr25519: (seed) => utilCrypto.sr25519FromSeed(seed),
12
+ ed25519: () => {
13
+ throw new Error("Not Implemented");
14
+ },
15
+ ecdsa: () => {
16
+ throw new Error("Not Implemented");
17
+ },
18
+ ethereum: () => {
19
+ throw new Error("Not Implemented");
20
+ }
21
+ };
22
+ function pairToPublic({ publicKey }) {
23
+ return publicKey;
24
+ }
25
+ class Keyring {
26
+ constructor(options = {}) {
27
+ this.decodeAddress = utilCrypto.decodeAddress;
28
+ this.encodeAddress = (address, ss58Format) => {
29
+ return utilCrypto.encodeAddress(address, ss58Format ?? this.#ss58);
30
+ };
31
+ options.type = options.type || "sr25519";
32
+ if (!["sr25519"].includes(options.type || "undefined")) {
33
+ throw new Error(
34
+ `Expected a keyring type of either 'sr25519', found '${options.type || "unknown"}`
35
+ );
36
+ }
37
+ this.#pairs = new pairs.Pairs();
38
+ this.#ss58 = options.ss58Format;
39
+ this.#type = options.type;
40
+ }
41
+ #pairs;
42
+ #type;
43
+ #ss58;
44
+ /**
45
+ * @description retrieve the pairs (alias for getPairs)
46
+ */
47
+ get pairs() {
48
+ return this.getPairs();
49
+ }
50
+ /**
51
+ * @description retrieve the publicKeys (alias for getPublicKeys)
52
+ */
53
+ get publicKeys() {
54
+ return this.getPublicKeys();
55
+ }
56
+ /**
57
+ * @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
58
+ */
59
+ get type() {
60
+ return this.#type;
61
+ }
62
+ /**
63
+ * @name addPair
64
+ * @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
65
+ */
66
+ addPair(pair) {
67
+ return this.#pairs.add(pair);
68
+ }
69
+ /**
70
+ * @name addFromAddress
71
+ * @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
72
+ * @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
73
+ * the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
74
+ * of an account backup), and then generates a keyring pair from them that it passes to
75
+ * `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
76
+ */
77
+ addFromAddress(address, meta = {}, encoded = null, type = this.type, ignoreChecksum, encType) {
78
+ const publicKey = this.decodeAddress(address, ignoreChecksum);
79
+ return this.addPair(
80
+ index.createPair(
81
+ { toSS58: this.encodeAddress, type },
82
+ { publicKey, secretKey: new Uint8Array() },
83
+ meta,
84
+ encoded,
85
+ encType
86
+ )
87
+ );
88
+ }
89
+ /**
90
+ * @name addFromJson
91
+ * @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
92
+ * @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
93
+ * of an account backup), and then generates a keyring pair from it that it passes to
94
+ * `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
95
+ */
96
+ addFromJson(json, ignoreChecksum) {
97
+ return this.addPair(this.createFromJson(json, ignoreChecksum));
98
+ }
99
+ /**
100
+ * @name addFromMnemonic
101
+ * @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
102
+ * @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
103
+ * argument and a metadata argument that contains account information (that may be obtained from the json file
104
+ * of an account backup), and then generates a keyring pair from it that it passes to
105
+ * `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
106
+ */
107
+ addFromMnemonic(mnemonic, meta = {}, type = this.type) {
108
+ return this.addFromUri(mnemonic, meta, type);
109
+ }
110
+ /**
111
+ * @name addFromPair
112
+ * @summary Stores an account created from an explicit publicKey/secreteKey combination
113
+ */
114
+ addFromPair(pair, meta = {}, type = this.type) {
115
+ return this.addPair(this.createFromPair(pair, meta, type));
116
+ }
117
+ /**
118
+ * @name addFromSeed
119
+ * @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
120
+ * @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
121
+ * Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
122
+ * `addPair` to store in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
123
+ */
124
+ addFromSeed(seed, meta = {}, type = this.type) {
125
+ return this.addPair(
126
+ index.createPair(
127
+ { toSS58: this.encodeAddress, type },
128
+ PairFromSeed[type](seed),
129
+ meta,
130
+ null
131
+ )
132
+ );
133
+ }
134
+ /**
135
+ * @name addFromUri
136
+ * @summary Creates an account via an suri
137
+ * @description Extracts the phrase, path and password from a SURI format for specifying secret keys `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` maybe repeated and mixed). The secret can be a hex string, mnemonic phrase or a string (to be padded)
138
+ */
139
+ addFromUri(suri, meta = {}, type = this.type) {
140
+ return this.addPair(this.createFromUri(suri, meta, type));
141
+ }
142
+ /**
143
+ * @name createFromJson
144
+ * @description Creates a pair from a JSON keyfile
145
+ */
146
+ createFromJson({
147
+ address,
148
+ encoded,
149
+ encoding: { content, type, version },
150
+ meta
151
+ }, ignoreChecksum) {
152
+ if (version === "3" && content[0] !== "pkcs8") {
153
+ throw new Error(
154
+ `Unable to decode non-pkcs8 type, [${content.join(",")}] found}`
155
+ );
156
+ }
157
+ const cryptoType = version === "0" || !Array.isArray(content) ? this.type : content[1];
158
+ if (!cryptoType) {
159
+ throw new Error("cryptoType is undefined");
160
+ }
161
+ const encType = !Array.isArray(type) ? [type] : type;
162
+ if (!["sr25519"].includes(cryptoType)) {
163
+ throw new Error(`Unknown crypto type ${cryptoType}`);
164
+ }
165
+ const publicKey = is.isHex(address) ? hex.hexToU8a(address) : this.decodeAddress(address, ignoreChecksum);
166
+ const decoded = is.isHex(encoded) ? hex.hexToU8a(encoded) : utilCrypto.base64Decode(encoded);
167
+ return index.createPair(
168
+ { toSS58: this.encodeAddress, type: cryptoType },
169
+ { publicKey, secretKey: new Uint8Array() },
170
+ meta,
171
+ decoded,
172
+ encType
173
+ );
174
+ }
175
+ /**
176
+ * @name createFromPair
177
+ * @summary Creates a pair from an explicit publicKey/secreteKey combination
178
+ */
179
+ createFromPair(pair, meta = {}, type = this.type) {
180
+ return index.createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
181
+ }
182
+ /**
183
+ * @name createFromUri
184
+ * @summary Creates a Keypair from an suri
185
+ * @description This creates a pair from the suri, but does not add it to the keyring
186
+ */
187
+ createFromUri(_suri, meta = {}, type = this.type) {
188
+ const suri = _suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri;
189
+ const { derivePath, password, path, phrase } = utilCrypto.keyExtractSuri(suri);
190
+ let seed;
191
+ const isPhraseHex = is.isHex(phrase, 256);
192
+ if (isPhraseHex) {
193
+ seed = hex.hexToU8a(phrase);
194
+ } else {
195
+ const parts = phrase.split(" ");
196
+ if ([12, 15, 18, 21, 24].includes(parts.length)) {
197
+ seed = type === "ethereum" ? (() => {
198
+ throw new Error(
199
+ "Not implemented - Prosopo Keyring supports sr25519 only"
200
+ );
201
+ })() : utilCrypto.mnemonicToMiniSecret(phrase, password);
202
+ } else {
203
+ if (phrase.length > 32) {
204
+ throw new Error(
205
+ "specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes"
206
+ );
207
+ }
208
+ seed = util.stringToU8a(phrase.padEnd(32));
209
+ }
210
+ }
211
+ const derived = utilCrypto.keyFromPath(PairFromSeed[type](seed), path, type);
212
+ return index.createPair(
213
+ { toSS58: this.encodeAddress, type },
214
+ derived,
215
+ meta,
216
+ null
217
+ );
218
+ }
219
+ /**
220
+ * @name getPair
221
+ * @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
222
+ * @description Returns a keyring pair value from the keyring pair dictionary by performing
223
+ * a key lookup using the provided account address or public key (after decoding it).
224
+ */
225
+ getPair(address) {
226
+ return this.#pairs.get(address);
227
+ }
228
+ /**
229
+ * @name getPairs
230
+ * @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
231
+ * @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
232
+ */
233
+ getPairs() {
234
+ return this.#pairs.all();
235
+ }
236
+ /**
237
+ * @name getPublicKeys
238
+ * @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
239
+ * @description Returns an array list of all the public keys associated with each of the keyring pair values that are stored in the keyring pair dictionary.
240
+ */
241
+ getPublicKeys() {
242
+ return this.#pairs.all().map(pairToPublic);
243
+ }
244
+ /**
245
+ * @name removePair
246
+ * @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
247
+ */
248
+ removePair(address) {
249
+ this.#pairs.remove(address);
250
+ }
251
+ /**
252
+ * @name setSS58Format;
253
+ * @description Sets the ss58 format for the keyring
254
+ */
255
+ setSS58Format(ss58) {
256
+ this.#ss58 = ss58;
257
+ }
258
+ /**
259
+ * @name toJson
260
+ * @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
261
+ * @description Returns a JSON object containing the metadata associated with an account
262
+ * when valid address or public key and when the account passphrase is provided if the account secret
263
+ * is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
264
+ * may backup their account to a JSON file that contains this information.
265
+ */
266
+ toJson(address, passphrase) {
267
+ return this.#pairs.get(address).toJson(passphrase);
268
+ }
269
+ }
270
+ exports.DEV_PHRASE = DEV_PHRASE;
271
+ exports.Keyring = Keyring;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const util$1 = require("@polkadot/util");
4
+ const util = require("@prosopo/util");
5
+ const utilCrypto = require("@prosopo/util-crypto");
6
+ class Pairs {
7
+ #map = {};
8
+ add(pair) {
9
+ this.#map[utilCrypto.decodeAddress(pair.address).toString()] = pair;
10
+ return pair;
11
+ }
12
+ all() {
13
+ return Object.values(this.#map);
14
+ }
15
+ get(address) {
16
+ const pair = this.#map[utilCrypto.decodeAddress(address).toString()];
17
+ if (!pair) {
18
+ throw new Error(
19
+ `Unable to retrieve keypair '${util$1.isU8a(address) || util$1.isHex(address) ? util.u8aToHex(util$1.u8aToU8a(address)) : address}'`
20
+ );
21
+ }
22
+ return pair;
23
+ }
24
+ remove(address) {
25
+ delete this.#map[utilCrypto.decodeAddress(address).toString()];
26
+ }
27
+ }
28
+ exports.Pairs = Pairs;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const util = require("@polkadot/util");
4
+ const utilCrypto = require("@prosopo/util-crypto");
5
+ const defaults = require("./defaults.cjs");
6
+ const SEED_OFFSET = defaults.PAIR_HDR.length;
7
+ function decodePair(passphrase, encrypted, _encType) {
8
+ const encType = Array.isArray(_encType) || _encType === void 0 ? _encType : [_encType];
9
+ const decrypted = utilCrypto.jsonDecryptData(encrypted, passphrase, encType);
10
+ const header = decrypted.subarray(0, defaults.PAIR_HDR.length);
11
+ if (!util.u8aEq(header, defaults.PAIR_HDR)) {
12
+ throw new Error("Invalid encoding header found in body");
13
+ }
14
+ let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + defaults.SEC_LENGTH);
15
+ let divOffset = SEED_OFFSET + defaults.SEC_LENGTH;
16
+ let divider = decrypted.subarray(divOffset, divOffset + defaults.PAIR_DIV.length);
17
+ if (!util.u8aEq(divider, defaults.PAIR_DIV)) {
18
+ divOffset = SEED_OFFSET + defaults.SEED_LENGTH;
19
+ secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
20
+ divider = decrypted.subarray(divOffset, divOffset + defaults.PAIR_DIV.length);
21
+ if (!util.u8aEq(divider, defaults.PAIR_DIV)) {
22
+ throw new Error("Invalid encoding divider found in body");
23
+ }
24
+ }
25
+ const pubOffset = divOffset + defaults.PAIR_DIV.length;
26
+ const publicKey = decrypted.subarray(pubOffset, pubOffset + defaults.PUB_LENGTH);
27
+ return {
28
+ publicKey,
29
+ secretKey
30
+ };
31
+ }
32
+ exports.decodePair = decodePair;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const PAIR_DIV = new Uint8Array([161, 35, 3, 33, 0]);
4
+ const PAIR_HDR = new Uint8Array([
5
+ 48,
6
+ 83,
7
+ 2,
8
+ 1,
9
+ 1,
10
+ 48,
11
+ 5,
12
+ 6,
13
+ 3,
14
+ 43,
15
+ 101,
16
+ 112,
17
+ 4,
18
+ 34,
19
+ 4,
20
+ 32
21
+ ]);
22
+ const PUB_LENGTH = 32;
23
+ const SALT_LENGTH = 32;
24
+ const SEC_LENGTH = 64;
25
+ const SEED_LENGTH = 32;
26
+ exports.PAIR_DIV = PAIR_DIV;
27
+ exports.PAIR_HDR = PAIR_HDR;
28
+ exports.PUB_LENGTH = PUB_LENGTH;
29
+ exports.SALT_LENGTH = SALT_LENGTH;
30
+ exports.SEC_LENGTH = SEC_LENGTH;
31
+ exports.SEED_LENGTH = SEED_LENGTH;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const util = require("@polkadot/util");
4
+ const utilCrypto = require("@prosopo/util-crypto");
5
+ const defaults = require("./defaults.cjs");
6
+ function encodePair({ publicKey, secretKey }, passphrase) {
7
+ if (!secretKey) {
8
+ throw new Error("Expected a valid secretKey to be passed to encode");
9
+ }
10
+ const encoded = util.u8aConcat(defaults.PAIR_HDR, secretKey, defaults.PAIR_DIV, publicKey);
11
+ if (!passphrase) {
12
+ return encoded;
13
+ }
14
+ const { params, password, salt } = utilCrypto.scryptEncode(passphrase);
15
+ const { encrypted, nonce } = utilCrypto.naclEncrypt(encoded, password.subarray(0, 32));
16
+ return util.u8aConcat(utilCrypto.scryptToU8a(salt, params), nonce, encrypted);
17
+ }
18
+ exports.encodePair = encodePair;
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const util = require("@polkadot/util");
4
+ const utilCrypto = require("@prosopo/util-crypto");
5
+ const decode = require("./decode.cjs");
6
+ const encode = require("./encode.cjs");
7
+ const toJson = require("./toJson.cjs");
8
+ const SIG_TYPE_NONE = new Uint8Array([]);
9
+ const TYPE_FROM_SEED = {
10
+ sr25519: utilCrypto.sr25519FromSeed,
11
+ ed25519: () => {
12
+ throw new Error("Not Implemented");
13
+ },
14
+ ecdsa: () => {
15
+ throw new Error("Not Implemented");
16
+ },
17
+ ethereum: () => {
18
+ throw new Error("Not Implemented");
19
+ }
20
+ };
21
+ const TYPE_PREFIX = {
22
+ ecdsa: new Uint8Array([2]),
23
+ ed25519: new Uint8Array([0]),
24
+ ethereum: new Uint8Array([2]),
25
+ sr25519: new Uint8Array([1])
26
+ };
27
+ const TYPE_SIGNATURE = {
28
+ sr25519: utilCrypto.sr25519Sign,
29
+ ed25519: () => {
30
+ throw new Error("Not Implemented");
31
+ },
32
+ ecdsa: () => {
33
+ throw new Error("Not Implemented");
34
+ },
35
+ ethereum: () => {
36
+ throw new Error("Not Implemented");
37
+ }
38
+ };
39
+ const TYPE_ADDRESS = {
40
+ sr25519: (p) => p,
41
+ ed25519: () => {
42
+ throw new Error("Not Implemented");
43
+ },
44
+ ecdsa: () => {
45
+ throw new Error("Not Implemented");
46
+ },
47
+ ethereum: () => {
48
+ throw new Error("Not Implemented");
49
+ }
50
+ };
51
+ function isLocked(secretKey) {
52
+ return !secretKey || util.u8aEmpty(secretKey);
53
+ }
54
+ function vrfHash(proof, context, extra) {
55
+ return utilCrypto.blake2AsU8a(util.u8aConcat(context || "", extra || "", proof));
56
+ }
57
+ function createPair({ toSS58, type }, { publicKey, secretKey }, meta = {}, encoded = null, encTypes) {
58
+ const decodePkcs8 = (passphrase, userEncoded) => {
59
+ const decoded = decode.decodePair(passphrase, userEncoded || encoded, encTypes);
60
+ if (decoded.secretKey.length === 64) {
61
+ publicKey = decoded.publicKey;
62
+ secretKey = decoded.secretKey;
63
+ } else {
64
+ const pair = TYPE_FROM_SEED[type](decoded.secretKey);
65
+ publicKey = pair.publicKey;
66
+ secretKey = pair.secretKey;
67
+ }
68
+ };
69
+ const recode = (passphrase) => {
70
+ isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
71
+ encoded = encode.encodePair({ publicKey, secretKey }, passphrase);
72
+ encTypes = void 0;
73
+ return encoded;
74
+ };
75
+ const encodeAddress = () => {
76
+ const raw = TYPE_ADDRESS[type](publicKey);
77
+ return toSS58(raw);
78
+ };
79
+ return {
80
+ get address() {
81
+ return encodeAddress();
82
+ },
83
+ get addressRaw() {
84
+ return TYPE_ADDRESS[type](publicKey);
85
+ },
86
+ get isLocked() {
87
+ return isLocked(secretKey);
88
+ },
89
+ get meta() {
90
+ return meta;
91
+ },
92
+ get publicKey() {
93
+ return publicKey;
94
+ },
95
+ get type() {
96
+ return type;
97
+ },
98
+ // eslint-disable-next-line sort-keys
99
+ decodePkcs8,
100
+ derive: (suri, meta2) => {
101
+ if (isLocked(secretKey)) {
102
+ throw new Error("Cannot derive on a locked keypair");
103
+ }
104
+ const { path } = utilCrypto.keyExtractPath(suri);
105
+ const derived = utilCrypto.keyFromPath({ publicKey, secretKey }, path, type);
106
+ return createPair({ toSS58, type }, derived, meta2, null);
107
+ },
108
+ encodePkcs8: (passphrase) => {
109
+ return recode(passphrase);
110
+ },
111
+ lock: () => {
112
+ secretKey = new Uint8Array([]);
113
+ },
114
+ setMeta: (additional) => {
115
+ meta = util.objectSpread({}, meta, additional);
116
+ },
117
+ sign: (message, options = {}) => {
118
+ if (isLocked(secretKey)) {
119
+ throw new Error("Cannot sign with a locked key pair");
120
+ }
121
+ return util.u8aConcat(
122
+ options.withType ? TYPE_PREFIX[type] : SIG_TYPE_NONE,
123
+ TYPE_SIGNATURE[type](util.u8aToU8a(message), { publicKey, secretKey })
124
+ );
125
+ },
126
+ toJson: (passphrase) => {
127
+ const address = ["ecdsa", "ethereum"].includes(type) ? publicKey.length === 20 ? util.u8aToHex(publicKey) : util.u8aToHex(utilCrypto.secp256k1Compress(publicKey)) : encodeAddress();
128
+ return toJson.pairToJson(
129
+ type,
130
+ { address, meta },
131
+ recode(passphrase),
132
+ !!passphrase
133
+ );
134
+ },
135
+ unlock: (passphrase) => {
136
+ decodePkcs8(passphrase);
137
+ },
138
+ verify: (message, signature, signerPublic) => {
139
+ return utilCrypto.signatureVerify(
140
+ message,
141
+ signature,
142
+ TYPE_ADDRESS[type](util.u8aToU8a(signerPublic))
143
+ ).isValid;
144
+ },
145
+ vrfSign: (message, context, extra) => {
146
+ if (isLocked(secretKey)) {
147
+ throw new Error("Cannot sign with a locked key pair");
148
+ }
149
+ return utilCrypto.sr25519VrfSign(message, { secretKey }, context, extra);
150
+ },
151
+ vrfVerify: (message, vrfResult, signerPublic, context, extra) => {
152
+ return utilCrypto.sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
153
+ }
154
+ };
155
+ }
156
+ exports.createPair = createPair;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const util = require("@polkadot/util");
4
+ const utilCrypto = require("@prosopo/util-crypto");
5
+ function pairToJson(type, { address, meta }, encoded, isEncrypted) {
6
+ return util.objectSpread(
7
+ utilCrypto.jsonEncryptFormat(encoded, ["pkcs8", type], isEncrypted),
8
+ {
9
+ address,
10
+ meta
11
+ }
12
+ );
13
+ }
14
+ exports.pairToJson = pairToJson;
package/dist/index.js CHANGED
@@ -1,2 +1,19 @@
1
- export * from "./accounts/index.js";
2
- //# sourceMappingURL=index.js.map
1
+ import "./accounts/index.js";
2
+ import "./keyring/index.js";
3
+ import { createPair } from "./pair/index.js";
4
+ import { generateMiniSecret, generateMnemonic } from "./accounts/mnemonic.js";
5
+ import { getPair } from "./accounts/getPair.js";
6
+ import { getDefaultProviders, getDefaultSiteKeys } from "./accounts/testAccounts.js";
7
+ import { DEV_PHRASE, Keyring } from "./keyring/keyring.js";
8
+ import { Pairs } from "./keyring/pairs.js";
9
+ export {
10
+ DEV_PHRASE,
11
+ Keyring,
12
+ Pairs,
13
+ createPair,
14
+ generateMiniSecret,
15
+ generateMnemonic,
16
+ getDefaultProviders,
17
+ getDefaultSiteKeys,
18
+ getPair
19
+ };
@@ -0,0 +1,7 @@
1
+ import { DEV_PHRASE, Keyring } from "./keyring.js";
2
+ import { Pairs } from "./pairs.js";
3
+ export {
4
+ DEV_PHRASE,
5
+ Keyring,
6
+ Pairs
7
+ };