@prosopo/keyring 2.9.0 → 2.9.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { KeyringOptions, KeyringPair } from "@prosopo/types";
5
+ import type { KeypairType } from "@prosopo/util-crypto";
6
+
7
+ import { nobody } from "../pair/nobody.js";
8
+ import { createTestKeyring } from "./testing.js";
9
+
10
+ export interface TestKeyringMap {
11
+ nobody: KeyringPair;
12
+
13
+ [index: string]: KeyringPair;
14
+ }
15
+
16
+ export interface TestKeyringMapSubstrate extends TestKeyringMap {
17
+ alice: KeyringPair;
18
+ bob: KeyringPair;
19
+ charlie: KeyringPair;
20
+ dave: KeyringPair;
21
+ eve: KeyringPair;
22
+ ferdie: KeyringPair;
23
+ }
24
+
25
+ export interface TestKeyringMapEthereum extends TestKeyringMap {
26
+ Alith: KeyringPair;
27
+ Baltathar: KeyringPair;
28
+ Charleth: KeyringPair;
29
+ Dorothy: KeyringPair;
30
+ Ethan: KeyringPair;
31
+ Faith: KeyringPair;
32
+ }
33
+
34
+ export type DetectMap<O extends KeyringOptions | undefined> =
35
+ DetectPairType<O> extends "ethereum"
36
+ ? TestKeyringMapEthereum
37
+ : TestKeyringMapSubstrate;
38
+
39
+ export type DetectPairType<O extends KeyringOptions | undefined> =
40
+ O extends KeyringOptions
41
+ ? O["type"] extends KeypairType
42
+ ? O["type"]
43
+ : "sr25519"
44
+ : "sr25519";
45
+
46
+ export function createTestPairs<O extends KeyringOptions, M = DetectMap<O>>(
47
+ options?: O,
48
+ isDerived = true,
49
+ ): M {
50
+ const keyring = createTestKeyring(options, isDerived);
51
+ const pairs = keyring.getPairs();
52
+ const map: TestKeyringMap = { nobody: nobody() };
53
+
54
+ for (const p of pairs) {
55
+ if (p.meta.name) {
56
+ map[p.meta.name] = p;
57
+ }
58
+ }
59
+
60
+ return map as M;
61
+ }
@@ -0,0 +1,23 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { describe, expect, it } from "vitest";
5
+ import { createTestPairs } from "../keyring/testingPairs.js";
6
+
7
+ const keyring = createTestPairs({ type: "sr25519" }, false);
8
+
9
+ describe("decode", (): void => {
10
+ it("fails when no data provided", (): void => {
11
+ expect((): void => keyring.alice.decodePkcs8()).toThrow(
12
+ /(No encrypted data available|Password required)/,
13
+ );
14
+ });
15
+
16
+ it("returns correct publicKey from encoded", (): void => {
17
+ const PASS = "testing";
18
+
19
+ expect((): void =>
20
+ keyring.alice.decodePkcs8(PASS, keyring.alice.encodePkcs8(PASS)),
21
+ ).not.toThrow();
22
+ });
23
+ });
@@ -0,0 +1,65 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { EncryptedJsonEncoding } from "@polkadot/util-crypto/types";
5
+
6
+ import { u8aEq } from "@polkadot/util";
7
+ import { jsonDecryptData } from "@prosopo/util-crypto";
8
+
9
+ import {
10
+ PAIR_DIV,
11
+ PAIR_HDR,
12
+ PUB_LENGTH,
13
+ SEC_LENGTH,
14
+ SEED_LENGTH,
15
+ } from "./defaults.js";
16
+
17
+ const SEED_OFFSET = PAIR_HDR.length;
18
+
19
+ /**
20
+ * Decode a pair, taking into account the generation-specific formats and headers
21
+ *
22
+ * For divisor/headers, don't rely on the magic being static. These will
23
+ * change between generations, aka with the long-awaited 4th generation
24
+ * of the format. The external decode interface is the only way to use and decode these.
25
+ **/
26
+ export function decodePair(
27
+ passphrase?: string,
28
+ encrypted?: Uint8Array | null,
29
+ _encType?: EncryptedJsonEncoding | EncryptedJsonEncoding[],
30
+ ): { publicKey: Uint8Array; secretKey: Uint8Array } {
31
+ const encType =
32
+ Array.isArray(_encType) || _encType === undefined ? _encType : [_encType];
33
+ const decrypted = jsonDecryptData(encrypted, passphrase, encType);
34
+ const header = decrypted.subarray(0, PAIR_HDR.length);
35
+
36
+ // check the start header (generations 1-3)
37
+ if (!u8aEq(header, PAIR_HDR)) {
38
+ throw new Error("Invalid encoding header found in body");
39
+ }
40
+
41
+ // setup for generation 3 format
42
+ let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + SEC_LENGTH);
43
+ let divOffset = SEED_OFFSET + SEC_LENGTH;
44
+ let divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
45
+
46
+ // old-style (generation 1 & 2), we have the seed here
47
+ if (!u8aEq(divider, PAIR_DIV)) {
48
+ divOffset = SEED_OFFSET + SEED_LENGTH;
49
+ secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
50
+ divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
51
+
52
+ // check the divisior at this point (already checked for generation 3)
53
+ if (!u8aEq(divider, PAIR_DIV)) {
54
+ throw new Error("Invalid encoding divider found in body");
55
+ }
56
+ }
57
+
58
+ const pubOffset = divOffset + PAIR_DIV.length;
59
+ const publicKey = decrypted.subarray(pubOffset, pubOffset + PUB_LENGTH);
60
+
61
+ return {
62
+ publicKey,
63
+ secretKey,
64
+ };
65
+ }
@@ -0,0 +1,22 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /** public/secret section divider (generation 1-3, will change in 4, don't rely on value) */
5
+ export const PAIR_DIV = new Uint8Array([161, 35, 3, 33, 0]);
6
+
7
+ /** public/secret start block (generation 1-3, will change in 4, don't rely on value) */
8
+ export const PAIR_HDR = new Uint8Array([
9
+ 48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32,
10
+ ]);
11
+
12
+ /** length of a public key */
13
+ export const PUB_LENGTH = 32;
14
+
15
+ /** length of a salt */
16
+ export const SALT_LENGTH = 32;
17
+
18
+ /** length of a secret key */
19
+ export const SEC_LENGTH = 64;
20
+
21
+ /** length of a user-input seed */
22
+ export const SEED_LENGTH = 32;
@@ -0,0 +1,24 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { NONCE_LENGTH, SCRYPT_LENGTH } from "@prosopo/util-crypto";
5
+ import { describe, expect, it } from "vitest";
6
+
7
+ import { createTestPairs } from "../keyring/testingPairs.js";
8
+ import { PAIR_DIV, PAIR_HDR, PUB_LENGTH, SEC_LENGTH } from "./defaults.js";
9
+
10
+ const DECODED_LENGTH =
11
+ 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: "sr25519" }, false);
15
+
16
+ describe("encode", (): void => {
17
+ it("returns PKCS8 when no passphrase supplied", (): void => {
18
+ expect(keyring.alice.encodePkcs8()).toHaveLength(DECODED_LENGTH);
19
+ });
20
+
21
+ it("returns encoded PKCS8 when passphrase supplied", (): void => {
22
+ expect(keyring.alice.encodePkcs8("testing")).toHaveLength(ENCODED_LENGTH);
23
+ });
24
+ });
@@ -0,0 +1,32 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { u8aConcat } from "@polkadot/util";
5
+ import type { PairInfo } from "@prosopo/types";
6
+ import { naclEncrypt, scryptEncode, scryptToU8a } from "@prosopo/util-crypto";
7
+
8
+ import { PAIR_DIV, PAIR_HDR } from "./defaults.js";
9
+
10
+ /**
11
+ * Encode a pair with the latest generation format (generation 3)
12
+ **/
13
+ export function encodePair(
14
+ { publicKey, secretKey }: PairInfo,
15
+ passphrase?: string,
16
+ ): Uint8Array {
17
+ if (!secretKey) {
18
+ throw new Error("Expected a valid secretKey to be passed to encode");
19
+ }
20
+
21
+ const encoded = u8aConcat(PAIR_HDR, secretKey, PAIR_DIV, publicKey);
22
+
23
+ if (!passphrase) {
24
+ return encoded;
25
+ }
26
+
27
+ // this is only for generation 3 (previous generations are only handled in decoding)
28
+ const { params, password, salt } = scryptEncode(passphrase);
29
+ const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
30
+
31
+ return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
32
+ }
@@ -0,0 +1,304 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ KeyringPair,
6
+ KeyringPair$Json,
7
+ KeyringPair$Meta,
8
+ SignOptions,
9
+ } from "@prosopo/types";
10
+ import type { PairInfo } from "@prosopo/types";
11
+
12
+ import {
13
+ objectSpread,
14
+ u8aConcat,
15
+ u8aEmpty,
16
+ u8aToHex,
17
+ u8aToU8a,
18
+ } from "@polkadot/util";
19
+
20
+ import {
21
+ type JWT,
22
+ blake2AsU8a,
23
+ jwtVerify,
24
+ sr25519jwtIssue,
25
+ } from "@prosopo/util-crypto";
26
+ import type { EncryptedJsonEncoding } from "@prosopo/util-crypto";
27
+ import { keyExtractPath, keyFromPath } from "@prosopo/util-crypto";
28
+ import { secp256k1Compress } from "@prosopo/util-crypto";
29
+ import { signatureVerify } from "@prosopo/util-crypto";
30
+ import { sr25519FromSeed, sr25519Sign } from "@prosopo/util-crypto";
31
+ import { sr25519VrfSign, sr25519VrfVerify } from "@prosopo/util-crypto";
32
+ import type { JWTVerifyResult } from "@prosopo/util-crypto";
33
+ import { decodePair } from "./decode.js";
34
+ import { encodePair } from "./encode.js";
35
+ import { pairToJson } from "./toJson.js";
36
+
37
+ type KeypairType = "sr25519" | "ed25519" | "ecdsa" | "ethereum";
38
+
39
+ interface Setup {
40
+ toSS58: (publicKey: Uint8Array) => string;
41
+ type: KeypairType;
42
+ }
43
+
44
+ const SIG_TYPE_NONE = new Uint8Array([]);
45
+
46
+ const TYPE_FROM_SEED = {
47
+ sr25519: sr25519FromSeed,
48
+ ed25519: () => {
49
+ throw new Error("Not Implemented");
50
+ },
51
+ ecdsa: () => {
52
+ throw new Error("Not Implemented");
53
+ },
54
+ ethereum: () => {
55
+ throw new Error("Not Implemented");
56
+ },
57
+ };
58
+
59
+ const TYPE_PREFIX = {
60
+ ecdsa: new Uint8Array([2]),
61
+ ed25519: new Uint8Array([0]),
62
+ ethereum: new Uint8Array([2]),
63
+ sr25519: new Uint8Array([1]),
64
+ };
65
+
66
+ const TYPE_SIGNATURE = {
67
+ sr25519: sr25519Sign,
68
+ ed25519: () => {
69
+ throw new Error("Not Implemented");
70
+ },
71
+ ecdsa: () => {
72
+ throw new Error("Not Implemented");
73
+ },
74
+ ethereum: () => {
75
+ throw new Error("Not Implemented");
76
+ },
77
+ };
78
+
79
+ const TYPE_JWT_ISSUE = {
80
+ sr25519: sr25519jwtIssue,
81
+ ed25519: () => {
82
+ throw new Error("Not Implemented");
83
+ },
84
+ ecdsa: () => {
85
+ throw new Error("Not Implemented");
86
+ },
87
+ ethereum: () => {
88
+ throw new Error("Not Implemented");
89
+ },
90
+ };
91
+
92
+ const TYPE_ADDRESS = {
93
+ sr25519: (p: Uint8Array) => p,
94
+ ed25519: () => {
95
+ throw new Error("Not Implemented");
96
+ },
97
+ ecdsa: () => {
98
+ throw new Error("Not Implemented");
99
+ },
100
+ ethereum: () => {
101
+ throw new Error("Not Implemented");
102
+ },
103
+ };
104
+
105
+ function isLocked(secretKey?: Uint8Array): secretKey is undefined {
106
+ return !secretKey || u8aEmpty(secretKey);
107
+ }
108
+
109
+ function vrfHash(
110
+ proof: Uint8Array,
111
+ context?: string | Uint8Array,
112
+ extra?: string | Uint8Array,
113
+ ): Uint8Array {
114
+ return blake2AsU8a(u8aConcat(context || "", extra || "", proof));
115
+ }
116
+
117
+ /**
118
+ * @name createPair
119
+ * @summary Creates a keyring pair object
120
+ * @description Creates a keyring pair object with provided account public key, metadata, and encoded arguments.
121
+ * The keyring pair stores the account state including the encoded address and associated metadata.
122
+ *
123
+ * It has properties whose values are functions that may be called to perform account actions:
124
+ *
125
+ * - `address` function retrieves the address associated with the account.
126
+ * - `decodedPkcs8` function is called with the account passphrase and account encoded public key.
127
+ * It decodes the encoded public key using the passphrase provided to obtain the decoded account public key
128
+ * and associated secret key that are then available in memory, and changes the account address stored in the
129
+ * state of the pair to correspond to the address of the decoded public key.
130
+ * - `encodePkcs8` function when provided with the correct passphrase associated with the account pair
131
+ * and when the secret key is in memory (when the account pair is not locked) it returns an encoded
132
+ * public key of the account.
133
+ * - `meta` is the metadata that is stored in the state of the pair, either when it was originally
134
+ * created or set via `setMeta`.
135
+ * - `publicKey` returns the public key stored in memory for the pair.
136
+ * - `sign` may be used to return a signature by signing a provided message with the secret
137
+ * key (if it is in memory) using Nacl.
138
+ * - `toJson` calls another `toJson` function and provides the state of the pair,
139
+ * it generates arguments to be passed to the other `toJson` function including an encoded public key of the account
140
+ * that it generates using the secret key from memory (if it has been made available in memory)
141
+ * and the optionally provided passphrase argument. It passes a third boolean argument to `toJson`
142
+ * indicating whether the public key has been encoded or not (if a passphrase argument was provided then it is encoded).
143
+ * The `toJson` function that it calls returns a JSON object with properties including the `address`
144
+ * and `meta` that are assigned with the values stored in the corresponding state variables of the account pair,
145
+ * an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
146
+ * property that indicates whether the public key value of the `encoded` property is encoded or not.
147
+ */
148
+ export function createPair(
149
+ { toSS58, type }: Setup,
150
+ { publicKey, secretKey }: PairInfo,
151
+ meta: KeyringPair$Meta = {},
152
+ encoded: Uint8Array | null = null,
153
+ encTypes?: EncryptedJsonEncoding[],
154
+ ): KeyringPair {
155
+ const decodePkcs8 = (
156
+ passphrase?: string,
157
+ userEncoded?: Uint8Array | null,
158
+ ): void => {
159
+ const decoded = decodePair(passphrase, userEncoded || encoded, encTypes);
160
+
161
+ if (decoded.secretKey.length === 64) {
162
+ publicKey = decoded.publicKey;
163
+ secretKey = decoded.secretKey;
164
+ } else {
165
+ const pair = TYPE_FROM_SEED[type](decoded.secretKey);
166
+
167
+ publicKey = pair.publicKey;
168
+ secretKey = pair.secretKey;
169
+ }
170
+ };
171
+
172
+ const recode = (passphrase?: string): Uint8Array => {
173
+ isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
174
+
175
+ encoded = encodePair({ publicKey, secretKey }, passphrase); // re-encode, latest version
176
+ encTypes = undefined; // swap to defaults, latest version follows
177
+
178
+ return encoded;
179
+ };
180
+
181
+ const encodeAddress = (): string => {
182
+ const raw = TYPE_ADDRESS[type](publicKey);
183
+
184
+ return toSS58(raw);
185
+ };
186
+
187
+ return {
188
+ get address(): string {
189
+ return encodeAddress();
190
+ },
191
+ get addressRaw(): Uint8Array {
192
+ return TYPE_ADDRESS[type](publicKey);
193
+ },
194
+ get isLocked(): boolean {
195
+ return isLocked(secretKey);
196
+ },
197
+ get meta(): KeyringPair$Meta {
198
+ return meta;
199
+ },
200
+ get publicKey(): Uint8Array {
201
+ return publicKey;
202
+ },
203
+ get type(): KeypairType {
204
+ return type;
205
+ },
206
+ // eslint-disable-next-line sort-keys
207
+ decodePkcs8,
208
+ derive: (suri: string, meta?: KeyringPair$Meta): KeyringPair => {
209
+ if (isLocked(secretKey)) {
210
+ throw new Error("Cannot derive on a locked keypair");
211
+ }
212
+
213
+ const { path } = keyExtractPath(suri);
214
+ const derived = keyFromPath({ publicKey, secretKey }, path, type);
215
+
216
+ return createPair({ toSS58, type }, derived, meta, null);
217
+ },
218
+ encodePkcs8: (passphrase?: string): Uint8Array => {
219
+ return recode(passphrase);
220
+ },
221
+ jwtIssue: (
222
+ options?: { expiresIn?: number; notBefore?: number },
223
+ message?: { [key: string]: string },
224
+ ): JWT => {
225
+ if (isLocked(secretKey)) {
226
+ throw new Error("Cannot sign with a locked key pair");
227
+ }
228
+ return TYPE_JWT_ISSUE[type]({ publicKey, secretKey }, options, message);
229
+ },
230
+ jwtVerify: (jwt: JWT): JWTVerifyResult => {
231
+ return jwtVerify(jwt, publicKey);
232
+ },
233
+ lock: (): void => {
234
+ secretKey = new Uint8Array([]);
235
+ },
236
+ setMeta: (additional: KeyringPair$Meta): void => {
237
+ meta = objectSpread({}, meta, additional);
238
+ },
239
+ sign: (
240
+ message: string | Uint8Array,
241
+ options: SignOptions = {},
242
+ ): Uint8Array => {
243
+ if (isLocked(secretKey)) {
244
+ throw new Error("Cannot sign with a locked key pair");
245
+ }
246
+
247
+ return u8aConcat(
248
+ options.withType ? TYPE_PREFIX[type] : SIG_TYPE_NONE,
249
+ TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey }),
250
+ );
251
+ },
252
+ toJson: (passphrase?: string): KeyringPair$Json => {
253
+ // NOTE: For ecdsa and ethereum, the publicKey cannot be extracted from the address. For these
254
+ // pass the hex-encoded publicKey through to the address portion of the JSON (before decoding)
255
+ // unless the publicKey is already an address
256
+ const address = ["ecdsa", "ethereum"].includes(type)
257
+ ? publicKey.length === 20
258
+ ? u8aToHex(publicKey)
259
+ : u8aToHex(secp256k1Compress(publicKey))
260
+ : encodeAddress();
261
+
262
+ return pairToJson(
263
+ type,
264
+ { address, meta },
265
+ recode(passphrase),
266
+ !!passphrase,
267
+ );
268
+ },
269
+ unlock: (passphrase?: string): void => {
270
+ decodePkcs8(passphrase);
271
+ },
272
+ verify: (
273
+ message: string | Uint8Array,
274
+ signature: string | Uint8Array,
275
+ signerPublic: string | Uint8Array,
276
+ ): boolean => {
277
+ return signatureVerify(
278
+ message,
279
+ signature,
280
+ TYPE_ADDRESS[type](u8aToU8a(signerPublic)),
281
+ ).isValid;
282
+ },
283
+ vrfSign: (
284
+ message: string | Uint8Array,
285
+ context?: string | Uint8Array,
286
+ extra?: string | Uint8Array,
287
+ ): Uint8Array => {
288
+ if (isLocked(secretKey)) {
289
+ throw new Error("Cannot sign with a locked key pair");
290
+ }
291
+
292
+ return sr25519VrfSign(message, { secretKey }, context, extra);
293
+ },
294
+ vrfVerify: (
295
+ message: string | Uint8Array,
296
+ vrfResult: Uint8Array,
297
+ signerPublic: Uint8Array | string,
298
+ context?: string | Uint8Array,
299
+ extra?: string | Uint8Array,
300
+ ): boolean => {
301
+ return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
302
+ },
303
+ };
304
+ }
@@ -0,0 +1,78 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ KeyringPair,
6
+ KeyringPair$Json,
7
+ KeyringPair$Meta,
8
+ } from "@prosopo/types";
9
+ import type { JWT, JWTVerifyResult } from "@prosopo/util-crypto";
10
+
11
+ // empty publicKey
12
+ const publicKey = new Uint8Array(32);
13
+
14
+ // pre-computed via encodeAddress(publicKey)
15
+ const address = "5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM";
16
+
17
+ const meta = {
18
+ isTesting: true,
19
+ name: "nobody",
20
+ };
21
+
22
+ const json: KeyringPair$Json = {
23
+ address,
24
+ encoded: "",
25
+ encoding: {
26
+ content: ["pkcs8", "ed25519"],
27
+ type: "none",
28
+ version: "0",
29
+ },
30
+ meta,
31
+ };
32
+
33
+ const pair: KeyringPair = {
34
+ address,
35
+ addressRaw: publicKey,
36
+ decodePkcs8: (_passphrase?: string, _encoded?: Uint8Array): void => undefined,
37
+ derive: (_suri: string, _meta?: KeyringPair$Meta): KeyringPair => pair,
38
+ encodePkcs8: (_passphrase?: string): Uint8Array => new Uint8Array(0),
39
+ isLocked: true,
40
+ jwtIssue: (_options?: { expiresIn?: number; notBefore?: number }): JWT =>
41
+ "jwt.dummy.token",
42
+ jwtVerify: (
43
+ _jwt: JWT,
44
+ _options?: { ignoreExpiration?: boolean; ignoreNotBefore?: boolean },
45
+ ): JWTVerifyResult => ({
46
+ isValid: false,
47
+ error: "JWT verification failed",
48
+ crypto: "sr25519",
49
+ publicKey,
50
+ isWrapped: false,
51
+ }),
52
+ lock: (): void => {
53
+ // no locking, it is always locked
54
+ },
55
+ meta,
56
+ publicKey,
57
+ setMeta: (_meta: KeyringPair$Meta): void => undefined,
58
+ sign: (_message: Uint8Array): Uint8Array => new Uint8Array(64),
59
+ toJson: (_passphrase?: string): KeyringPair$Json => json,
60
+ type: "sr25519",
61
+ unlock: (_passphrase?: string): void => undefined,
62
+ verify: (_message: Uint8Array, _signature: Uint8Array): boolean => false,
63
+ vrfSign: (
64
+ _message: Uint8Array,
65
+ _context?: string | Uint8Array,
66
+ _extra?: string | Uint8Array,
67
+ ): Uint8Array => new Uint8Array(96),
68
+ vrfVerify: (
69
+ _message: Uint8Array,
70
+ _vrfResult: Uint8Array,
71
+ _context?: string | Uint8Array,
72
+ _extra?: string | Uint8Array,
73
+ ): boolean => false,
74
+ };
75
+
76
+ export function nobody(): KeyringPair {
77
+ return pair;
78
+ }
@@ -0,0 +1,40 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { describe, expect, it } from "vitest";
5
+ import { createTestPairs } from "../keyring/testingPairs.js";
6
+
7
+ const keyring = createTestPairs({ type: "sr25519" }, false);
8
+
9
+ describe("toJson", (): void => {
10
+ it("creates an unencoded output with no passphrase", (): void => {
11
+ expect(keyring.alice.toJson()).toMatchObject({
12
+ address: "5Engs9f8Gk6JqvVWz3kFyJ8Kqkgx7pLi8C1UTcr7EZ855qBQ",
13
+ encoded:
14
+ "MFMCAQEwBQYDK2VwBCIEIHipoQ68w1cHP0Tju+ym3lzqc2fma5FCMXgDqwBDqTtU2pOQGFg2vA+4oVIIZMphcnOugCZhNuyAfxQ4r1OyWsahIwMhAHh9b36VcuIWVvYdPYl8NDyAyBt3Sx125cjHJVK3zLwl",
15
+ encoding: {
16
+ content: ["pkcs8", "sr25519"],
17
+ type: ["none"],
18
+ version: "3",
19
+ },
20
+ meta: {
21
+ isTesting: true,
22
+ name: "alice",
23
+ },
24
+ });
25
+ });
26
+
27
+ it("creates an encoded output with passphrase", (): void => {
28
+ const json = keyring.alice.toJson("testing");
29
+
30
+ expect(json.encoded).toHaveLength(268);
31
+ expect(json).toMatchObject({
32
+ address: "5Engs9f8Gk6JqvVWz3kFyJ8Kqkgx7pLi8C1UTcr7EZ855qBQ",
33
+ encoding: {
34
+ content: ["pkcs8", "sr25519"],
35
+ type: ["scrypt", "xsalsa20-poly1305"],
36
+ version: "3",
37
+ },
38
+ });
39
+ });
40
+ });
@@ -0,0 +1,28 @@
1
+ // Copyright 2017-2025 @polkadot/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { KeyringPair$Json, KeyringPair$Meta } from "@prosopo/types";
5
+ import type { KeypairType } from "@prosopo/util-crypto";
6
+
7
+ import { objectSpread } from "@polkadot/util";
8
+ import { jsonEncryptFormat } from "@prosopo/util-crypto";
9
+
10
+ interface PairStateJson {
11
+ address: string;
12
+ meta: KeyringPair$Meta;
13
+ }
14
+
15
+ export function pairToJson(
16
+ type: KeypairType,
17
+ { address, meta }: PairStateJson,
18
+ encoded: Uint8Array,
19
+ isEncrypted: boolean,
20
+ ): KeyringPair$Json {
21
+ return objectSpread(
22
+ jsonEncryptFormat(encoded, ["pkcs8", type], isEncrypted),
23
+ {
24
+ address,
25
+ meta,
26
+ },
27
+ );
28
+ }