@pezkuwi/keyring 14.0.1 → 14.0.3

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 (96) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +3 -3
  3. package/bundle-polkadot-keyring.js +553 -0
  4. package/bundle.d.ts +7 -0
  5. package/{src/bundle.ts → bundle.js} +0 -9
  6. package/cjs/bundle.d.ts +7 -0
  7. package/cjs/bundle.js +19 -0
  8. package/cjs/defaults.d.ts +2 -0
  9. package/cjs/defaults.js +5 -0
  10. package/{src/index.ts → cjs/index.d.ts} +0 -6
  11. package/cjs/index.js +7 -0
  12. package/cjs/keyring.d.ts +145 -0
  13. package/cjs/keyring.js +261 -0
  14. package/cjs/package.json +3 -0
  15. package/cjs/packageDetect.d.ts +1 -0
  16. package/cjs/packageDetect.js +7 -0
  17. package/cjs/packageInfo.d.ts +6 -0
  18. package/cjs/packageInfo.js +4 -0
  19. package/cjs/pair/decode.d.ts +12 -0
  20. package/cjs/pair/decode.js +45 -0
  21. package/cjs/pair/defaults.d.ts +12 -0
  22. package/cjs/pair/defaults.js +15 -0
  23. package/cjs/pair/encode.d.ts +5 -0
  24. package/cjs/pair/encode.js +22 -0
  25. package/cjs/pair/index.d.ts +40 -0
  26. package/cjs/pair/index.js +183 -0
  27. package/cjs/pair/nobody.d.ts +2 -0
  28. package/cjs/pair/nobody.js +43 -0
  29. package/cjs/pair/toJson.d.ts +8 -0
  30. package/cjs/pair/toJson.js +11 -0
  31. package/cjs/pair/types.d.ts +5 -0
  32. package/cjs/pair/types.js +2 -0
  33. package/cjs/pairs.d.ts +8 -0
  34. package/cjs/pairs.js +28 -0
  35. package/cjs/testing.d.ts +20 -0
  36. package/cjs/testing.js +126 -0
  37. package/cjs/testingPairs.d.ts +25 -0
  38. package/cjs/testingPairs.js +16 -0
  39. package/cjs/types.d.ts +111 -0
  40. package/cjs/types.js +2 -0
  41. package/defaults.d.ts +2 -0
  42. package/{src/defaults.ts → defaults.js} +0 -6
  43. package/index.d.ts +4 -0
  44. package/index.js +4 -0
  45. package/keyring.d.ts +145 -0
  46. package/keyring.js +257 -0
  47. package/package.json +268 -6
  48. package/packageDetect.d.ts +1 -0
  49. package/{src/packageDetect.ts → packageDetect.js} +0 -8
  50. package/packageInfo.d.ts +6 -0
  51. package/packageInfo.js +1 -0
  52. package/pair/decode.d.ts +12 -0
  53. package/pair/decode.js +42 -0
  54. package/pair/defaults.d.ts +12 -0
  55. package/{src/pair/defaults.ts → pair/defaults.js} +0 -8
  56. package/pair/encode.d.ts +5 -0
  57. package/pair/encode.js +19 -0
  58. package/pair/index.d.ts +40 -0
  59. package/pair/index.js +180 -0
  60. package/pair/nobody.d.ts +2 -0
  61. package/pair/nobody.js +40 -0
  62. package/pair/toJson.d.ts +8 -0
  63. package/pair/toJson.js +8 -0
  64. package/pair/types.d.ts +5 -0
  65. package/pair/types.js +1 -0
  66. package/pairs.d.ts +8 -0
  67. package/pairs.js +24 -0
  68. package/testing.d.ts +20 -0
  69. package/testing.js +122 -0
  70. package/testingPairs.d.ts +25 -0
  71. package/testingPairs.js +13 -0
  72. package/types.d.ts +111 -0
  73. package/types.js +1 -0
  74. package/src/index.spec.ts +0 -609
  75. package/src/keyring.ts +0 -307
  76. package/src/mod.ts +0 -4
  77. package/src/packageInfo.ts +0 -6
  78. package/src/pair/decode.spec.ts +0 -26
  79. package/src/pair/decode.ts +0 -56
  80. package/src/pair/encode.spec.ts +0 -28
  81. package/src/pair/encode.ts +0 -30
  82. package/src/pair/index.spec.ts +0 -189
  83. package/src/pair/index.ts +0 -220
  84. package/src/pair/nobody.ts +0 -62
  85. package/src/pair/toJson.spec.ts +0 -42
  86. package/src/pair/toJson.ts +0 -20
  87. package/src/pair/types.ts +0 -8
  88. package/src/pair/vrf.spec.ts +0 -47
  89. package/src/pairs.ts +0 -41
  90. package/src/suri.spec.ts +0 -109
  91. package/src/testing.ts +0 -156
  92. package/src/testingPairs.spec.ts +0 -79
  93. package/src/testingPairs.ts +0 -56
  94. package/src/types.ts +0 -131
  95. package/tsconfig.build.json +0 -16
  96. package/tsconfig.spec.json +0 -18
