@prosopo/keyring 2.9.59 → 2.9.61

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,271 +1,260 @@
1
- import { stringToU8a } from "@polkadot/util";
2
- import { hexToU8a } from "@polkadot/util/hex";
3
- import { isHex } from "@polkadot/util/is";
4
- import { sr25519FromSeed, decodeAddress, encodeAddress, base64Decode, keyExtractSuri, mnemonicToMiniSecret, keyFromPath } from "@prosopo/util-crypto";
5
1
  import { createPair } from "../pair/index.js";
6
2
  import { Pairs } from "./pairs.js";
7
- const DEV_PHRASE = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
8
- const PairFromSeed = {
9
- sr25519: (seed) => sr25519FromSeed(seed),
10
- ed25519: () => {
11
- throw new Error("Not Implemented");
12
- },
13
- ecdsa: () => {
14
- throw new Error("Not Implemented");
15
- },
16
- ethereum: () => {
17
- throw new Error("Not Implemented");
18
- }
3
+ import { base64Decode, decodeAddress, encodeAddress, keyExtractSuri, keyFromPath, mnemonicToMiniSecret, sr25519FromSeed } from "@prosopo/util-crypto";
4
+ import { stringToU8a } from "@polkadot/util";
5
+ import { hexToU8a } from "@polkadot/util/hex";
6
+ import { isHex as isHex$1 } from "@polkadot/util/is";
7
+ //#region src/keyring/keyring.ts
8
+ var DEV_PHRASE = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
9
+ var PairFromSeed = {
10
+ sr25519: (seed) => sr25519FromSeed(seed),
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
+ }
19
20
  };
