@pezkuwi/keyring 14.0.6 → 14.0.10

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 (174) hide show
  1. package/build/LICENSE +201 -0
  2. package/build/README.md +17 -0
  3. package/{bundle-pezkuwi-keyring.js → build/bundle-pezkuwi-keyring.js} +1 -1
  4. package/build/bundle.js +7 -0
  5. package/build/cjs/bundle.d.ts +7 -0
  6. package/build/cjs/bundle.js +19 -0
  7. package/build/cjs/defaults.js +5 -0
  8. package/build/cjs/index.js +7 -0
  9. package/build/cjs/keyring.js +261 -0
  10. package/build/cjs/packageInfo.js +4 -0
  11. package/build/cjs/pair/decode.js +45 -0
  12. package/build/cjs/pair/defaults.js +15 -0
  13. package/build/cjs/pair/encode.js +22 -0
  14. package/build/cjs/pair/index.js +183 -0
  15. package/build/cjs/pair/nobody.js +43 -0
  16. package/build/cjs/pair/toJson.js +11 -0
  17. package/build/cjs/pair/types.js +2 -0
  18. package/build/cjs/pairs.js +28 -0
  19. package/build/cjs/testing.js +126 -0
  20. package/build/cjs/testingPairs.js +16 -0
  21. package/build/cjs/types.js +2 -0
  22. package/build/defaults.d.ts +2 -0
  23. package/build/defaults.js +2 -0
  24. package/build/index.d.ts +4 -0
  25. package/build/index.js +4 -0
  26. package/build/keyring.d.ts +145 -0
  27. package/build/keyring.js +257 -0
  28. package/build/package.json +294 -0
  29. package/build/packageDetect.d.ts +1 -0
  30. package/build/packageDetect.js +5 -0
  31. package/build/packageInfo.d.ts +6 -0
  32. package/build/packageInfo.js +1 -0
  33. package/build/pair/decode.d.ts +12 -0
  34. package/build/pair/decode.js +42 -0
  35. package/build/pair/defaults.d.ts +12 -0
  36. package/build/pair/defaults.js +12 -0
  37. package/build/pair/encode.d.ts +5 -0
  38. package/build/pair/encode.js +19 -0
  39. package/build/pair/index.d.ts +40 -0
  40. package/build/pair/index.js +180 -0
  41. package/build/pair/nobody.d.ts +2 -0
  42. package/build/pair/nobody.js +40 -0
  43. package/build/pair/toJson.d.ts +8 -0
  44. package/build/pair/toJson.js +8 -0
  45. package/build/pair/types.d.ts +5 -0
  46. package/build/pair/types.js +1 -0
  47. package/build/pairs.d.ts +8 -0
  48. package/build/pairs.js +24 -0
  49. package/build/testing.d.ts +20 -0
  50. package/build/testing.js +122 -0
  51. package/build/testingPairs.d.ts +25 -0
  52. package/build/testingPairs.js +13 -0
  53. package/build/types.d.ts +111 -0
  54. package/build/types.js +1 -0
  55. package/build-deno/README.md +17 -0
  56. package/build-deno/bundle.ts +10 -0
  57. package/build-deno/defaults.ts +4 -0
  58. package/build-deno/index.ts +8 -0
  59. package/build-deno/keyring.ts +305 -0
  60. package/build-deno/mod.ts +2 -0
  61. package/build-deno/packageDetect.ts +9 -0
  62. package/build-deno/packageInfo.ts +3 -0
  63. package/build-deno/pair/decode.ts +54 -0
  64. package/build-deno/pair/defaults.ts +18 -0
  65. package/build-deno/pair/encode.ts +28 -0
  66. package/build-deno/pair/index.ts +218 -0
  67. package/build-deno/pair/nobody.ts +58 -0
  68. package/build-deno/pair/toJson.ts +18 -0
  69. package/build-deno/pair/types.ts +6 -0
  70. package/build-deno/pairs.ts +39 -0
  71. package/build-deno/testing.ts +150 -0
  72. package/build-deno/testingPairs.ts +54 -0
  73. package/build-deno/types.ts +129 -0
  74. package/build-tsc-cjs/packageDetect.js +7 -0
  75. package/{cjs → build-tsc-cjs}/packageInfo.js +1 -1
  76. package/{packageInfo.js → build-tsc-esm/packageInfo.js} +1 -1
  77. package/package.json +9 -271
  78. package/src/bundle.ts +16 -0
  79. package/src/defaults.ts +8 -0
  80. package/src/index.spec.ts +609 -0
  81. package/src/index.ts +10 -0
  82. package/src/keyring.ts +307 -0
  83. package/src/mod.ts +4 -0
  84. package/src/packageDetect.ts +13 -0
  85. package/src/packageInfo.ts +6 -0
  86. package/src/pair/decode.spec.ts +26 -0
  87. package/src/pair/decode.ts +56 -0
  88. package/src/pair/defaults.ts +20 -0
  89. package/src/pair/encode.spec.ts +28 -0
  90. package/src/pair/encode.ts +30 -0
  91. package/src/pair/index.spec.ts +189 -0
  92. package/src/pair/index.ts +220 -0
  93. package/src/pair/nobody.ts +62 -0
  94. package/src/pair/toJson.spec.ts +42 -0
  95. package/src/pair/toJson.ts +20 -0
  96. package/src/pair/types.ts +8 -0
  97. package/src/pair/vrf.spec.ts +47 -0
  98. package/src/pairs.ts +41 -0
  99. package/src/suri.spec.ts +109 -0
  100. package/src/testing.ts +156 -0
  101. package/src/testingPairs.spec.ts +79 -0
  102. package/src/testingPairs.ts +56 -0
  103. package/src/types.ts +131 -0
  104. package/tsconfig.build.json +16 -0
  105. package/tsconfig.build.tsbuildinfo +1 -0
  106. package/tsconfig.spec.json +18 -0
  107. package/tsconfig.spec.tsbuildinfo +1 -0
  108. /package/{cjs → build}/bundle.d.ts +0 -0
  109. /package/{defaults.d.ts → build/cjs/defaults.d.ts} +0 -0
  110. /package/{index.d.ts → build/cjs/index.d.ts} +0 -0
  111. /package/{keyring.d.ts → build/cjs/keyring.d.ts} +0 -0
  112. /package/{cjs → build/cjs}/package.json +0 -0
  113. /package/{packageDetect.d.ts → build/cjs/packageDetect.d.ts} +0 -0
  114. /package/{cjs → build/cjs}/packageDetect.js +0 -0
  115. /package/{packageInfo.d.ts → build/cjs/packageInfo.d.ts} +0 -0
  116. /package/{pair → build/cjs/pair}/decode.d.ts +0 -0
  117. /package/{pair → build/cjs/pair}/defaults.d.ts +0 -0
  118. /package/{pair → build/cjs/pair}/encode.d.ts +0 -0
  119. /package/{pair → build/cjs/pair}/index.d.ts +0 -0
  120. /package/{pair → build/cjs/pair}/nobody.d.ts +0 -0
  121. /package/{pair → build/cjs/pair}/toJson.d.ts +0 -0
  122. /package/{pair → build/cjs/pair}/types.d.ts +0 -0
  123. /package/{pairs.d.ts → build/cjs/pairs.d.ts} +0 -0
  124. /package/{testing.d.ts → build/cjs/testing.d.ts} +0 -0
  125. /package/{testingPairs.d.ts → build/cjs/testingPairs.d.ts} +0 -0
  126. /package/{types.d.ts → build/cjs/types.d.ts} +0 -0
  127. /package/{bundle.d.ts → build-tsc/bundle.d.ts} +0 -0
  128. /package/{cjs → build-tsc}/defaults.d.ts +0 -0
  129. /package/{cjs → build-tsc}/index.d.ts +0 -0
  130. /package/{cjs → build-tsc}/keyring.d.ts +0 -0
  131. /package/{cjs → build-tsc}/packageDetect.d.ts +0 -0
  132. /package/{cjs → build-tsc}/packageInfo.d.ts +0 -0
  133. /package/{cjs → build-tsc}/pair/decode.d.ts +0 -0
  134. /package/{cjs → build-tsc}/pair/defaults.d.ts +0 -0
  135. /package/{cjs → build-tsc}/pair/encode.d.ts +0 -0
  136. /package/{cjs → build-tsc}/pair/index.d.ts +0 -0
  137. /package/{cjs → build-tsc}/pair/nobody.d.ts +0 -0
  138. /package/{cjs → build-tsc}/pair/toJson.d.ts +0 -0
  139. /package/{cjs → build-tsc}/pair/types.d.ts +0 -0
  140. /package/{cjs → build-tsc}/pairs.d.ts +0 -0
  141. /package/{cjs → build-tsc}/testing.d.ts +0 -0
  142. /package/{cjs → build-tsc}/testingPairs.d.ts +0 -0
  143. /package/{cjs → build-tsc}/types.d.ts +0 -0
  144. /package/{cjs → build-tsc-cjs}/bundle.js +0 -0
  145. /package/{cjs → build-tsc-cjs}/defaults.js +0 -0
  146. /package/{cjs → build-tsc-cjs}/index.js +0 -0
  147. /package/{cjs → build-tsc-cjs}/keyring.js +0 -0
  148. /package/{cjs → build-tsc-cjs}/pair/decode.js +0 -0
  149. /package/{cjs → build-tsc-cjs}/pair/defaults.js +0 -0
  150. /package/{cjs → build-tsc-cjs}/pair/encode.js +0 -0
  151. /package/{cjs → build-tsc-cjs}/pair/index.js +0 -0
  152. /package/{cjs → build-tsc-cjs}/pair/nobody.js +0 -0
  153. /package/{cjs → build-tsc-cjs}/pair/toJson.js +0 -0
  154. /package/{cjs → build-tsc-cjs}/pair/types.js +0 -0
  155. /package/{cjs → build-tsc-cjs}/pairs.js +0 -0
  156. /package/{cjs → build-tsc-cjs}/testing.js +0 -0
  157. /package/{cjs → build-tsc-cjs}/testingPairs.js +0 -0
  158. /package/{cjs → build-tsc-cjs}/types.js +0 -0
  159. /package/{bundle.js → build-tsc-esm/bundle.js} +0 -0
  160. /package/{defaults.js → build-tsc-esm/defaults.js} +0 -0
  161. /package/{index.js → build-tsc-esm/index.js} +0 -0
  162. /package/{keyring.js → build-tsc-esm/keyring.js} +0 -0
  163. /package/{packageDetect.js → build-tsc-esm/packageDetect.js} +0 -0
  164. /package/{pair → build-tsc-esm/pair}/decode.js +0 -0
  165. /package/{pair → build-tsc-esm/pair}/defaults.js +0 -0
  166. /package/{pair → build-tsc-esm/pair}/encode.js +0 -0
  167. /package/{pair → build-tsc-esm/pair}/index.js +0 -0
  168. /package/{pair → build-tsc-esm/pair}/nobody.js +0 -0
  169. /package/{pair → build-tsc-esm/pair}/toJson.js +0 -0
  170. /package/{pair → build-tsc-esm/pair}/types.js +0 -0
  171. /package/{pairs.js → build-tsc-esm/pairs.js} +0 -0
  172. /package/{testing.js → build-tsc-esm/testing.js} +0 -0
  173. /package/{testingPairs.js → build-tsc-esm/testingPairs.js} +0 -0
  174. /package/{types.js → build-tsc-esm/types.js} +0 -0
