@prosopo/keyring 2.10.0 → 2.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +3 -6
- package/.turbo/turbo-build$colon$tsc.log +11 -14
- package/.turbo/turbo-build.log +4 -10
- package/CHANGELOG.md +44 -0
- package/LICENSE +201 -0
- package/dist/accounts/testAccounts.d.ts.map +1 -1
- package/dist/accounts/testAccounts.js +23 -8
- package/dist/accounts/testAccounts.js.map +1 -1
- package/dist/accounts/testAccounts.test.js +33 -4
- package/dist/accounts/testAccounts.test.js.map +1 -1
- package/dist/cjs/accounts/testAccounts.cjs +23 -8
- package/package.json +59 -60
- package/src/accounts/getPair.test.ts +0 -142
- package/src/accounts/getPair.ts +0 -63
- package/src/accounts/index.ts +0 -16
- package/src/accounts/mnemonic.test.ts +0 -103
- package/src/accounts/mnemonic.ts +0 -45
- package/src/accounts/testAccounts.test.ts +0 -102
- package/src/accounts/testAccounts.ts +0 -66
- package/src/index.ts +0 -16
- package/src/keyring/index.ts +0 -15
- package/src/keyring/keyring.test.ts +0 -373
- package/src/keyring/keyring.ts +0 -411
- package/src/keyring/pairs.test.ts +0 -133
- package/src/keyring/pairs.ts +0 -44
- package/src/keyring/testing.test.ts +0 -146
- package/src/keyring/testing.ts +0 -119
- package/src/keyring/testingPairs.ts +0 -61
- package/src/keyring.test-d.ts +0 -134
- package/src/pair/decode.spec.ts +0 -25
- package/src/pair/decode.ts +0 -64
- package/src/pair/defaults.ts +0 -22
- package/src/pair/encode.spec.ts +0 -30
- package/src/pair/encode.ts +0 -32
- package/src/pair/index.ts +0 -304
- package/src/pair/nobody.test.ts +0 -127
- package/src/pair/nobody.ts +0 -79
- package/src/pair/toJson.spec.ts +0 -46
- package/src/pair/toJson.ts +0 -28
- package/tsconfig.cjs.json +0 -30
- package/tsconfig.json +0 -33
- package/tsconfig.tsbuildinfo +0 -1
- package/tsconfig.types.json +0 -9
package/src/keyring/keyring.ts
DELETED
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { stringToU8a } from "@polkadot/util";
|
|
16
|
-
import { hexToU8a } from "@polkadot/util/hex";
|
|
17
|
-
import { isHex } from "@polkadot/util/is";
|
|
18
|
-
import type { KeyringPair$Json, KeyringPair$Meta } from "@prosopo/types";
|
|
19
|
-
import type { KeyringInstance, KeyringPair } from "@prosopo/types";
|
|
20
|
-
import { decodeAddress, mnemonicToMiniSecret } from "@prosopo/util-crypto";
|
|
21
|
-
import { encodeAddress } from "@prosopo/util-crypto";
|
|
22
|
-
import { base64Decode } from "@prosopo/util-crypto";
|
|
23
|
-
import { sr25519FromSeed } from "@prosopo/util-crypto";
|
|
24
|
-
import { keyExtractSuri, keyFromPath } from "@prosopo/util-crypto";
|
|
25
|
-
import type {
|
|
26
|
-
EncryptedJsonEncoding,
|
|
27
|
-
Keypair,
|
|
28
|
-
KeypairType,
|
|
29
|
-
} from "@prosopo/util-crypto";
|
|
30
|
-
import { createPair } from "../pair/index.js";
|
|
31
|
-
import { Pairs } from "./pairs.js";
|
|
32
|
-
|
|
33
|
-
export const DEV_PHRASE =
|
|
34
|
-
"bottom drive obey lake curtain smoke basket hold race lonely fit walk";
|
|
35
|
-
|
|
36
|
-
const PairFromSeed = {
|
|
37
|
-
sr25519: (seed: Uint8Array): Keypair => sr25519FromSeed(seed),
|
|
38
|
-
ed25519: () => {
|
|
39
|
-
throw new Error("Not Implemented");
|
|
40
|
-
},
|
|
41
|
-
ecdsa: () => {
|
|
42
|
-
throw new Error("Not Implemented");
|
|
43
|
-
},
|
|
44
|
-
ethereum: () => {
|
|
45
|
-
throw new Error("Not Implemented");
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
interface KeyringOptions {
|
|
50
|
-
type?: KeypairType; // "sr25519" only
|
|
51
|
-
ss58Format?: number;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function pairToPublic({ publicKey }: KeyringPair): Uint8Array {
|
|
55
|
-
return publicKey;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* # @polkadot/keyring
|
|
60
|
-
*
|
|
61
|
-
* ## Overview
|
|
62
|
-
*
|
|
63
|
-
* @name Keyring
|
|
64
|
-
* @summary Keyring management of user accounts
|
|
65
|
-
* @description Allows generation of keyring pairs from a variety of input combinations, such as
|
|
66
|
-
* json object containing account address or public key, account metadata, and account encoded using
|
|
67
|
-
* `addFromJson`, or by providing those values as arguments separately to `addFromAddress`,
|
|
68
|
-
* or by providing the mnemonic (seed phrase) and account metadata as arguments to `addFromMnemonic`.
|
|
69
|
-
* Stores the keyring pairs in a keyring pair dictionary. Removal of the keyring pairs from the keyring pair
|
|
70
|
-
* dictionary is achieved using `removePair`. Retrieval of all the stored pairs via `getPairs` or perform
|
|
71
|
-
* lookup of a pair for a given account address or public key using `getPair`. JSON metadata associated with
|
|
72
|
-
* an account may be obtained using `toJson` accompanied by the account passphrase.
|
|
73
|
-
*/
|
|
74
|
-
export class Keyring implements KeyringInstance {
|
|
75
|
-
readonly #pairs: Pairs;
|
|
76
|
-
|
|
77
|
-
readonly #type: KeypairType;
|
|
78
|
-
|
|
79
|
-
#ss58?: number | undefined;
|
|
80
|
-
|
|
81
|
-
public decodeAddress = decodeAddress;
|
|
82
|
-
|
|
83
|
-
constructor(options: KeyringOptions = {}) {
|
|
84
|
-
options.type = options.type || "sr25519";
|
|
85
|
-
|
|
86
|
-
if (!["sr25519"].includes(options.type || "undefined")) {
|
|
87
|
-
throw new Error(
|
|
88
|
-
`Expected a keyring type of either 'sr25519', found '${options.type || "unknown"}`,
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
this.#pairs = new Pairs();
|
|
93
|
-
this.#ss58 = options.ss58Format;
|
|
94
|
-
this.#type = options.type;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @description retrieve the pairs (alias for getPairs)
|
|
99
|
-
*/
|
|
100
|
-
public get pairs(): KeyringPair[] {
|
|
101
|
-
return this.getPairs();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* @description retrieve the publicKeys (alias for getPublicKeys)
|
|
106
|
-
*/
|
|
107
|
-
public get publicKeys(): Uint8Array[] {
|
|
108
|
-
return this.getPublicKeys();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
|
|
113
|
-
*/
|
|
114
|
-
public get type(): KeypairType {
|
|
115
|
-
return this.#type;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* @name addPair
|
|
120
|
-
* @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
121
|
-
*/
|
|
122
|
-
public addPair(pair: KeyringPair): KeyringPair {
|
|
123
|
-
return this.#pairs.add(pair);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* @name addFromAddress
|
|
128
|
-
* @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
129
|
-
* @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
|
|
130
|
-
* the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
|
|
131
|
-
* of an account backup), and then generates a keyring pair from them that it passes to
|
|
132
|
-
* `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.
|
|
133
|
-
*/
|
|
134
|
-
public addFromAddress(
|
|
135
|
-
address: string | Uint8Array,
|
|
136
|
-
meta: KeyringPair$Meta = {},
|
|
137
|
-
encoded: Uint8Array | null = null,
|
|
138
|
-
type: KeypairType = this.type,
|
|
139
|
-
ignoreChecksum?: boolean,
|
|
140
|
-
encType?: EncryptedJsonEncoding[],
|
|
141
|
-
): KeyringPair {
|
|
142
|
-
const publicKey = this.decodeAddress(address, ignoreChecksum);
|
|
143
|
-
|
|
144
|
-
return this.addPair(
|
|
145
|
-
createPair(
|
|
146
|
-
{ toSS58: this.encodeAddress, type },
|
|
147
|
-
{ publicKey, secretKey: new Uint8Array() },
|
|
148
|
-
meta,
|
|
149
|
-
encoded,
|
|
150
|
-
encType,
|
|
151
|
-
),
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* @name addFromJson
|
|
157
|
-
* @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
158
|
-
* @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
|
|
159
|
-
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
160
|
-
* `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.
|
|
161
|
-
*/
|
|
162
|
-
public addFromJson(
|
|
163
|
-
json: KeyringPair$Json,
|
|
164
|
-
ignoreChecksum?: boolean,
|
|
165
|
-
): KeyringPair {
|
|
166
|
-
return this.addPair(this.createFromJson(json, ignoreChecksum));
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* @name addFromMnemonic
|
|
171
|
-
* @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
172
|
-
* @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
|
|
173
|
-
* argument and a metadata argument that contains account information (that may be obtained from the json file
|
|
174
|
-
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
175
|
-
* `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.
|
|
176
|
-
*/
|
|
177
|
-
public addFromMnemonic(
|
|
178
|
-
mnemonic: string,
|
|
179
|
-
meta: KeyringPair$Meta = {},
|
|
180
|
-
type: KeypairType = this.type,
|
|
181
|
-
): KeyringPair {
|
|
182
|
-
return this.addFromUri(mnemonic, meta, type);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* @name addFromPair
|
|
187
|
-
* @summary Stores an account created from an explicit publicKey/secreteKey combination
|
|
188
|
-
*/
|
|
189
|
-
public addFromPair(
|
|
190
|
-
pair: Keypair,
|
|
191
|
-
meta: KeyringPair$Meta = {},
|
|
192
|
-
type: KeypairType = this.type,
|
|
193
|
-
): KeyringPair {
|
|
194
|
-
return this.addPair(this.createFromPair(pair, meta, type));
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* @name addFromSeed
|
|
199
|
-
* @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
200
|
-
* @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
|
|
201
|
-
* Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
|
|
202
|
-
* `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.
|
|
203
|
-
*/
|
|
204
|
-
public addFromSeed(
|
|
205
|
-
seed: Uint8Array,
|
|
206
|
-
meta: KeyringPair$Meta = {},
|
|
207
|
-
type: KeypairType = this.type,
|
|
208
|
-
): KeyringPair {
|
|
209
|
-
return this.addPair(
|
|
210
|
-
createPair(
|
|
211
|
-
{ toSS58: this.encodeAddress, type },
|
|
212
|
-
PairFromSeed[type](seed),
|
|
213
|
-
meta,
|
|
214
|
-
null,
|
|
215
|
-
),
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* @name addFromUri
|
|
221
|
-
* @summary Creates an account via an suri
|
|
222
|
-
* @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)
|
|
223
|
-
*/
|
|
224
|
-
public addFromUri(
|
|
225
|
-
suri: string,
|
|
226
|
-
meta: KeyringPair$Meta = {},
|
|
227
|
-
type: KeypairType = this.type,
|
|
228
|
-
): KeyringPair {
|
|
229
|
-
return this.addPair(this.createFromUri(suri, meta, type));
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* @name createFromJson
|
|
234
|
-
* @description Creates a pair from a JSON keyfile
|
|
235
|
-
*/
|
|
236
|
-
public createFromJson(
|
|
237
|
-
{
|
|
238
|
-
address,
|
|
239
|
-
encoded,
|
|
240
|
-
encoding: { content, type, version },
|
|
241
|
-
meta,
|
|
242
|
-
}: KeyringPair$Json,
|
|
243
|
-
ignoreChecksum?: boolean,
|
|
244
|
-
): KeyringPair {
|
|
245
|
-
if (version === "3" && content[0] !== "pkcs8") {
|
|
246
|
-
throw new Error(
|
|
247
|
-
`Unable to decode non-pkcs8 type, [${content.join(",")}] found}`,
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const cryptoType =
|
|
252
|
-
version === "0" || !Array.isArray(content) ? this.type : content[1];
|
|
253
|
-
|
|
254
|
-
if (!cryptoType) {
|
|
255
|
-
throw new Error("cryptoType is undefined");
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const encType = !Array.isArray(type) ? [type] : type;
|
|
259
|
-
|
|
260
|
-
if (!["sr25519"].includes(cryptoType)) {
|
|
261
|
-
throw new Error(`Unknown crypto type ${cryptoType}`);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Here the address and publicKey are 32 bytes and isomorphic. This is why the address field needs to be the public key for ethereum type pairs
|
|
265
|
-
const publicKey = isHex(address)
|
|
266
|
-
? hexToU8a(address)
|
|
267
|
-
: this.decodeAddress(address, ignoreChecksum);
|
|
268
|
-
const decoded = isHex(encoded) ? hexToU8a(encoded) : base64Decode(encoded);
|
|
269
|
-
|
|
270
|
-
return createPair(
|
|
271
|
-
{ toSS58: this.encodeAddress, type: cryptoType as KeypairType },
|
|
272
|
-
{ publicKey, secretKey: new Uint8Array() },
|
|
273
|
-
meta,
|
|
274
|
-
decoded,
|
|
275
|
-
encType,
|
|
276
|
-
);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* @name createFromPair
|
|
281
|
-
* @summary Creates a pair from an explicit publicKey/secreteKey combination
|
|
282
|
-
*/
|
|
283
|
-
public createFromPair(
|
|
284
|
-
pair: Keypair,
|
|
285
|
-
meta: KeyringPair$Meta = {},
|
|
286
|
-
type: KeypairType = this.type,
|
|
287
|
-
): KeyringPair {
|
|
288
|
-
return createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* @name createFromUri
|
|
293
|
-
* @summary Creates a Keypair from an suri
|
|
294
|
-
* @description This creates a pair from the suri, but does not add it to the keyring
|
|
295
|
-
*/
|
|
296
|
-
public createFromUri(
|
|
297
|
-
_suri: string,
|
|
298
|
-
meta: KeyringPair$Meta = {},
|
|
299
|
-
type: KeypairType = this.type,
|
|
300
|
-
): KeyringPair {
|
|
301
|
-
// here we only aut-add the dev phrase if we have a hard-derived path
|
|
302
|
-
const suri = _suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri;
|
|
303
|
-
const { derivePath, password, path, phrase } = keyExtractSuri(suri);
|
|
304
|
-
|
|
305
|
-
let seed: Uint8Array;
|
|
306
|
-
const isPhraseHex = isHex(phrase, 256);
|
|
307
|
-
|
|
308
|
-
if (isPhraseHex) {
|
|
309
|
-
seed = hexToU8a(phrase);
|
|
310
|
-
} else {
|
|
311
|
-
const parts = phrase.split(" ");
|
|
312
|
-
|
|
313
|
-
if ([12, 15, 18, 21, 24].includes(parts.length)) {
|
|
314
|
-
seed =
|
|
315
|
-
type === "ethereum"
|
|
316
|
-
? (() => {
|
|
317
|
-
throw new Error(
|
|
318
|
-
"Not implemented - Prosopo Keyring supports sr25519 only",
|
|
319
|
-
);
|
|
320
|
-
})()
|
|
321
|
-
: mnemonicToMiniSecret(phrase, password);
|
|
322
|
-
} else {
|
|
323
|
-
if (phrase.length > 32) {
|
|
324
|
-
throw new Error(
|
|
325
|
-
"specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes",
|
|
326
|
-
);
|
|
327
|
-
}
|
|
328
|
-
seed = stringToU8a(phrase.padEnd(32));
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const derived = keyFromPath(PairFromSeed[type](seed), path, type);
|
|
333
|
-
|
|
334
|
-
return createPair(
|
|
335
|
-
{ toSS58: this.encodeAddress, type },
|
|
336
|
-
derived,
|
|
337
|
-
meta,
|
|
338
|
-
null,
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* @name encodeAddress
|
|
344
|
-
* @description Encodes the input into an ss58 representation
|
|
345
|
-
*/
|
|
346
|
-
public encodeAddress = (
|
|
347
|
-
address: Uint8Array | string,
|
|
348
|
-
ss58Format?: number,
|
|
349
|
-
): string => {
|
|
350
|
-
return encodeAddress(address, ss58Format ?? this.#ss58);
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* @name getPair
|
|
355
|
-
* @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
|
|
356
|
-
* @description Returns a keyring pair value from the keyring pair dictionary by performing
|
|
357
|
-
* a key lookup using the provided account address or public key (after decoding it).
|
|
358
|
-
*/
|
|
359
|
-
public getPair(address: string | Uint8Array): KeyringPair {
|
|
360
|
-
return this.#pairs.get(address);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* @name getPairs
|
|
365
|
-
* @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
|
|
366
|
-
* @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
|
|
367
|
-
*/
|
|
368
|
-
public getPairs(): KeyringPair[] {
|
|
369
|
-
return this.#pairs.all();
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* @name getPublicKeys
|
|
374
|
-
* @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
|
|
375
|
-
* @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.
|
|
376
|
-
*/
|
|
377
|
-
public getPublicKeys(): Uint8Array[] {
|
|
378
|
-
return this.#pairs.all().map(pairToPublic);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* @name removePair
|
|
383
|
-
* @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
|
|
384
|
-
*/
|
|
385
|
-
public removePair(address: string | Uint8Array): void {
|
|
386
|
-
this.#pairs.remove(address);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* @name setSS58Format;
|
|
391
|
-
* @description Sets the ss58 format for the keyring
|
|
392
|
-
*/
|
|
393
|
-
public setSS58Format(ss58: number): void {
|
|
394
|
-
this.#ss58 = ss58;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* @name toJson
|
|
399
|
-
* @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
|
|
400
|
-
* @description Returns a JSON object containing the metadata associated with an account
|
|
401
|
-
* when valid address or public key and when the account passphrase is provided if the account secret
|
|
402
|
-
* is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
|
|
403
|
-
* may backup their account to a JSON file that contains this information.
|
|
404
|
-
*/
|
|
405
|
-
public toJson(
|
|
406
|
-
address: string | Uint8Array,
|
|
407
|
-
passphrase?: string,
|
|
408
|
-
): KeyringPair$Json {
|
|
409
|
-
return this.#pairs.get(address).toJson(passphrase);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { KeyringPair } from "@prosopo/types";
|
|
16
|
-
import { decodeAddress, encodeAddress } from "@prosopo/util-crypto";
|
|
17
|
-
import { describe, expect, it } from "vitest";
|
|
18
|
-
import { nobody } from "../pair/nobody.js";
|
|
19
|
-
import { Pairs } from "./pairs.js";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* A pair is stored under `decodeAddress(pair.address).toString()`, which is the
|
|
23
|
-
* comma-joined bytes of the public key. Only `address` and `publicKey` are read
|
|
24
|
-
* by `Pairs`, so the inert `nobody` pair supplies the rest of the surface.
|
|
25
|
-
*/
|
|
26
|
-
const stubPair = (publicKey: Uint8Array): KeyringPair => ({
|
|
27
|
-
...nobody(),
|
|
28
|
-
address: encodeAddress(publicKey, 42),
|
|
29
|
-
addressRaw: publicKey,
|
|
30
|
-
publicKey,
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const keyOf = (byte: number): Uint8Array => new Uint8Array(32).fill(byte);
|
|
34
|
-
|
|
35
|
-
describe("Pairs", () => {
|
|
36
|
-
it("returns the pair it was given, so callers can chain off add", () => {
|
|
37
|
-
const pairs = new Pairs();
|
|
38
|
-
const pair = stubPair(keyOf(1));
|
|
39
|
-
expect(pairs.add(pair)).toBe(pair);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("starts empty", () => {
|
|
43
|
-
expect(new Pairs().all()).toEqual([]);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("retrieves a pair by SS58 address, public key or raw bytes", () => {
|
|
47
|
-
const pairs = new Pairs();
|
|
48
|
-
const publicKey = keyOf(2);
|
|
49
|
-
const pair = pairs.add(stubPair(publicKey));
|
|
50
|
-
|
|
51
|
-
expect(pairs.get(pair.address)).toBe(pair);
|
|
52
|
-
expect(pairs.get(publicKey)).toBe(pair);
|
|
53
|
-
// A different SS58 prefix decodes to the same public key, so it must
|
|
54
|
-
// find the same pair — the map is keyed on bytes, not on the string.
|
|
55
|
-
expect(pairs.get(encodeAddress(publicKey, 2))).toBe(pair);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("keeps only the latest pair for a public key", () => {
|
|
59
|
-
const pairs = new Pairs();
|
|
60
|
-
const publicKey = keyOf(3);
|
|
61
|
-
pairs.add(stubPair(publicKey));
|
|
62
|
-
const second = stubPair(publicKey);
|
|
63
|
-
pairs.add(second);
|
|
64
|
-
|
|
65
|
-
expect(pairs.all()).toHaveLength(1);
|
|
66
|
-
expect(pairs.get(publicKey)).toBe(second);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("keeps distinct public keys apart", () => {
|
|
70
|
-
const pairs = new Pairs();
|
|
71
|
-
const first = pairs.add(stubPair(keyOf(4)));
|
|
72
|
-
const second = pairs.add(stubPair(keyOf(5)));
|
|
73
|
-
|
|
74
|
-
expect(pairs.all()).toEqual([first, second]);
|
|
75
|
-
expect(pairs.get(first.address)).toBe(first);
|
|
76
|
-
expect(pairs.get(second.address)).toBe(second);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("names the address it could not find, formatting bytes as hex", () => {
|
|
80
|
-
const pairs = new Pairs();
|
|
81
|
-
// A caller debugging a missing key needs to see which key was asked
|
|
82
|
-
// for; a bare "not found" would be useless.
|
|
83
|
-
expect(() => pairs.get(keyOf(6))).toThrow(
|
|
84
|
-
`Unable to retrieve keypair '0x${"06".repeat(32)}'`,
|
|
85
|
-
);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("names the address verbatim when it was given as SS58", () => {
|
|
89
|
-
const address = encodeAddress(keyOf(7), 42);
|
|
90
|
-
expect(() => new Pairs().get(address)).toThrow(
|
|
91
|
-
`Unable to retrieve keypair '${address}'`,
|
|
92
|
-
);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("rejects an address that is not decodable at all", () => {
|
|
96
|
-
expect(() => new Pairs().get("not-an-address")).toThrow();
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("removes a pair, after which lookup throws again", () => {
|
|
100
|
-
const pairs = new Pairs();
|
|
101
|
-
const publicKey = keyOf(8);
|
|
102
|
-
pairs.add(stubPair(publicKey));
|
|
103
|
-
pairs.remove(publicKey);
|
|
104
|
-
|
|
105
|
-
expect(pairs.all()).toEqual([]);
|
|
106
|
-
expect(() => pairs.get(publicKey)).toThrow("Unable to retrieve keypair");
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("removes by any encoding of the same key", () => {
|
|
110
|
-
const pairs = new Pairs();
|
|
111
|
-
const publicKey = keyOf(9);
|
|
112
|
-
const pair = pairs.add(stubPair(publicKey));
|
|
113
|
-
pairs.remove(encodeAddress(publicKey, 2));
|
|
114
|
-
expect(pairs.all()).toEqual([]);
|
|
115
|
-
expect(pair.address).toBe(encodeAddress(publicKey, 42));
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("ignores removal of a key that was never added", () => {
|
|
119
|
-
const pairs = new Pairs();
|
|
120
|
-
const kept = pairs.add(stubPair(keyOf(10)));
|
|
121
|
-
expect(() => pairs.remove(keyOf(11))).not.toThrow();
|
|
122
|
-
expect(pairs.all()).toEqual([kept]);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it("decodes the address for the map key rather than trusting the string", () => {
|
|
126
|
-
// Guards the assumption the class is built on: two encodings of one
|
|
127
|
-
// key must decode to identical bytes.
|
|
128
|
-
const publicKey = keyOf(12);
|
|
129
|
-
expect(decodeAddress(encodeAddress(publicKey, 42)).toString()).toBe(
|
|
130
|
-
decodeAddress(encodeAddress(publicKey, 2)).toString(),
|
|
131
|
-
);
|
|
132
|
-
});
|
|
133
|
-
});
|
package/src/keyring/pairs.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// Copyright 2017-2025 @polkadot/keyring authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { isHex, isU8a, u8aToU8a } from "@polkadot/util";
|
|
5
|
-
import type { KeyringPair, KeyringPairs } from "@prosopo/types";
|
|
6
|
-
|
|
7
|
-
import { u8aToHex } from "@prosopo/util";
|
|
8
|
-
import { decodeAddress } from "@prosopo/util-crypto";
|
|
9
|
-
|
|
10
|
-
type KeyringPairMap = Record<string, KeyringPair>;
|
|
11
|
-
|
|
12
|
-
export class Pairs implements KeyringPairs {
|
|
13
|
-
readonly #map: KeyringPairMap = {};
|
|
14
|
-
|
|
15
|
-
public add(pair: KeyringPair): KeyringPair {
|
|
16
|
-
this.#map[decodeAddress(pair.address).toString()] = pair;
|
|
17
|
-
|
|
18
|
-
return pair;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public all(): KeyringPair[] {
|
|
22
|
-
return Object.values(this.#map);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public get(address: string | Uint8Array): KeyringPair {
|
|
26
|
-
const pair = this.#map[decodeAddress(address).toString()];
|
|
27
|
-
|
|
28
|
-
if (!pair) {
|
|
29
|
-
throw new Error(
|
|
30
|
-
`Unable to retrieve keypair '${
|
|
31
|
-
isU8a(address) || isHex(address)
|
|
32
|
-
? u8aToHex(u8aToU8a(address))
|
|
33
|
-
: address
|
|
34
|
-
}'`,
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return pair;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public remove(address: string | Uint8Array): void {
|
|
42
|
-
delete this.#map[decodeAddress(address).toString()];
|
|
43
|
-
}
|
|
44
|
-
}
|