20
21
  function pairToPublic({ publicKey }) {
21
- return publicKey;
22
- }
23
- class Keyring {
24
- constructor(options = {}) {
25
- this.decodeAddress = decodeAddress;
26
- this.encodeAddress = (address, ss58Format) => {
27
- return encodeAddress(address, ss58Format ?? this.#ss58);
28
- };
29
- options.type = options.type || "sr25519";
30
- if (!["sr25519"].includes(options.type || "undefined")) {
31
- throw new Error(
32
- `Expected a keyring type of either 'sr25519', found '${options.type || "unknown"}`
33
- );
34
- }
35
- this.#pairs = new Pairs();
36
- this.#ss58 = options.ss58Format;
37
- this.#type = options.type;
38
- }
39
- #pairs;
40
- #type;
41
- #ss58;
42
- /**
43
- * @description retrieve the pairs (alias for getPairs)
44
- */
45
- get pairs() {
46
- return this.getPairs();
47
- }
48
- /**
49
- * @description retrieve the publicKeys (alias for getPublicKeys)
50
- */
51
- get publicKeys() {
52
- return this.getPublicKeys();
53
- }
54
- /**
55
- * @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
56
- */
57
- get type() {
58
- return this.#type;
59
- }
60
- /**
61
- * @name addPair
62
- * @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
63
- */
64
- addPair(pair) {
65
- return this.#pairs.add(pair);
66
- }
67
- /**
68
- * @name addFromAddress
69
- * @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
70
- * @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
71
- * the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
72
- * of an account backup), and then generates a keyring pair from them that it passes to
73
- * `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.
74
- */
75
- addFromAddress(address, meta = {}, encoded = null, type = this.type, ignoreChecksum, encType) {
76
- const publicKey = this.decodeAddress(address, ignoreChecksum);
77
- return this.addPair(
78
- createPair(
79
- { toSS58: this.encodeAddress, type },
80
- { publicKey, secretKey: new Uint8Array() },
81
- meta,
82
- encoded,
83
- encType
84
- )
85
- );
86
- }
87
- /**
88
- * @name addFromJson
89
- * @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
90
- * @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
91
- * of an account backup), and then generates a keyring pair from it that it passes to
92
- * `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.
93
- */
94
- addFromJson(json, ignoreChecksum) {
95
- return this.addPair(this.createFromJson(json, ignoreChecksum));
96
- }
97
- /**
98
- * @name addFromMnemonic
99
- * @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
100
- * @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
101
- * argument and a metadata argument that contains account information (that may be obtained from the json file
102
- * of an account backup), and then generates a keyring pair from it that it passes to
103
- * `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.
104
- */
105
- addFromMnemonic(mnemonic, meta = {}, type = this.type) {
106
- return this.addFromUri(mnemonic, meta, type);
107
- }
108
- /**
109
- * @name addFromPair
110
- * @summary Stores an account created from an explicit publicKey/secreteKey combination
111
- */
112
- addFromPair(pair, meta = {}, type = this.type) {
113
- return this.addPair(this.createFromPair(pair, meta, type));
114
- }
115
- /**
116
- * @name addFromSeed
117
- * @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
118
- * @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
119
- * Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
120
- * `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.
121
- */
122
- addFromSeed(seed, meta = {}, type = this.type) {
123
- return this.addPair(
124
- createPair(
125
- { toSS58: this.encodeAddress, type },
126
- PairFromSeed[type](seed),
127
- meta,
128
- null
129
- )
130
- );
131
- }
132
- /**
133
- * @name addFromUri
134
- * @summary Creates an account via an suri
135
- * @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)
136
- */
137
- addFromUri(suri, meta = {}, type = this.type) {
138
- return this.addPair(this.createFromUri(suri, meta, type));
139
- }
140
- /**
141
- * @name createFromJson
142
- * @description Creates a pair from a JSON keyfile
143
- */
144
- createFromJson({
145
- address,
146
- encoded,
147
- encoding: { content, type, version },
148
- meta
149
- }, ignoreChecksum) {
150
- if (version === "3" && content[0] !== "pkcs8") {
151
- throw new Error(
152
- `Unable to decode non-pkcs8 type, [${content.join(",")}] found}`
153
- );
154
- }
155
- const cryptoType = version === "0" || !Array.isArray(content) ? this.type : content[1];
156
- if (!cryptoType) {
157
- throw new Error("cryptoType is undefined");
158
- }
159
- const encType = !Array.isArray(type) ? [type] : type;
160
- if (!["sr25519"].includes(cryptoType)) {
161
- throw new Error(`Unknown crypto type ${cryptoType}`);
162
- }
163
- const publicKey = isHex(address) ? hexToU8a(address) : this.decodeAddress(address, ignoreChecksum);
164
- const decoded = isHex(encoded) ? hexToU8a(encoded) : base64Decode(encoded);
165
- return createPair(
166
- { toSS58: this.encodeAddress, type: cryptoType },
167
- { publicKey, secretKey: new Uint8Array() },
168
- meta,
169
- decoded,
170
- encType
171
- );
172
- }
173
- /**
174
- * @name createFromPair
175
- * @summary Creates a pair from an explicit publicKey/secreteKey combination
176
- */
177
- createFromPair(pair, meta = {}, type = this.type) {
178
- return createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
179
- }
180
- /**
181
- * @name createFromUri
182
- * @summary Creates a Keypair from an suri
183
- * @description This creates a pair from the suri, but does not add it to the keyring
184
- */
185
- createFromUri(_suri, meta = {}, type = this.type) {
186
- const suri = _suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri;
187
- const { derivePath, password, path, phrase } = keyExtractSuri(suri);
188
- let seed;
189
- const isPhraseHex = isHex(phrase, 256);
190
- if (isPhraseHex) {
191
- seed = hexToU8a(phrase);
192
- } else {
193
- const parts = phrase.split(" ");
194
- if ([12, 15, 18, 21, 24].includes(parts.length)) {
195
- seed = type === "ethereum" ? (() => {
196
- throw new Error(
197
- "Not implemented - Prosopo Keyring supports sr25519 only"
198
- );
199
- })() : mnemonicToMiniSecret(phrase, password);
200
- } else {
201
- if (phrase.length > 32) {
202
- throw new Error(
203
- "specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes"
204
- );
205
- }
206
- seed = stringToU8a(phrase.padEnd(32));
207
- }
208
- }
209
- const derived = keyFromPath(PairFromSeed[type](seed), path, type);
210
- return createPair(
211
- { toSS58: this.encodeAddress, type },
212
- derived,
213
- meta,
214
- null
215
- );
216
- }
217
- /**
218
- * @name getPair
219
- * @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
220
- * @description Returns a keyring pair value from the keyring pair dictionary by performing
221
- * a key lookup using the provided account address or public key (after decoding it).
222
- */
223
- getPair(address) {
224
- return this.#pairs.get(address);
225
- }
226
- /**
227
- * @name getPairs
228
- * @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
229
- * @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
230
- */
231
- getPairs() {
232
- return this.#pairs.all();
233
- }
234
- /**
235
- * @name getPublicKeys
236
- * @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
237
- * @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.
238
- */
239
- getPublicKeys() {
240
- return this.#pairs.all().map(pairToPublic);
241
- }
242
- /**
243
- * @name removePair
244
- * @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
245
- */
246
- removePair(address) {
247
- this.#pairs.remove(address);
248
- }
249
- /**
250
- * @name setSS58Format;
251
- * @description Sets the ss58 format for the keyring
252
- */
253
- setSS58Format(ss58) {
254
- this.#ss58 = ss58;
255
- }
256
- /**
257
- * @name toJson
258
- * @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
259
- * @description Returns a JSON object containing the metadata associated with an account
260
- * when valid address or public key and when the account passphrase is provided if the account secret
261
- * is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
262
- * may backup their account to a JSON file that contains this information.
263
- */
264
- toJson(address, passphrase) {
265
- return this.#pairs.get(address).toJson(passphrase);
266
- }
22
+ return publicKey;
267
23
  }
268
- export {
269
- DEV_PHRASE,
270
- Keyring
24
+ /**
25
+ * # @polkadot/keyring
26
+ *
27
+ * ## Overview
28
+ *
29
+ * @name Keyring
30
+ * @summary Keyring management of user accounts
31
+ * @description Allows generation of keyring pairs from a variety of input combinations, such as
32
+ * json object containing account address or public key, account metadata, and account encoded using
33
+ * `addFromJson`, or by providing those values as arguments separately to `addFromAddress`,
34
+ * or by providing the mnemonic (seed phrase) and account metadata as arguments to `addFromMnemonic`.
35
+ * Stores the keyring pairs in a keyring pair dictionary. Removal of the keyring pairs from the keyring pair
36
+ * dictionary is achieved using `removePair`. Retrieval of all the stored pairs via `getPairs` or perform
37
+ * lookup of a pair for a given account address or public key using `getPair`. JSON metadata associated with
38
+ * an account may be obtained using `toJson` accompanied by the account passphrase.
39
+ */
40
+ var Keyring = class {
41
+ #pairs;
42
+ #type;
43
+ #ss58;
44
+ constructor(options = {}) {
45
+ this.decodeAddress = decodeAddress;
46
+ this.encodeAddress = (address, ss58Format) => {
47
+ return encodeAddress(address, ss58Format ?? this.#ss58);
48
+ };
49
+ options.type = options.type || "sr25519";
50
+ if (!["sr25519"].includes(options.type || "undefined")) throw new Error(`Expected a keyring type of either 'sr25519', found '${options.type || "unknown"}`);
51
+ this.#pairs = new Pairs();
52
+ this.#ss58 = options.ss58Format;
53
+ this.#type = options.type;
54
+ }
55
+ /**
56
+ * @description retrieve the pairs (alias for getPairs)
57
+ */
58
+ get pairs() {
59
+ return this.getPairs();
60
+ }
61
+ /**
62
+ * @description retrieve the publicKeys (alias for getPublicKeys)
63
+ */
64
+ get publicKeys() {
65
+ return this.getPublicKeys();
66
+ }
67
+ /**
68
+ * @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
69
+ */
70
+ get type() {
71
+ return this.#type;
72
+ }
73
+ /**
74
+ * @name addPair
75
+ * @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
76
+ */
77
+ addPair(pair) {
78
+ return this.#pairs.add(pair);
79
+ }
80
+ /**
81
+ * @name addFromAddress
82
+ * @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
83
+ * @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
84
+ * the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
85
+ * of an account backup), and then generates a keyring pair from them that it passes to
86
+ * `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.
87
+ */
88
+ addFromAddress(address, meta = {}, encoded = null, type = this.type, ignoreChecksum, encType) {
89
+ const publicKey = this.decodeAddress(address, ignoreChecksum);
90
+ return this.addPair(createPair({
91
+ toSS58: this.encodeAddress,
92
+ type
93
+ }, {
94
+ publicKey,
95
+ secretKey: /* @__PURE__ */ new Uint8Array()
96
+ }, meta, encoded, encType));
97
+ }
98
+ /**
99
+ * @name addFromJson
100
+ * @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
101
+ * @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
102
+ * of an account backup), and then generates a keyring pair from it that it passes to
103
+ * `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.
104
+ */
105
+ addFromJson(json, ignoreChecksum) {
106
+ return this.addPair(this.createFromJson(json, ignoreChecksum));
107
+ }
108
+ /**
109
+ * @name addFromMnemonic
110
+ * @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
111
+ * @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
112
+ * argument and a metadata argument that contains account information (that may be obtained from the json file
113
+ * of an account backup), and then generates a keyring pair from it that it passes to
114
+ * `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.
115
+ */
116
+ addFromMnemonic(mnemonic, meta = {}, type = this.type) {
117
+ return this.addFromUri(mnemonic, meta, type);
118
+ }
119
+ /**
120
+ * @name addFromPair
121
+ * @summary Stores an account created from an explicit publicKey/secreteKey combination
122
+ */
123
+ addFromPair(pair, meta = {}, type = this.type) {
124
+ return this.addPair(this.createFromPair(pair, meta, type));
125
+ }
126
+ /**
127
+ * @name addFromSeed
128
+ * @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
129
+ * @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
130
+ * Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
131
+ * `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.
132
+ */
133
+ addFromSeed(seed, meta = {}, type = this.type) {
134
+ return this.addPair(createPair({
135
+ toSS58: this.encodeAddress,
136
+ type
137
+ }, PairFromSeed[type](seed), meta, null));
138
+ }
139
+ /**
140
+ * @name addFromUri
141
+ * @summary Creates an account via an suri
142
+ * @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)
143
+ */
144
+ addFromUri(suri, meta = {}, type = this.type) {
145
+ return this.addPair(this.createFromUri(suri, meta, type));
146
+ }
147
+ /**
148
+ * @name createFromJson
149
+ * @description Creates a pair from a JSON keyfile
150
+ */
151
+ createFromJson({ address, encoded, encoding: { content, type, version }, meta }, ignoreChecksum) {
152
+ if (version === "3" && content[0] !== "pkcs8") throw new Error(`Unable to decode non-pkcs8 type, [${content.join(",")}] found}`);
153
+ const cryptoType = version === "0" || !Array.isArray(content) ? this.type : content[1];
154
+ if (!cryptoType) throw new Error("cryptoType is undefined");
155
+ const encType = !Array.isArray(type) ? [type] : type;
156
+ if (!["sr25519"].includes(cryptoType)) throw new Error(`Unknown crypto type ${cryptoType}`);
157
+ const publicKey = isHex$1(address) ? hexToU8a(address) : this.decodeAddress(address, ignoreChecksum);
158
+ const decoded = isHex$1(encoded) ? hexToU8a(encoded) : base64Decode(encoded);
159
+ return createPair({
160
+ toSS58: this.encodeAddress,
161
+ type: cryptoType
162
+ }, {
163
+ publicKey,
164
+ secretKey: /* @__PURE__ */ new Uint8Array()
165
+ }, meta, decoded, encType);
166
+ }
167
+ /**
168
+ * @name createFromPair
169
+ * @summary Creates a pair from an explicit publicKey/secreteKey combination
170
+ */
171
+ createFromPair(pair, meta = {}, type = this.type) {
172
+ return createPair({
173
+ toSS58: this.encodeAddress,
174
+ type
175
+ }, pair, meta, null);
176
+ }
177
+ /**
178
+ * @name createFromUri
179
+ * @summary Creates a Keypair from an suri
180
+ * @description This creates a pair from the suri, but does not add it to the keyring
181
+ */
182
+ createFromUri(_suri, meta = {}, type = this.type) {
183
+ const { derivePath, password, path, phrase } = keyExtractSuri(_suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri);
184
+ let seed;
185
+ if (isHex$1(phrase, 256)) seed = hexToU8a(phrase);
186
+ else {
187
+ const parts = phrase.split(" ");
188
+ if ([
189
+ 12,
190
+ 15,
191
+ 18,
192
+ 21,
193
+ 24
194
+ ].includes(parts.length)) seed = type === "ethereum" ? (() => {
195
+ throw new Error("Not implemented - Prosopo Keyring supports sr25519 only");
196
+ })() : mnemonicToMiniSecret(phrase, password);
197
+ else {
198
+ if (phrase.length > 32) throw new Error("specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes");
199
+ seed = stringToU8a(phrase.padEnd(32));
200
+ }
201
+ }
202
+ const derived = keyFromPath(PairFromSeed[type](seed), path, type);
203
+ return createPair({
204
+ toSS58: this.encodeAddress,
205
+ type
206
+ }, derived, meta, null);
207
+ }
208
+ /**
209
+ * @name getPair
210
+ * @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
211
+ * @description Returns a keyring pair value from the keyring pair dictionary by performing
212
+ * a key lookup using the provided account address or public key (after decoding it).
213
+ */
214
+ getPair(address) {
215
+ return this.#pairs.get(address);
216
+ }
217
+ /**
218
+ * @name getPairs
219
+ * @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
220
+ * @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
221
+ */
222
+ getPairs() {
223
+ return this.#pairs.all();
224
+ }
225
+ /**
226
+ * @name getPublicKeys
227
+ * @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
228
+ * @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.
229
+ */
230
+ getPublicKeys() {
231
+ return this.#pairs.all().map(pairToPublic);
232
+ }
233
+ /**
234
+ * @name removePair
235
+ * @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
236
+ */
237
+ removePair(address) {
238
+ this.#pairs.remove(address);
239
+ }
240
+ /**
241
+ * @name setSS58Format;
242
+ * @description Sets the ss58 format for the keyring
243
+ */
244
+ setSS58Format(ss58) {
245
+ this.#ss58 = ss58;
246
+ }
247
+ /**
248
+ * @name toJson
249
+ * @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
250
+ * @description Returns a JSON object containing the metadata associated with an account
251
+ * when valid address or public key and when the account passphrase is provided if the account secret
252
+ * is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
253
+ * may backup their account to a JSON file that contains this information.
254
+ */
255
+ toJson(address, passphrase) {
256
+ return this.#pairs.get(address).toJson(passphrase);
257
+ }
271
258
  };
259
+ //#endregion
260
+ export { DEV_PHRASE, Keyring };
@@ -1,28 +1,24 @@
1
- import { isU8a, isHex, u8aToU8a } from "@polkadot/util";
2
- import { u8aToHex } from "@prosopo/util";
3
1
  import { decodeAddress } from "@prosopo/util-crypto";
4
- class Pairs {
5
- #map = {};
6
- add(pair) {
7
- this.#map[decodeAddress(pair.address).toString()] = pair;
8
- return pair;
9
- }
10
- all() {
11
- return Object.values(this.#map);
12
- }
13
- get(address) {
14
- const pair = this.#map[decodeAddress(address).toString()];
15
- if (!pair) {
16
- throw new Error(
17
- `Unable to retrieve keypair '${isU8a(address) || isHex(address) ? u8aToHex(u8aToU8a(address)) : address}'`
18
- );
19
- }
20
- return pair;
21
- }
22
- remove(address) {
23
- delete this.#map[decodeAddress(address).toString()];
24
- }
25
- }
26
- export {
27
- Pairs
2
+ import { isHex, isU8a, u8aToU8a } from "@polkadot/util";
3
+ import { u8aToHex as u8aToHex$1 } from "@prosopo/util";
4
+ //#region src/keyring/pairs.ts
5
+ var Pairs = class {
6
+ #map = {};
7
+ add(pair) {
8
+ this.#map[decodeAddress(pair.address).toString()] = pair;
9
+ return pair;
10
+ }
11
+ all() {
12
+ return Object.values(this.#map);
13
+ }
14
+ get(address) {
15
+ const pair = this.#map[decodeAddress(address).toString()];
16
+ if (!pair) throw new Error(`Unable to retrieve keypair '${isU8a(address) || isHex(address) ? u8aToHex$1(u8aToU8a(address)) : address}'`);
17
+ return pair;
18
+ }
19
+ remove(address) {
20
+ delete this.#map[decodeAddress(address).toString()];
21
+ }
28
22
  };
23
+ //#endregion
24
+ export { Pairs };
@@ -1,32 +1,32 @@
1
- import { u8aEq } from "@polkadot/util";
1
+ import { PAIR_DIV, PAIR_HDR } from "./defaults.js";
2
2
  import { jsonDecryptData } from "@prosopo/util-crypto";
3
- import { PAIR_HDR, SEC_LENGTH, PAIR_DIV, SEED_LENGTH, PUB_LENGTH } from "./defaults.js";
4
- const SEED_OFFSET = PAIR_HDR.length;
3
+ import { u8aEq } from "@polkadot/util";
4
+ //#region src/pair/decode.ts
5
+ var SEED_OFFSET = PAIR_HDR.length;
6
+ /**
7
+ * Decode a pair, taking into account the generation-specific formats and headers
8
+ *
9
+ * For divisor/headers, don't rely on the magic being static. These will
10
+ * change between generations, aka with the long-awaited 4th generation
11
+ * of the format. The external decode interface is the only way to use and decode these.
12
+ **/
5
13
  function decodePair(passphrase, encrypted, _encType) {
6
- const encType = Array.isArray(_encType) || _encType === void 0 ? _encType : [_encType];
7
- const decrypted = jsonDecryptData(encrypted, passphrase, encType);
8
- const header = decrypted.subarray(0, PAIR_HDR.length);
9
- if (!u8aEq(header, PAIR_HDR)) {
10
- throw new Error("Invalid encoding header found in body");
11
- }
12
- let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + SEC_LENGTH);
13
- let divOffset = SEED_OFFSET + SEC_LENGTH;
14
- let divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
15
- if (!u8aEq(divider, PAIR_DIV)) {
16
- divOffset = SEED_OFFSET + SEED_LENGTH;
17
- secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
18
- divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
19
- if (!u8aEq(divider, PAIR_DIV)) {
20
- throw new Error("Invalid encoding divider found in body");
21
- }
22
- }
23
- const pubOffset = divOffset + PAIR_DIV.length;
24
- const publicKey = decrypted.subarray(pubOffset, pubOffset + PUB_LENGTH);
25
- return {
26
- publicKey,
27
- secretKey
28
- };
14
+ const decrypted = jsonDecryptData(encrypted, passphrase, Array.isArray(_encType) || _encType === void 0 ? _encType : [_encType]);
15
+ if (!u8aEq(decrypted.subarray(0, PAIR_HDR.length), PAIR_HDR)) throw new Error("Invalid encoding header found in body");
16
+ let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + 64);
17
+ let divOffset = SEED_OFFSET + 64;
18
+ let divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
19
+ if (!u8aEq(divider, PAIR_DIV)) {
20
+ divOffset = SEED_OFFSET + 32;
21
+ secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
22
+ divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
23
+ if (!u8aEq(divider, PAIR_DIV)) throw new Error("Invalid encoding divider found in body");
24
+ }
25
+ const pubOffset = divOffset + PAIR_DIV.length;
26
+ return {
27
+ publicKey: decrypted.subarray(pubOffset, pubOffset + 32),
28
+ secretKey
29
+ };
29
30
  }
30
- export {
31
- decodePair
32
- };
31
+ //#endregion
32
+ export { decodePair };