@@ -0,0 +1,189 @@
1
+ // Copyright 2017-2025 @pezkuwi/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@pezkuwi/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
+ });
@@ -0,0 +1,220 @@
1
+ // Copyright 2017-2025 @pezkuwi/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 { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } from '../types.js';
6
+ import type { PairInfo } from './types.js';
7
+
8
+ import { objectSpread, u8aConcat, u8aEmpty, u8aEq, u8aToHex, u8aToU8a } from '@pezkuwi/util';
9
+ import { blake2AsU8a, ed25519PairFromSeed as ed25519FromSeed, ed25519Sign, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, secp256k1Compress, secp256k1Expand, secp256k1PairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify, sr25519PairFromSeed as sr25519FromSeed, sr25519Sign, sr25519VrfSign, sr25519VrfVerify } from '@pezkuwi/util-crypto';
10
+
11
+ import { decodePair } from './decode.js';
12
+ import { encodePair } from './encode.js';
13
+ import { pairToJson } from './toJson.js';
14
+
15
+ interface Setup {
16
+ toSS58: (publicKey: Uint8Array) => string;
17
+ type: KeypairType;
18
+ }
19
+
20
+ const SIG_TYPE_NONE = new Uint8Array();
21
+
22
+ const TYPE_FROM_SEED = {
23
+ ecdsa: secp256k1FromSeed,
24
+ ed25519: ed25519FromSeed,
25
+ ethereum: secp256k1FromSeed,
26
+ sr25519: sr25519FromSeed
27
+ };
28
+
29
+ const TYPE_PREFIX = {
30
+ ecdsa: new Uint8Array([2]),
31
+ ed25519: new Uint8Array([0]),
32
+ ethereum: new Uint8Array([2]),
33
+ sr25519: new Uint8Array([1])
34
+ };
35
+
36
+ const TYPE_SIGNATURE = {
37
+ ecdsa: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'blake2'),
38
+ ed25519: ed25519Sign,
39
+ ethereum: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'keccak'),
40
+ sr25519: sr25519Sign
41
+ };
42
+
43
+ const TYPE_ADDRESS = {
44
+ ecdsa: (p: Uint8Array) => p.length > 32 ? blake2AsU8a(p) : p,
45
+ ed25519: (p: Uint8Array) => p,
46
+ ethereum: (p: Uint8Array) => p.length === 20 ? p : keccakAsU8a(secp256k1Expand(p)),
47
+ sr25519: (p: Uint8Array) => p
48
+ };
49
+
50
+ function isLocked (secretKey?: Uint8Array): secretKey is undefined {
51
+ return !secretKey || u8aEmpty(secretKey);
52
+ }
53
+
54
+ function vrfHash (proof: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array {
55
+ return blake2AsU8a(u8aConcat(context || '', extra || '', proof));
56
+ }
57
+
58
+ /**
59
+ * @name createPair
60
+ * @summary Creates a keyring pair object
61
+ * @description Creates a keyring pair object with provided account public key, metadata, and encoded arguments.
62
+ * The keyring pair stores the account state including the encoded address and associated metadata.
63
+ *
64
+ * It has properties whose values are functions that may be called to perform account actions:
65
+ *
66
+ * - `address` function retrieves the address associated with the account.
67
+ * - `decodedPkcs8` function is called with the account passphrase and account encoded public key.
68
+ * It decodes the encoded public key using the passphrase provided to obtain the decoded account public key
69
+ * and associated secret key that are then available in memory, and changes the account address stored in the
70
+ * state of the pair to correspond to the address of the decoded public key.
71
+ * - `encodePkcs8` function when provided with the correct passphrase associated with the account pair
72
+ * and when the secret key is in memory (when the account pair is not locked) it returns an encoded
73
+ * public key of the account.
74
+ * - `meta` is the metadata that is stored in the state of the pair, either when it was originally
75
+ * created or set via `setMeta`.
76
+ * - `publicKey` returns the public key stored in memory for the pair.
77
+ * - `sign` may be used to return a signature by signing a provided message with the secret
78
+ * key (if it is in memory) using Nacl.
79
+ * - `toJson` calls another `toJson` function and provides the state of the pair,
80
+ * it generates arguments to be passed to the other `toJson` function including an encoded public key of the account
81
+ * that it generates using the secret key from memory (if it has been made available in memory)
82
+ * and the optionally provided passphrase argument. It passes a third boolean argument to `toJson`
83
+ * indicating whether the public key has been encoded or not (if a passphrase argument was provided then it is encoded).
84
+ * The `toJson` function that it calls returns a JSON object with properties including the `address`
85
+ * and `meta` that are assigned with the values stored in the corresponding state variables of the account pair,
86
+ * an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
87
+ * property that indicates whether the public key value of the `encoded` property is encoded or not.
88
+ */
89
+ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null, encTypes?: EncryptedJsonEncoding[]): KeyringPair {
90
+ const decodePkcs8 = (passphrase?: string, userEncoded?: Uint8Array | null): void => {
91
+ const decoded = decodePair(passphrase, userEncoded || encoded, encTypes);
92
+
93
+ if (decoded.secretKey.length === 64) {
94
+ publicKey = decoded.publicKey;
95
+ secretKey = decoded.secretKey;
96
+ } else {
97
+ const pair = TYPE_FROM_SEED[type](decoded.secretKey);
98
+
99
+ publicKey = pair.publicKey;
100
+ secretKey = pair.secretKey;
101
+ }
102
+ };
103
+
104
+ const recode = (passphrase?: string): Uint8Array => {
105
+ isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
106
+
107
+ encoded = encodePair({ publicKey, secretKey }, passphrase); // re-encode, latest version
108
+ encTypes = undefined; // swap to defaults, latest version follows
109
+
110
+ return encoded;
111
+ };
112
+
113
+ const encodeAddress = (): string => {
114
+ const raw = TYPE_ADDRESS[type](publicKey);
115
+
116
+ return type === 'ethereum'
117
+ ? ethereumEncode(raw)
118
+ : toSS58(raw);
119
+ };
120
+
121
+ return {
122
+ get address (): string {
123
+ return encodeAddress();
124
+ },
125
+ get addressRaw (): Uint8Array {
126
+ const raw = TYPE_ADDRESS[type](publicKey);
127
+
128
+ return type === 'ethereum'
129
+ ? raw.slice(-20)
130
+ : raw;
131
+ },
132
+ get isLocked (): boolean {
133
+ return isLocked(secretKey);
134
+ },
135
+ get meta (): KeyringPair$Meta {
136
+ return meta;
137
+ },
138
+ get publicKey (): Uint8Array {
139
+ return publicKey;
140
+ },
141
+ get type (): KeypairType {
142
+ return type;
143
+ },
144
+ // eslint-disable-next-line sort-keys
145
+ decodePkcs8,
146
+ derive: (suri: string, meta?: KeyringPair$Meta): KeyringPair => {
147
+ if (type === 'ethereum') {
148
+ throw new Error('Unable to derive on this keypair');
149
+ } else if (isLocked(secretKey)) {
150
+ throw new Error('Cannot derive on a locked keypair');
151
+ }
152
+
153
+ const { path } = keyExtractPath(suri);
154
+ const derived = keyFromPath({ publicKey, secretKey }, path, type);
155
+
156
+ return createPair({ toSS58, type }, derived, meta, null);
157
+ },
158
+ encodePkcs8: (passphrase?: string): Uint8Array => {
159
+ return recode(passphrase);
160
+ },
161
+ lock: (): void => {
162
+ secretKey = new Uint8Array();
163
+ },
164
+ setMeta: (additional: KeyringPair$Meta): void => {
165
+ meta = objectSpread({}, meta, additional);
166
+ },
167
+ sign: (message: string | Uint8Array, options: SignOptions = {}): Uint8Array => {
168
+ if (isLocked(secretKey)) {
169
+ throw new Error('Cannot sign with a locked key pair');
170
+ }
171
+
172
+ return u8aConcat(
173
+ options.withType
174
+ ? TYPE_PREFIX[type]
175
+ : SIG_TYPE_NONE,
176
+ TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey })
177
+ );
178
+ },
179
+ toJson: (passphrase?: string): KeyringPair$Json => {
180
+ // NOTE: For ecdsa and ethereum, the publicKey cannot be extracted from the address. For these
181
+ // pass the hex-encoded publicKey through to the address portion of the JSON (before decoding)
182
+ // unless the publicKey is already an address
183
+ const address = ['ecdsa', 'ethereum'].includes(type)
184
+ ? publicKey.length === 20
185
+ ? u8aToHex(publicKey)
186
+ : u8aToHex(secp256k1Compress(publicKey))
187
+ : encodeAddress();
188
+
189
+ return pairToJson(type, { address, meta }, recode(passphrase), !!passphrase);
190
+ },
191
+ unlock: (passphrase?: string): void => {
192
+ return decodePkcs8(passphrase);
193
+ },
194
+ verify: (message: string | Uint8Array, signature: string | Uint8Array, signerPublic: string | Uint8Array): boolean => {
195
+ return signatureVerify(message, signature, TYPE_ADDRESS[type](u8aToU8a(signerPublic))).isValid;
196
+ },
197
+ vrfSign: (message: string | Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array => {
198
+ if (isLocked(secretKey)) {
199
+ throw new Error('Cannot sign with a locked key pair');
200
+ }
201
+
202
+ if (type === 'sr25519') {
203
+ return sr25519VrfSign(message, { secretKey }, context, extra);
204
+ }
205
+
206
+ const proof = TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey });
207
+
208
+ return u8aConcat(vrfHash(proof, context, extra), proof);
209
+ },
210
+ vrfVerify: (message: string | Uint8Array, vrfResult: Uint8Array, signerPublic: Uint8Array | string, context?: string | Uint8Array, extra?: string | Uint8Array): boolean => {
211
+ if (type === 'sr25519') {
212
+ return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
213
+ }
214
+
215
+ const result = signatureVerify(message, u8aConcat(TYPE_PREFIX[type], vrfResult.subarray(32)), TYPE_ADDRESS[type](u8aToU8a(signerPublic)));
216
+
217
+ return result.isValid && u8aEq(vrfResult.subarray(0, 32), vrfHash(vrfResult.subarray(32), context, extra));
218
+ }
219
+ };
220
+ }
@@ -0,0 +1,62 @@
1
+ // Copyright 2017-2025 @pezkuwi/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '../types.js';
5
+
6
+ // empty publicKey
7
+ const publicKey = new Uint8Array(32);
8
+
9
+ // pre-computed via encodeAddress(publicKey)
10
+ const address = '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM';
11
+
12
+ const meta = {
13
+ isTesting: true,
14
+ name: 'nobody'
15
+ };
16
+
17
+ const json: KeyringPair$Json = {
18
+ address,
19
+ encoded: '',
20
+ encoding: {
21
+ content: ['pkcs8', 'ed25519'],
22
+ type: 'none',
23
+ version: '0'
24
+ },
25
+ meta
26
+ };
27
+
28
+ const pair: KeyringPair = {
29
+ address,
30
+ addressRaw: publicKey,
31
+ decodePkcs8: (_passphrase?: string, _encoded?: Uint8Array): void =>
32
+ undefined,
33
+ derive: (_suri: string, _meta?: KeyringPair$Meta): KeyringPair =>
34
+ pair,
35
+ encodePkcs8: (_passphrase?: string): Uint8Array =>
36
+ new Uint8Array(0),
37
+ isLocked: true,
38
+ lock: (): void => {
39
+ // no locking, it is always locked
40
+ },
41
+ meta,
42
+ publicKey,
43
+ setMeta: (_meta: KeyringPair$Meta): void =>
44
+ undefined,
45
+ sign: (_message: Uint8Array): Uint8Array =>
46
+ new Uint8Array(64),
47
+ toJson: (_passphrase?: string): KeyringPair$Json =>
48
+ json,
49
+ type: 'ed25519',
50
+ unlock: (_passphrase?: string): void =>
51
+ undefined,
52
+ verify: (_message: Uint8Array, _signature: Uint8Array): boolean =>
53
+ false,
54
+ vrfSign: (_message: Uint8Array, _context?: string | Uint8Array, _extra?: string | Uint8Array): Uint8Array =>
55
+ new Uint8Array(96),
56
+ vrfVerify: (_message: Uint8Array, _vrfResult: Uint8Array, _context?: string | Uint8Array, _extra?: string | Uint8Array): boolean =>
57
+ false
58
+ };
59
+
60
+ export function nobody (): KeyringPair {
61
+ return pair;
62
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright 2017-2025 @pezkuwi/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@pezkuwi/dev-test/globals.d.ts" />
5
+
6
+ import { createTestPairs } from '../testingPairs.js';
7
+
8
+ const keyring = createTestPairs({ type: 'ed25519' }, false);
9
+
10
+ describe('toJson', (): void => {
11
+ it('creates an unencoded output with no passphrase', (): void => {
12
+ expect(
13
+ keyring.alice.toJson()
14
+ ).toMatchObject({
15
+ address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua',
16
+ encoded: 'MFMCAQEwBQYDK2VwBCIEIEFsaWNlICAgICAgICAgICAgICAgICAgICAgICAgICAg0XKnTNpMhlkSwyugqApXrmmrrkEOXMtZ3uhOL0Qy20+hIwMhANFyp0zaTIZZEsMroKgKV65pq65BDlzLWd7oTi9EMttP',
17
+ encoding: {
18
+ content: ['pkcs8', 'ed25519'],
19
+ type: ['none'],
20
+ version: '3'
21
+ },
22
+ meta: {
23
+ isTesting: true,
24
+ name: 'alice'
25
+ }
26
+ });
27
+ });
28
+
29
+ it('creates an encoded output with passphrase', (): void => {
30
+ const json = keyring.alice.toJson('testing');
31
+
32
+ expect(json.encoded).toHaveLength(268);
33
+ expect(json).toMatchObject({
34
+ address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua',
35
+ encoding: {
36
+ content: ['pkcs8', 'ed25519'],
37
+ type: ['scrypt', 'xsalsa20-poly1305'],
38
+ version: '3'
39
+ }
40
+ });
41
+ });
42
+ });
@@ -0,0 +1,20 @@
1
+ // Copyright 2017-2025 @pezkuwi/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
5
+ import type { KeyringPair$Json, KeyringPair$Meta } from '../types.js';
6
+
7
+ import { objectSpread } from '@pezkuwi/util';
8
+ import { jsonEncryptFormat } from '@pezkuwi/util-crypto';
9
+
10
+ interface PairStateJson {
11
+ address: string;
12
+ meta: KeyringPair$Meta;
13
+ }
14
+
15
+ export function pairToJson (type: KeypairType, { address, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
16
+ return objectSpread(jsonEncryptFormat(encoded, ['pkcs8', type], isEncrypted), {
17
+ address,
18
+ meta
19
+ });
20
+ }
@@ -0,0 +1,8 @@
1
+ // Copyright 2017-2025 @pezkuwi/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export interface PairInfo {
5
+ publicKey: Uint8Array;
6
+ secretKey?: Uint8Array | undefined;
7
+ seed?: Uint8Array | null;
8
+ }
@@ -0,0 +1,47 @@
1
+ // Copyright 2017-2025 @pezkuwi/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@pezkuwi/dev-test/globals.d.ts" />
5
+
6
+ import { u8aToHex } from '@pezkuwi/util';
7
+ import { cryptoWaitReady, ed25519PairFromSeed, encodeAddress as toSS58, randomAsU8a, secp256k1PairFromSeed, sr25519PairFromSeed } from '@pezkuwi/util-crypto';
8
+
9
+ import { createPair } from './index.js';
10
+
11
+ const MESSAGE = 'this is a test message';
12
+ const CONTEXT = 'some context';
13
+
14
+ await cryptoWaitReady();
15
+
16
+ const ecdsa = createPair({ toSS58, type: 'ecdsa' }, secp256k1PairFromSeed(randomAsU8a()));
17
+ const ed25519 = createPair({ toSS58, type: 'ed25519' }, ed25519PairFromSeed(randomAsU8a()));
18
+ const sr25519 = createPair({ toSS58, type: 'sr25519' }, sr25519PairFromSeed(randomAsU8a()));
19
+
20
+ describe('vrf', (): void => {
21
+ it('has deterministic signature values for ecdsa', (): void => {
22
+ const sig1 = ecdsa.vrfSign(MESSAGE, CONTEXT);
23
+ const sig2 = ecdsa.vrfSign(MESSAGE, CONTEXT);
24
+
25
+ expect(u8aToHex(sig1)).toEqual(u8aToHex(sig2));
26
+ expect(ecdsa.vrfVerify(MESSAGE, sig1, ecdsa.publicKey, CONTEXT)).toEqual(true);
27
+ expect(ecdsa.vrfVerify(MESSAGE, sig2, ecdsa.publicKey, CONTEXT)).toEqual(true);
28
+ });
29
+
30
+ it('has deterministic signature values for ed25519', (): void => {
31
+ const sig1 = ed25519.vrfSign(MESSAGE, CONTEXT);
32
+ const sig2 = ed25519.vrfSign(MESSAGE, CONTEXT);
33
+
34
+ expect(u8aToHex(sig1)).toEqual(u8aToHex(sig2));
35
+ expect(ed25519.vrfVerify(MESSAGE, sig1, ed25519.publicKey, CONTEXT)).toEqual(true);
36
+ expect(ed25519.vrfVerify(MESSAGE, sig2, ed25519.publicKey, CONTEXT)).toEqual(true);
37
+ });
38
+
39
+ it('has deterministic signature values for sr25519', (): void => {
40
+ const sig1 = sr25519.vrfSign(MESSAGE, CONTEXT);
41
+ const sig2 = sr25519.vrfSign(MESSAGE, CONTEXT);
42
+
43
+ expect(u8aToHex(sig1.slice(0, 32))).toEqual(u8aToHex(sig2.slice(0, 32)));
44
+ expect(sr25519.vrfVerify(MESSAGE, sig1, sr25519.publicKey, CONTEXT)).toEqual(true);
45
+ expect(sr25519.vrfVerify(MESSAGE, sig2, sr25519.publicKey, CONTEXT)).toEqual(true);
46
+ });
47
+ });
package/src/pairs.ts ADDED
@@ -0,0 +1,41 @@
1
+ // Copyright 2017-2025 @pezkuwi/keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { KeyringPair, KeyringPairs } from './types.js';
5
+
6
+ import { isHex, isU8a, u8aToHex, u8aToU8a } from '@pezkuwi/util';
7
+ import { decodeAddress } from '@pezkuwi/util-crypto';
8
+
9
+ type KeyringPairMap = Record<string, KeyringPair>;
10
+
11
+ export class Pairs implements KeyringPairs {
12
+ readonly #map: KeyringPairMap = {};
13
+
14
+ public add (pair: KeyringPair): KeyringPair {
15
+ this.#map[decodeAddress(pair.address).toString()] = pair;
16
+
17
+ return pair;
18
+ }
19
+
20
+ public all (): KeyringPair[] {
21
+ return Object.values(this.#map);
22
+ }
23
+
24
+ public get (address: string | Uint8Array): KeyringPair {
25
+ const pair = this.#map[decodeAddress(address).toString()];
26
+
27
+ if (!pair) {
28
+ throw new Error(`Unable to retrieve keypair '${
29
+ isU8a(address) || isHex(address)
30
+ ? u8aToHex(u8aToU8a(address))
31
+ : address
32
+ }'`);
33
+ }
34
+
35
+ return pair;
36
+ }
37
+
38
+ public remove (address: string | Uint8Array): void {
39
+ delete this.#map[decodeAddress(address).toString()];
40
+ }
41
+ }