package/src/keyring.ts DELETED
@@ -1,307 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- import type { EncryptedJsonEncoding, Keypair, KeypairType } from '@pezkuwi/util-crypto/types';
5
- import type { KeyringInstance, KeyringOptions, KeyringPair, KeyringPair$Json, KeyringPair$Meta } from './types.js';
6
-
7
- import { hexToU8a, isHex, stringToU8a } from '@pezkuwi/util';
8
- import { base64Decode, decodeAddress, ed25519PairFromSeed as ed25519FromSeed, encodeAddress, ethereumEncode, hdEthereum, keyExtractSuri, keyFromPath, mnemonicToLegacySeed, mnemonicToMiniSecret, secp256k1PairFromSeed as secp256k1FromSeed, sr25519PairFromSeed as sr25519FromSeed } from '@pezkuwi/util-crypto';
9
-
10
- import { createPair } from './pair/index.js';
11
- import { DEV_PHRASE } from './defaults.js';
12
- import { Pairs } from './pairs.js';
13
-
14
- const PairFromSeed = {
15
- ecdsa: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
16
- ed25519: (seed: Uint8Array): Keypair => ed25519FromSeed(seed),
17
- ethereum: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
18
- sr25519: (seed: Uint8Array): Keypair => sr25519FromSeed(seed)
19
- };
20
-
21
- function pairToPublic ({ publicKey }: KeyringPair): Uint8Array {
22
- return publicKey;
23
- }
24
-
25
- /**
26
- * # @polkadot/keyring
27
- *
28
- * ## Overview
29
- *
30
- * @name Keyring
31
- * @summary Keyring management of user accounts
32
- * @description Allows generation of keyring pairs from a variety of input combinations, such as
33
- * json object containing account address or public key, account metadata, and account encoded using
34
- * `addFromJson`, or by providing those values as arguments separately to `addFromAddress`,
35
- * or by providing the mnemonic (seed phrase) and account metadata as arguments to `addFromMnemonic`.
36
- * Stores the keyring pairs in a keyring pair dictionary. Removal of the keyring pairs from the keyring pair
37
- * dictionary is achieved using `removePair`. Retrieval of all the stored pairs via `getPairs` or perform
38
- * lookup of a pair for a given account address or public key using `getPair`. JSON metadata associated with
39
- * an account may be obtained using `toJson` accompanied by the account passphrase.
40
- */
41
- export class Keyring implements KeyringInstance {
42
- readonly #pairs: Pairs;
43
-
44
- readonly #type: KeypairType;
45
-
46
- #ss58?: number | undefined;
47
-
48
- public decodeAddress = decodeAddress;
49
-
50
- constructor (options: KeyringOptions = {}) {
51
- options.type = options.type || 'ed25519';
52
-
53
- if (!['ecdsa', 'ethereum', 'ed25519', 'sr25519'].includes(options.type || 'undefined')) {
54
- throw new Error(`Expected a keyring type of either 'ed25519', 'sr25519', 'ethereum' or 'ecdsa', found '${options.type || 'unknown'}`);
55
- }
56
-
57
- this.#pairs = new Pairs();
58
- this.#ss58 = options.ss58Format;
59
- this.#type = options.type;
60
- }
61
-
62
- /**
63
- * @description retrieve the pairs (alias for getPairs)
64
- */
65
- public get pairs (): KeyringPair[] {
66
- return this.getPairs();
67
- }
68
-
69
- /**
70
- * @description retrieve the publicKeys (alias for getPublicKeys)
71
- */
72
- public get publicKeys (): Uint8Array[] {
73
- return this.getPublicKeys();
74
- }
75
-
76
- /**
77
- * @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
78
- */
79
- public get type (): KeypairType {
80
- return this.#type;
81
- }
82
-
83
- /**
84
- * @name addPair
85
- * @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
86
- */
87
- public addPair (pair: KeyringPair): KeyringPair {
88
- return this.#pairs.add(pair);
89
- }
90
-
91
- /**
92
- * @name addFromAddress
93
- * @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
94
- * @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
95
- * the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
96
- * of an account backup), and then generates a keyring pair from them that it passes to
97
- * `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.
98
- */
99
- public addFromAddress (address: string | Uint8Array, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null, type: KeypairType = this.type, ignoreChecksum?: boolean, encType?: EncryptedJsonEncoding[]): KeyringPair {
100
- const publicKey = this.decodeAddress(address, ignoreChecksum);
101
-
102
- return this.addPair(createPair({ toSS58: this.encodeAddress, type }, { publicKey, secretKey: new Uint8Array() }, meta, encoded, encType));
103
- }
104
-
105
- /**
106
- * @name addFromJson
107
- * @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
108
- * @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
109
- * of an account backup), and then generates a keyring pair from it that it passes to
110
- * `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.
111
- */
112
- public addFromJson (json: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair {
113
- return this.addPair(this.createFromJson(json, ignoreChecksum));
114
- }
115
-
116
- /**
117
- * @name addFromMnemonic
118
- * @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
119
- * @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
120
- * argument and a metadata argument that contains account information (that may be obtained from the json file
121
- * of an account backup), and then generates a keyring pair from it that it passes to
122
- * `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.
123
- */
124
- public addFromMnemonic (mnemonic: string, meta: KeyringPair$Meta = {}, type: KeypairType = this.type, wordlist?: string[]): KeyringPair {
125
- return this.addFromUri(mnemonic, meta, type, wordlist);
126
- }
127
-
128
- /**
129
- * @name addFromPair
130
- * @summary Stores an account created from an explicit publicKey/secreteKey combination
131
- */
132
- public addFromPair (pair: Keypair, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair {
133
- return this.addPair(
134
- this.createFromPair(pair, meta, type)
135
- );
136
- }
137
-
138
- /**
139
- * @name addFromSeed
140
- * @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
141
- * @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
142
- * Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
143
- * `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.
144
- */
145
- public addFromSeed (seed: Uint8Array, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair {
146
- return this.addPair(
147
- createPair({ toSS58: this.encodeAddress, type }, PairFromSeed[type](seed), meta, null)
148
- );
149
- }
150
-
151
- /**
152
- * @name addFromUri
153
- * @summary Creates an account via an suri
154
- * @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)
155
- */
156
- public addFromUri (suri: string, meta: KeyringPair$Meta = {}, type: KeypairType = this.type, wordlist?: string[]): KeyringPair {
157
- return this.addPair(
158
- this.createFromUri(suri, meta, type, wordlist)
159
- );
160
- }
161
-
162
- /**
163
- * @name createFromJson
164
- * @description Creates a pair from a JSON keyfile
165
- */
166
- public createFromJson ({ address, encoded, encoding: { content, type, version }, meta }: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair {
167
- if (version === '3' && content[0] !== 'pkcs8') {
168
- throw new Error(`Unable to decode non-pkcs8 type, [${content.join(',')}] found}`);
169
- }
170
-
171
- const cryptoType = version === '0' || !Array.isArray(content)
172
- ? this.type
173
- : content[1];
174
- const encType = !Array.isArray(type)
175
- ? [type]
176
- : type;
177
-
178
- if (!['ed25519', 'sr25519', 'ecdsa', 'ethereum'].includes(cryptoType)) {
179
- throw new Error(`Unknown crypto type ${cryptoType}`);
180
- }
181
-
182
- // 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
183
- const publicKey = isHex(address)
184
- ? hexToU8a(address)
185
- : this.decodeAddress(address, ignoreChecksum);
186
- const decoded = isHex(encoded)
187
- ? hexToU8a(encoded)
188
- : base64Decode(encoded);
189
-
190
- return createPair({ toSS58: this.encodeAddress, type: cryptoType as KeypairType }, { publicKey, secretKey: new Uint8Array() }, meta, decoded, encType);
191
- }
192
-
193
- /**
194
- * @name createFromPair
195
- * @summary Creates a pair from an explicit publicKey/secreteKey combination
196
- */
197
- public createFromPair (pair: Keypair, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair {
198
- return createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
199
- }
200
-
201
- /**
202
- * @name createFromUri
203
- * @summary Creates a Keypair from an suri
204
- * @description This creates a pair from the suri, but does not add it to the keyring
205
- */
206
- public createFromUri (_suri: string, meta: KeyringPair$Meta = {}, type: KeypairType = this.type, wordlist?: string[]): KeyringPair {
207
- // here we only aut-add the dev phrase if we have a hard-derived path
208
- const suri = _suri.startsWith('//')
209
- ? `${DEV_PHRASE}${_suri}`
210
- : _suri;
211
- const { derivePath, password, path, phrase } = keyExtractSuri(suri);
212
- let seed: Uint8Array;
213
- const isPhraseHex = isHex(phrase, 256);
214
-
215
- if (isPhraseHex) {
216
- seed = hexToU8a(phrase);
217
- } else {
218
- const parts = phrase.split(' ');
219
-
220
- if ([12, 15, 18, 21, 24].includes(parts.length)) {
221
- seed = type === 'ethereum'
222
- ? mnemonicToLegacySeed(phrase, '', false, 64)
223
- : mnemonicToMiniSecret(phrase, password, wordlist);
224
- } else {
225
- if (phrase.length > 32) {
226
- throw new Error('specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes');
227
- }
228
-
229
- seed = stringToU8a(phrase.padEnd(32));
230
- }
231
- }
232
-
233
- const derived = type === 'ethereum'
234
- ? isPhraseHex
235
- ? PairFromSeed[type](seed) // for eth, if the private key is provided as suri, it must be derived only once
236
- : hdEthereum(seed, derivePath.substring(1))
237
- : keyFromPath(PairFromSeed[type](seed), path, type);
238
-
239
- return createPair({ toSS58: this.encodeAddress, type }, derived, meta, null);
240
- }
241
-
242
- /**
243
- * @name encodeAddress
244
- * @description Encodes the input into an ss58 representation
245
- */
246
- public encodeAddress = (address: Uint8Array | string, ss58Format?: number): string => {
247
- return this.type === 'ethereum'
248
- ? ethereumEncode(address)
249
- : encodeAddress(address, ss58Format ?? this.#ss58);
250
- };
251
-
252
- /**
253
- * @name getPair
254
- * @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
255
- * @description Returns a keyring pair value from the keyring pair dictionary by performing
256
- * a key lookup using the provided account address or public key (after decoding it).
257
- */
258
- public getPair (address: string | Uint8Array): KeyringPair {
259
- return this.#pairs.get(address);
260
- }
261
-
262
- /**
263
- * @name getPairs
264
- * @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
265
- * @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
266
- */
267
- public getPairs (): KeyringPair[] {
268
- return this.#pairs.all();
269
- }
270
-
271
- /**
272
- * @name getPublicKeys
273
- * @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
274
- * @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.
275
- */
276
- public getPublicKeys (): Uint8Array[] {
277
- return this.#pairs.all().map(pairToPublic);
278
- }
279
-
280
- /**
281
- * @name removePair
282
- * @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
283
- */
284
- public removePair (address: string | Uint8Array): void {
285
- this.#pairs.remove(address);
286
- }
287
-
288
- /**
289
- * @name setSS58Format;
290
- * @description Sets the ss58 format for the keyring
291
- */
292
- public setSS58Format (ss58: number): void {
293
- this.#ss58 = ss58;
294
- }
295
-
296
- /**
297
- * @name toJson
298
- * @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
299
- * @description Returns a JSON object containing the metadata associated with an account
300
- * when valid address or public key and when the account passphrase is provided if the account secret
301
- * is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
302
- * may backup their account to a JSON file that contains this information.
303
- */
304
- public toJson (address: string | Uint8Array, passphrase?: string): KeyringPair$Json {
305
- return this.#pairs.get(address).toJson(passphrase);
306
- }
307
- }
package/src/mod.ts DELETED
@@ -1,4 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- export * from './index.js';
@@ -1,6 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- // Do not edit, auto-generated by @polkadot/dev
5
-
6
- export const packageInfo = { name: '@polkadot/keyring', path: 'auto', type: 'auto', version: '14.0.1' };
@@ -1,26 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
-
6
- import { createTestPairs } from '../testingPairs.js';
7
-
8
- const keyring = createTestPairs({ type: 'ed25519' }, false);
9
-
10
- describe('decode', (): void => {
11
- it('fails when no data provided', (): void => {
12
- expect(
13
- (): void => keyring.alice.decodePkcs8()
14
- ).toThrow(/No encrypted data available/);
15
- });
16
-
17
- it('returns correct publicKey from encoded', (): void => {
18
- const PASS = 'testing';
19
-
20
- expect(
21
- (): void => keyring.alice.decodePkcs8(
22
- PASS, keyring.alice.encodePkcs8(PASS)
23
- )
24
- ).not.toThrow();
25
- });
26
- });
@@ -1,56 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- import type { EncryptedJsonEncoding } from '@pezkuwi/util-crypto/types';
5
-
6
- import { u8aEq } from '@pezkuwi/util';
7
- import { jsonDecryptData } from '@pezkuwi/util-crypto';
8
-
9
- import { PAIR_DIV, PAIR_HDR, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH } from './defaults.js';
10
-
11
- const SEED_OFFSET = PAIR_HDR.length;
12
-
13
- /**
14
- * Decode a pair, taking into account the generation-specific formats and headers
15
- *
16
- * For divisor/headers, don't rely on the magic being static. These will
17
- * change between generations, aka with the long-awaited 4th generation
18
- * of the format. The external decode interface is the only way to use and decode these.
19
- **/
20
- export function decodePair (passphrase?: string, encrypted?: Uint8Array | null, _encType?: EncryptedJsonEncoding | EncryptedJsonEncoding[]): { publicKey: Uint8Array; secretKey: Uint8Array } {
21
- const encType = Array.isArray(_encType) || _encType === undefined
22
- ? _encType
23
- : [_encType];
24
- const decrypted = jsonDecryptData(encrypted, passphrase, encType);
25
- const header = decrypted.subarray(0, PAIR_HDR.length);
26
-
27
- // check the start header (generations 1-3)
28
- if (!u8aEq(header, PAIR_HDR)) {
29
- throw new Error('Invalid encoding header found in body');
30
- }
31
-
32
- // setup for generation 3 format
33
- let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + SEC_LENGTH);
34
- let divOffset = SEED_OFFSET + SEC_LENGTH;
35
- let divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
36
-
37
- // old-style (generation 1 & 2), we have the seed here
38
- if (!u8aEq(divider, PAIR_DIV)) {
39
- divOffset = SEED_OFFSET + SEED_LENGTH;
40
- secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
41
- divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
42
-
43
- // check the divisior at this point (already checked for generation 3)
44
- if (!u8aEq(divider, PAIR_DIV)) {
45
- throw new Error('Invalid encoding divider found in body');
46
- }
47
- }
48
-
49
- const pubOffset = divOffset + PAIR_DIV.length;
50
- const publicKey = decrypted.subarray(pubOffset, pubOffset + PUB_LENGTH);
51
-
52
- return {
53
- publicKey,
54
- secretKey
55
- };
56
- }
@@ -1,28 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
-
6
- import { NONCE_LENGTH, SCRYPT_LENGTH } from '@pezkuwi/util-crypto/json/constants';
7
-
8
- import { createTestPairs } from '../testingPairs.js';
9
- import { PAIR_DIV, PAIR_HDR, PUB_LENGTH, SEC_LENGTH } from './defaults.js';
10
-
11
- const DECODED_LENGTH = PAIR_DIV.length + PAIR_HDR.length + PUB_LENGTH + SEC_LENGTH;
12
- const ENCODED_LENGTH = 16 + DECODED_LENGTH + NONCE_LENGTH + SCRYPT_LENGTH;
13
-
14
- const keyring = createTestPairs({ type: 'ed25519' }, false);
15
-
16
- describe('encode', (): void => {
17
- it('returns PKCS8 when no passphrase supplied', (): void => {
18
- expect(
19
- keyring.alice.encodePkcs8()
20
- ).toHaveLength(DECODED_LENGTH);
21
- });
22
-
23
- it('returns encoded PKCS8 when passphrase supplied', (): void => {
24
- expect(
25
- keyring.alice.encodePkcs8('testing')
26
- ).toHaveLength(ENCODED_LENGTH);
27
- });
28
- });
@@ -1,30 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- import type { PairInfo } from './types.js';
5
-
6
- import { u8aConcat } from '@pezkuwi/util';
7
- import { naclEncrypt, scryptEncode, scryptToU8a } from '@pezkuwi/util-crypto';
8
-
9
- import { PAIR_DIV, PAIR_HDR } from './defaults.js';
10
-
11
- /**
12
- * Encode a pair with the latest generation format (generation 3)
13
- **/
14
- export function encodePair ({ publicKey, secretKey }: PairInfo, passphrase?: string): Uint8Array {
15
- if (!secretKey) {
16
- throw new Error('Expected a valid secretKey to be passed to encode');
17
- }
18
-
19
- const encoded = u8aConcat(PAIR_HDR, secretKey, PAIR_DIV, publicKey);
20
-
21
- if (!passphrase) {
22
- return encoded;
23
- }
24
-
25
- // this is only for generation 3 (previous generations are only handled in decoding)
26
- const { params, password, salt } = scryptEncode(passphrase);
27
- const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
28
-
29
- return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
30
- }
@@ -1,189 +0,0 @@
1
- // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
-
6
- import { hexToU8a, u8aToHex } from '@pezkuwi/util';
7
- import { cryptoWaitReady, encodeAddress as toSS58, setSS58Format } from '@pezkuwi/util-crypto';
8
-
9
- import { PAIRSSR25519 } from '../testing.js';
10
- import { createTestPairs } from '../testingPairs.js';
11
- import { createPair } from './index.js';
12
-
13
- const keyring = createTestPairs({ type: 'ed25519' }, false);
14
-
15
- const TEST_ADDRESS = '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887';
16
-
17
- await cryptoWaitReady();
18
-
19
- describe('pair', (): void => {
20
- const SIGNATURE = new Uint8Array([80, 191, 198, 147, 225, 207, 75, 88, 126, 39, 129, 109, 191, 38, 72, 181, 75, 254, 81, 143, 244, 79, 237, 38, 236, 141, 28, 252, 134, 26, 169, 234, 79, 33, 153, 158, 151, 34, 175, 188, 235, 20, 35, 135, 83, 120, 139, 211, 233, 130, 1, 208, 201, 215, 73, 80, 56, 98, 185, 196, 11, 8, 193, 14]);
21
-
22
- it('has a publicKey', (): void => {
23
- expect(
24
- keyring.alice.publicKey
25
- ).toEqual(
26
- new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79])
27
- );
28
- expect(
29
- keyring.alice.addressRaw
30
- ).toEqual(
31
- new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79])
32
- );
33
- });
34
-
35
- it('allows signing', (): void => {
36
- expect(
37
- keyring.alice.sign(
38
- new Uint8Array([0x61, 0x62, 0x63, 0x64])
39
- )
40
- ).toEqual(SIGNATURE);
41
- });
42
-
43
- it('validates a correctly signed message', (): void => {
44
- expect(
45
- keyring.alice.verify(
46
- new Uint8Array([0x61, 0x62, 0x63, 0x64]),
47
- SIGNATURE,
48
- keyring.alice.publicKey
49
- )
50
- ).toEqual(true);
51
- });
52
-
53
- it('fails a correctly signed message (signer changed)', (): void => {
54
- expect(
55
- keyring.alice.verify(
56
- new Uint8Array([0x61, 0x62, 0x63, 0x64]),
57
- SIGNATURE,
58
- keyring.bob.publicKey
59
- )
60
- ).toEqual(false);
61
- });
62
-
63
- it('fails a correctly signed message (message changed)', (): void => {
64
- expect(
65
- keyring.alice.verify(
66
- new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]),
67
- SIGNATURE,
68
- keyring.alice.publicKey
69
- )
70
- ).toEqual(false);
71
- });
72
-
73
- it('allows vrf sign and verify', (): void => {
74
- const message = new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]);
75
-
76
- expect(
77
- keyring.alice.vrfVerify(
78
- message,
79
- keyring.alice.vrfSign(message),
80
- keyring.alice.publicKey
81
- )
82
- ).toBe(true);
83
- });
84
-
85
- it('fails vrf sign and verify (publicKey changed)', (): void => {
86
- const message = new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]);
87
-
88
- expect(
89
- keyring.alice.vrfVerify(
90
- message,
91
- keyring.alice.vrfSign(message),
92
- keyring.bob.publicKey
93
- )
94
- ).toBe(false);
95
- });
96
-
97
- it('allows setting/getting of meta', (): void => {
98
- keyring.bob.setMeta({ foo: 'bar', something: 'else' });
99
-
100
- expect(keyring.bob.meta).toMatchObject({ foo: 'bar', something: 'else' });
101
-
102
- keyring.bob.setMeta({ something: 'thing' });
103
-
104
- expect(keyring.bob.meta).toMatchObject({ foo: 'bar', something: 'thing' });
105
- });
106
-
107
- it('allows encoding of address with different prefixes', (): void => {
108
- expect(keyring.alice.address).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua');
109
-
110
- // eslint-disable-next-line deprecation/deprecation
111
- setSS58Format(255);
112
-
113
- expect(keyring.alice.address).toEqual('yGHU8YKprxHbHdEv7oUK4rzMZXtsdhcXVG2CAMyC9WhzhjH2k');
114
-
115
- // eslint-disable-next-line deprecation/deprecation
116
- setSS58Format(42);
117
- });
118
-
119
- it('allows getting public key after decoding', (): void => {
120
- const PASS = 'testing';
121
- const encoded = keyring.alice.encodePkcs8(PASS);
122
-
123
- const pair = createPair({ toSS58, type: 'sr25519' }, { publicKey: keyring.alice.publicKey });
124
-
125
- pair.decodePkcs8(PASS, encoded);
126
-
127
- expect(pair.isLocked).toEqual(false);
128
- });
129
-
130
- it('allows derivation on the pair', (): void => {
131
- const alice = createPair({ toSS58, type: 'sr25519' }, { publicKey: hexToU8a(PAIRSSR25519[0].p), secretKey: hexToU8a(PAIRSSR25519[0].s) }, {});
132
- const stash = alice.derive('//stash');
133
- const soft = alice.derive('//funding/0');
134
-
135
- expect(stash.publicKey).toEqual(hexToU8a(PAIRSSR25519[1].p));
136
- expect(soft.address).toEqual('5ECQNn7UueWHPFda5qUi4fTmTtyCnPvGnuoyVVSj5CboJh9J');
137
- });
138
-
139
- it('fails to sign when locked', (): void => {
140
- const pair = createPair({ toSS58, type: 'sr25519' }, { publicKey: keyring.alice.publicKey });
141
-
142
- expect(pair.isLocked).toEqual(true);
143
- expect((): Uint8Array =>
144
- pair.sign(new Uint8Array([0]))
145
- ).toThrow('Cannot sign with a locked key pair');
146
- });
147
-
148
- describe('ethereum', (): void => {
149
- const PUBLICDERIVED = new Uint8Array([
150
- 3, 129, 53, 27, 27, 70, 210, 96,
151
- 43, 9, 146, 187, 93, 85, 49, 249,
152
- 193, 105, 107, 8, 18, 254, 178, 83,
153
- 75, 104, 132, 173, 196, 126, 46, 29,
154
- 139
155
- ]);
156
- const SECRETDERIVED = new Uint8Array([
157
- 7, 13, 195, 17, 115, 0, 1, 25,
158
- 24, 226, 107, 2, 23, 105, 69, 204,
159
- 21, 195, 213, 72, 207, 73, 253, 132,
160
- 24, 217, 127, 147, 175, 105, 158, 70
161
- ]);
162
-
163
- it('has a valid address from a known public', (): void => {
164
- const pair = createPair({ toSS58, type: 'ethereum' }, { publicKey: hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077') });
165
-
166
- expect(pair.address).toEqual(TEST_ADDRESS);
167
- expect(pair.addressRaw).toEqual(hexToU8a(TEST_ADDRESS));
168
- });
169
-
170
- it('has a valid address from a known ethereum address (20 length)', (): void => {
171
- const pair = createPair({ toSS58, type: 'ethereum' }, { publicKey: new Uint8Array([75, 32, 205, 127, 248, 119, 52, 31, 46, 171, 170, 23, 158, 23, 46, 108, 95, 180, 186, 168]), secretKey: new Uint8Array([]) });
172
-
173
- expect(pair.address.toLowerCase()).toEqual('0x4b20cd7ff877341f2eabaa179e172e6c5fb4baa8');
174
- expect(pair.addressRaw).toEqual(hexToU8a('0x4b20cd7ff877341f2eabaa179e172e6c5fb4baa8'));
175
- });
176
-
177
- it('converts to json', (): void => {
178
- const pair = createPair({ toSS58, type: 'ethereum' }, { publicKey: PUBLICDERIVED, secretKey: SECRETDERIVED });
179
- const json = pair.toJson('password');
180
-
181
- expect(json.encoding).toEqual({
182
- content: ['pkcs8', 'ethereum'],
183
- type: ['scrypt', 'xsalsa20-poly1305'],
184
- version: '3'
185
- });
186
- expect(json.address).toEqual(u8aToHex(PUBLICDERIVED));
187
- });
188
- });
189
- });