@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,19 @@
1
+ import { u8aConcat } from '@pezkuwi/util';
2
+ import { naclEncrypt, scryptEncode, scryptToU8a } from '@pezkuwi/util-crypto';
3
+ import { PAIR_DIV, PAIR_HDR } from './defaults.js';
4
+ /**
5
+ * Encode a pair with the latest generation format (generation 3)
6
+ **/
7
+ export function encodePair({ publicKey, secretKey }, passphrase) {
8
+ if (!secretKey) {
9
+ throw new Error('Expected a valid secretKey to be passed to encode');
10
+ }
11
+ const encoded = u8aConcat(PAIR_HDR, secretKey, PAIR_DIV, publicKey);
12
+ if (!passphrase) {
13
+ return encoded;
14
+ }
15
+ // this is only for generation 3 (previous generations are only handled in decoding)
16
+ const { params, password, salt } = scryptEncode(passphrase);
17
+ const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
18
+ return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
19
+ }
@@ -0,0 +1,40 @@
1
+ import type { EncryptedJsonEncoding, KeypairType } from '@pezkuwi/util-crypto/types';
2
+ import type { KeyringPair, KeyringPair$Meta } from '../types.js';
3
+ import type { PairInfo } from './types.js';
4
+ interface Setup {
5
+ toSS58: (publicKey: Uint8Array) => string;
6
+ type: KeypairType;
7
+ }
8
+ /**
9
+ * @name createPair
10
+ * @summary Creates a keyring pair object
11
+ * @description Creates a keyring pair object with provided account public key, metadata, and encoded arguments.
12
+ * The keyring pair stores the account state including the encoded address and associated metadata.
13
+ *
14
+ * It has properties whose values are functions that may be called to perform account actions:
15
+ *
16
+ * - `address` function retrieves the address associated with the account.
17
+ * - `decodedPkcs8` function is called with the account passphrase and account encoded public key.
18
+ * It decodes the encoded public key using the passphrase provided to obtain the decoded account public key
19
+ * and associated secret key that are then available in memory, and changes the account address stored in the
20
+ * state of the pair to correspond to the address of the decoded public key.
21
+ * - `encodePkcs8` function when provided with the correct passphrase associated with the account pair
22
+ * and when the secret key is in memory (when the account pair is not locked) it returns an encoded
23
+ * public key of the account.
24
+ * - `meta` is the metadata that is stored in the state of the pair, either when it was originally
25
+ * created or set via `setMeta`.
26
+ * - `publicKey` returns the public key stored in memory for the pair.
27
+ * - `sign` may be used to return a signature by signing a provided message with the secret
28
+ * key (if it is in memory) using Nacl.
29
+ * - `toJson` calls another `toJson` function and provides the state of the pair,
30
+ * it generates arguments to be passed to the other `toJson` function including an encoded public key of the account
31
+ * that it generates using the secret key from memory (if it has been made available in memory)
32
+ * and the optionally provided passphrase argument. It passes a third boolean argument to `toJson`
33
+ * indicating whether the public key has been encoded or not (if a passphrase argument was provided then it is encoded).
34
+ * The `toJson` function that it calls returns a JSON object with properties including the `address`
35
+ * and `meta` that are assigned with the values stored in the corresponding state variables of the account pair,
36
+ * an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
37
+ * property that indicates whether the public key value of the `encoded` property is encoded or not.
38
+ */
39
+ export declare function createPair({ toSS58, type }: Setup, { publicKey, secretKey }: PairInfo, meta?: KeyringPair$Meta, encoded?: Uint8Array | null, encTypes?: EncryptedJsonEncoding[]): KeyringPair;
40
+ export {};
@@ -0,0 +1,180 @@
1
+ import { objectSpread, u8aConcat, u8aEmpty, u8aEq, u8aToHex, u8aToU8a } from '@pezkuwi/util';
2
+ 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';
3
+ import { decodePair } from './decode.js';
4
+ import { encodePair } from './encode.js';
5
+ import { pairToJson } from './toJson.js';
6
+ const SIG_TYPE_NONE = new Uint8Array();
7
+ const TYPE_FROM_SEED = {
8
+ ecdsa: secp256k1FromSeed,
9
+ ed25519: ed25519FromSeed,
10
+ ethereum: secp256k1FromSeed,
11
+ sr25519: sr25519FromSeed
12
+ };
13
+ const TYPE_PREFIX = {
14
+ ecdsa: new Uint8Array([2]),
15
+ ed25519: new Uint8Array([0]),
16
+ ethereum: new Uint8Array([2]),
17
+ sr25519: new Uint8Array([1])
18
+ };
19
+ const TYPE_SIGNATURE = {
20
+ ecdsa: (m, p) => secp256k1Sign(m, p, 'blake2'),
21
+ ed25519: ed25519Sign,
22
+ ethereum: (m, p) => secp256k1Sign(m, p, 'keccak'),
23
+ sr25519: sr25519Sign
24
+ };
25
+ const TYPE_ADDRESS = {
26
+ ecdsa: (p) => p.length > 32 ? blake2AsU8a(p) : p,
27
+ ed25519: (p) => p,
28
+ ethereum: (p) => p.length === 20 ? p : keccakAsU8a(secp256k1Expand(p)),
29
+ sr25519: (p) => p
30
+ };
31
+ function isLocked(secretKey) {
32
+ return !secretKey || u8aEmpty(secretKey);
33
+ }
34
+ function vrfHash(proof, context, extra) {
35
+ return blake2AsU8a(u8aConcat(context || '', extra || '', proof));
36
+ }
37
+ /**
38
+ * @name createPair
39
+ * @summary Creates a keyring pair object
40
+ * @description Creates a keyring pair object with provided account public key, metadata, and encoded arguments.
41
+ * The keyring pair stores the account state including the encoded address and associated metadata.
42
+ *
43
+ * It has properties whose values are functions that may be called to perform account actions:
44
+ *
45
+ * - `address` function retrieves the address associated with the account.
46
+ * - `decodedPkcs8` function is called with the account passphrase and account encoded public key.
47
+ * It decodes the encoded public key using the passphrase provided to obtain the decoded account public key
48
+ * and associated secret key that are then available in memory, and changes the account address stored in the
49
+ * state of the pair to correspond to the address of the decoded public key.
50
+ * - `encodePkcs8` function when provided with the correct passphrase associated with the account pair
51
+ * and when the secret key is in memory (when the account pair is not locked) it returns an encoded
52
+ * public key of the account.
53
+ * - `meta` is the metadata that is stored in the state of the pair, either when it was originally
54
+ * created or set via `setMeta`.
55
+ * - `publicKey` returns the public key stored in memory for the pair.
56
+ * - `sign` may be used to return a signature by signing a provided message with the secret
57
+ * key (if it is in memory) using Nacl.
58
+ * - `toJson` calls another `toJson` function and provides the state of the pair,
59
+ * it generates arguments to be passed to the other `toJson` function including an encoded public key of the account
60
+ * that it generates using the secret key from memory (if it has been made available in memory)
61
+ * and the optionally provided passphrase argument. It passes a third boolean argument to `toJson`
62
+ * indicating whether the public key has been encoded or not (if a passphrase argument was provided then it is encoded).
63
+ * The `toJson` function that it calls returns a JSON object with properties including the `address`
64
+ * and `meta` that are assigned with the values stored in the corresponding state variables of the account pair,
65
+ * an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
66
+ * property that indicates whether the public key value of the `encoded` property is encoded or not.
67
+ */
68
+ export function createPair({ toSS58, type }, { publicKey, secretKey }, meta = {}, encoded = null, encTypes) {
69
+ const decodePkcs8 = (passphrase, userEncoded) => {
70
+ const decoded = decodePair(passphrase, userEncoded || encoded, encTypes);
71
+ if (decoded.secretKey.length === 64) {
72
+ publicKey = decoded.publicKey;
73
+ secretKey = decoded.secretKey;
74
+ }
75
+ else {
76
+ const pair = TYPE_FROM_SEED[type](decoded.secretKey);
77
+ publicKey = pair.publicKey;
78
+ secretKey = pair.secretKey;
79
+ }
80
+ };
81
+ const recode = (passphrase) => {
82
+ isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
83
+ encoded = encodePair({ publicKey, secretKey }, passphrase); // re-encode, latest version
84
+ encTypes = undefined; // swap to defaults, latest version follows
85
+ return encoded;
86
+ };
87
+ const encodeAddress = () => {
88
+ const raw = TYPE_ADDRESS[type](publicKey);
89
+ return type === 'ethereum'
90
+ ? ethereumEncode(raw)
91
+ : toSS58(raw);
92
+ };
93
+ return {
94
+ get address() {
95
+ return encodeAddress();
96
+ },
97
+ get addressRaw() {
98
+ const raw = TYPE_ADDRESS[type](publicKey);
99
+ return type === 'ethereum'
100
+ ? raw.slice(-20)
101
+ : raw;
102
+ },
103
+ get isLocked() {
104
+ return isLocked(secretKey);
105
+ },
106
+ get meta() {
107
+ return meta;
108
+ },
109
+ get publicKey() {
110
+ return publicKey;
111
+ },
112
+ get type() {
113
+ return type;
114
+ },
115
+ // eslint-disable-next-line sort-keys
116
+ decodePkcs8,
117
+ derive: (suri, meta) => {
118
+ if (type === 'ethereum') {
119
+ throw new Error('Unable to derive on this keypair');
120
+ }
121
+ else if (isLocked(secretKey)) {
122
+ throw new Error('Cannot derive on a locked keypair');
123
+ }
124
+ const { path } = keyExtractPath(suri);
125
+ const derived = keyFromPath({ publicKey, secretKey }, path, type);
126
+ return createPair({ toSS58, type }, derived, meta, null);
127
+ },
128
+ encodePkcs8: (passphrase) => {
129
+ return recode(passphrase);
130
+ },
131
+ lock: () => {
132
+ secretKey = new Uint8Array();
133
+ },
134
+ setMeta: (additional) => {
135
+ meta = objectSpread({}, meta, additional);
136
+ },
137
+ sign: (message, options = {}) => {
138
+ if (isLocked(secretKey)) {
139
+ throw new Error('Cannot sign with a locked key pair');
140
+ }
141
+ return u8aConcat(options.withType
142
+ ? TYPE_PREFIX[type]
143
+ : SIG_TYPE_NONE, TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey }));
144
+ },
145
+ toJson: (passphrase) => {
146
+ // NOTE: For ecdsa and ethereum, the publicKey cannot be extracted from the address. For these
147
+ // pass the hex-encoded publicKey through to the address portion of the JSON (before decoding)
148
+ // unless the publicKey is already an address
149
+ const address = ['ecdsa', 'ethereum'].includes(type)
150
+ ? publicKey.length === 20
151
+ ? u8aToHex(publicKey)
152
+ : u8aToHex(secp256k1Compress(publicKey))
153
+ : encodeAddress();
154
+ return pairToJson(type, { address, meta }, recode(passphrase), !!passphrase);
155
+ },
156
+ unlock: (passphrase) => {
157
+ return decodePkcs8(passphrase);
158
+ },
159
+ verify: (message, signature, signerPublic) => {
160
+ return signatureVerify(message, signature, TYPE_ADDRESS[type](u8aToU8a(signerPublic))).isValid;
161
+ },
162
+ vrfSign: (message, context, extra) => {
163
+ if (isLocked(secretKey)) {
164
+ throw new Error('Cannot sign with a locked key pair');
165
+ }
166
+ if (type === 'sr25519') {
167
+ return sr25519VrfSign(message, { secretKey }, context, extra);
168
+ }
169
+ const proof = TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey });
170
+ return u8aConcat(vrfHash(proof, context, extra), proof);
171
+ },
172
+ vrfVerify: (message, vrfResult, signerPublic, context, extra) => {
173
+ if (type === 'sr25519') {
174
+ return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
175
+ }
176
+ const result = signatureVerify(message, u8aConcat(TYPE_PREFIX[type], vrfResult.subarray(32)), TYPE_ADDRESS[type](u8aToU8a(signerPublic)));
177
+ return result.isValid && u8aEq(vrfResult.subarray(0, 32), vrfHash(vrfResult.subarray(32), context, extra));
178
+ }
179
+ };
180
+ }
@@ -0,0 +1,2 @@
1
+ import type { KeyringPair } from '../types.js';
2
+ export declare function nobody(): KeyringPair;
@@ -0,0 +1,40 @@
1
+ const publicKey = new Uint8Array(32);
2
+ const address = '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM';
3
+ const meta = {
4
+ isTesting: true,
5
+ name: 'nobody'
6
+ };
7
+ const json = {
8
+ address,
9
+ encoded: '',
10
+ encoding: {
11
+ content: ['pkcs8', 'ed25519'],
12
+ type: 'none',
13
+ version: '0'
14
+ },
15
+ meta
16
+ };
17
+ const pair = {
18
+ address,
19
+ addressRaw: publicKey,
20
+ decodePkcs8: (_passphrase, _encoded) => undefined,
21
+ derive: (_suri, _meta) => pair,
22
+ encodePkcs8: (_passphrase) => new Uint8Array(0),
23
+ isLocked: true,
24
+ lock: () => {
25
+ // no locking, it is always locked
26
+ },
27
+ meta,
28
+ publicKey,
29
+ setMeta: (_meta) => undefined,
30
+ sign: (_message) => new Uint8Array(64),
31
+ toJson: (_passphrase) => json,
32
+ type: 'ed25519',
33
+ unlock: (_passphrase) => undefined,
34
+ verify: (_message, _signature) => false,
35
+ vrfSign: (_message, _context, _extra) => new Uint8Array(96),
36
+ vrfVerify: (_message, _vrfResult, _context, _extra) => false
37
+ };
38
+ export function nobody() {
39
+ return pair;
40
+ }
@@ -0,0 +1,8 @@
1
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
2
+ import type { KeyringPair$Json, KeyringPair$Meta } from '../types.js';
3
+ interface PairStateJson {
4
+ address: string;
5
+ meta: KeyringPair$Meta;
6
+ }
7
+ export declare function pairToJson(type: KeypairType, { address, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json;
8
+ export {};
@@ -0,0 +1,8 @@
1
+ import { objectSpread } from '@pezkuwi/util';
2
+ import { jsonEncryptFormat } from '@pezkuwi/util-crypto';
3
+ export function pairToJson(type, { address, meta }, encoded, isEncrypted) {
4
+ return objectSpread(jsonEncryptFormat(encoded, ['pkcs8', type], isEncrypted), {
5
+ address,
6
+ meta
7
+ });
8
+ }
@@ -0,0 +1,5 @@
1
+ export interface PairInfo {
2
+ publicKey: Uint8Array;
3
+ secretKey?: Uint8Array | undefined;
4
+ seed?: Uint8Array | null;
5
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import type { KeyringPair, KeyringPairs } from './types.js';
2
+ export declare class Pairs implements KeyringPairs {
3
+ #private;
4
+ add(pair: KeyringPair): KeyringPair;
5
+ all(): KeyringPair[];
6
+ get(address: string | Uint8Array): KeyringPair;
7
+ remove(address: string | Uint8Array): void;
8
+ }
package/build/pairs.js ADDED
@@ -0,0 +1,24 @@
1
+ import { isHex, isU8a, u8aToHex, u8aToU8a } from '@pezkuwi/util';
2
+ import { decodeAddress } from '@pezkuwi/util-crypto';
3
+ export class Pairs {
4
+ #map = {};
5
+ add(pair) {
6
+ this.#map[decodeAddress(pair.address).toString()] = pair;
7
+ return pair;
8
+ }
9
+ all() {
10
+ return Object.values(this.#map);
11
+ }
12
+ get(address) {
13
+ const pair = this.#map[decodeAddress(address).toString()];
14
+ if (!pair) {
15
+ throw new Error(`Unable to retrieve keypair '${isU8a(address) || isHex(address)
16
+ ? u8aToHex(u8aToU8a(address))
17
+ : address}'`);
18
+ }
19
+ return pair;
20
+ }
21
+ remove(address) {
22
+ delete this.#map[decodeAddress(address).toString()];
23
+ }
24
+ }
@@ -0,0 +1,20 @@
1
+ import type { HexString } from '@pezkuwi/util/types';
2
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
3
+ import type { KeyringInstance, KeyringOptions } from './types.js';
4
+ interface PairDef {
5
+ name?: string;
6
+ p: HexString;
7
+ s: HexString;
8
+ seed?: string;
9
+ type: KeypairType;
10
+ }
11
+ export declare const PAIRSSR25519: PairDef[];
12
+ export declare const PAIRSETHEREUM: PairDef[];
13
+ /**
14
+ * @name testKeyring
15
+ * @summary Create an instance of Keyring pre-populated with locked test accounts
16
+ * @description The test accounts (i.e. alice, bob, dave, eve, ferdie)
17
+ * are available on the dev chain and each test account is initialized with DOT funds.
18
+ */
19
+ export declare function createTestKeyring(options?: KeyringOptions, isDerived?: boolean): KeyringInstance;
20
+ export {};
@@ -0,0 +1,122 @@
1
+ import { hexToU8a } from '@pezkuwi/util';
2
+ import { createPair } from './pair/index.js';
3
+ import { Keyring } from './keyring.js';
4
+ export const PAIRSSR25519 = [
5
+ {
6
+ p: '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
7
+ s: '0x98319d4ff8a9508c4bb0cf0b5a78d760a0b2082c02775e6e82370816fedfff48925a225d97aa00682d6a59b95b18780c10d7032336e88f3442b42361f4a66011', // nosemgrep
8
+ seed: 'Alice',
9
+ type: 'sr25519'
10
+ },
11
+ {
12
+ p: '0xbe5ddb1579b72e84524fc29e78609e3caf42e85aa118ebfe0b0ad404b5bdd25f',
13
+ s: '0xe8da6c9d810e020f5e3c7f5af2dea314cbeaa0d72bc6421e92c0808a0c584a6046ab28e97c3ffc77fe12b5a4d37e8cd4afbfebbf2391ffc7cb07c0f38c023efd', // nosemgrep
14
+ seed: 'Alice//stash',
15
+ type: 'sr25519'
16
+ },
17
+ {
18
+ p: '0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48',
19
+ s: '0x081ff694633e255136bdb456c20a5fc8fed21f8b964c11bb17ff534ce80ebd5941ae88f85d0c1bfc37be41c904e1dfc01de8c8067b0d6d5df25dd1ac0894a325', // nosemgrep
20
+ seed: 'Bob',
21
+ type: 'sr25519'
22
+ },
23
+ {
24
+ p: '0xfe65717dad0447d715f660a0a58411de509b42e6efb8375f562f58a554d5860e',
25
+ s: '0xc006507cdfc267a21532394c49ca9b754ca71de21e15a1cdf807c7ceab6d0b6c3ed408d9d35311540dcd54931933e67cf1ea10d46f75408f82b789d9bd212fde', // nosemgrep
26
+ seed: 'Bob//stash',
27
+ type: 'sr25519'
28
+ },
29
+ {
30
+ p: '0x90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22',
31
+ s: '0xa8f2d83016052e5d6d77b2f6fd5d59418922a09024cda701b3c34369ec43a7668faf12ff39cd4e5d92bb773972f41a7a5279ebc2ed92264bed8f47d344f8f18c', // nosemgrep
32
+ seed: 'Charlie',
33
+ type: 'sr25519'
34
+ },
35
+ {
36
+ p: '0x306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20',
37
+ s: '0x20e05482ca4677e0edbc58ae9a3a59f6ed3b1a9484ba17e64d6fe8688b2b7b5d108c4487b9323b98b11fe36cb301b084e920f7b7895536809a6d62a451b25568', // nosemgrep
38
+ seed: 'Dave',
39
+ type: 'sr25519'
40
+ },
41
+ {
42
+ p: '0xe659a7a1628cdd93febc04a4e0646ea20e9f5f0ce097d9a05290d4a9e054df4e',
43
+ s: '0x683576abfd5dc35273e4264c23095a1bf21c14517bece57c7f0cc5c0ed4ce06a3dbf386b7828f348abe15d76973a72009e6ef86a5c91db2990cb36bb657c6587', // nosemgrep
44
+ seed: 'Eve',
45
+ type: 'sr25519'
46
+ },
47
+ {
48
+ p: '0x1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c',
49
+ s: '0xb835c20f450079cf4f513900ae9faf8df06ad86c681884122c752a4b2bf74d4303e4f21bc6cc62bb4eeed5a9cce642c25e2d2ac1464093b50f6196d78e3a7426', // nosemgrep
50
+ seed: 'Ferdie',
51
+ type: 'sr25519'
52
+ }
53
+ ];
54
+ export const PAIRSETHEREUM = [
55
+ {
56
+ name: 'Alith',
57
+ p: '0x02509540919faacf9ab52146c9aa40db68172d83777250b28e4679176e49ccdd9f',
58
+ s: '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133', // nosemgrep
59
+ type: 'ethereum'
60
+ },
61
+ {
62
+ name: 'Baltathar',
63
+ p: '0x033bc19e36ff1673910575b6727a974a9abd80c9a875d41ab3e2648dbfb9e4b518',
64
+ s: '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b', // nosemgrep
65
+ type: 'ethereum'
66
+ },
67
+ {
68
+ name: 'Charleth',
69
+ p: '0x0234637bdc0e89b5d46543bcbf8edff329d2702bc995e27e9af4b1ba009a3c2a5e',
70
+ s: '0x0b6e18cafb6ed99687ec547bd28139cafdd2bffe70e6b688025de6b445aa5c5b', // nosemgrep
71
+ type: 'ethereum'
72
+ },
73
+ {
74
+ name: 'Dorothy',
75
+ p: '0x02a00d60b2b408c2a14c5d70cdd2c205db8985ef737a7e55ad20ea32cc9e7c417c',
76
+ s: '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68', // nosemgrep
77
+ type: 'ethereum'
78
+ },
79
+ {
80
+ name: 'Ethan',
81
+ p: '0x025cdc005b752651cd3f728fb9192182acb3a9c89e19072cbd5b03f3ee1f1b3ffa',
82
+ s: '0x7dce9bc8babb68fec1409be38c8e1a52650206a7ed90ff956ae8a6d15eeaaef4', // nosemgrep
83
+ type: 'ethereum'
84
+ },
85
+ {
86
+ name: 'Faith',
87
+ p: '0x037964b6c9d546da4646ada28a99e34acaa1d14e7aba861a9055f9bd200c8abf74',
88
+ s: '0xb9d2ea9a615f3165812e8d44de0d24da9bbd164b65c4f0573e1ce2c8dbd9c8df', // nosemgrep
89
+ type: 'ethereum'
90
+ }
91
+ ];
92
+ function createMeta(name, seed) {
93
+ if (!name && !seed) {
94
+ throw new Error('Testing pair should have either a name or a seed');
95
+ }
96
+ return {
97
+ isTesting: true,
98
+ name: name || seed?.replace('//', '_').toLowerCase()
99
+ };
100
+ }
101
+ /**
102
+ * @name testKeyring
103
+ * @summary Create an instance of Keyring pre-populated with locked test accounts
104
+ * @description The test accounts (i.e. alice, bob, dave, eve, ferdie)
105
+ * are available on the dev chain and each test account is initialized with DOT funds.
106
+ */
107
+ export function createTestKeyring(options = {}, isDerived = true) {
108
+ const keyring = new Keyring(options);
109
+ const pairs = options.type === 'ethereum'
110
+ ? PAIRSETHEREUM
111
+ : PAIRSSR25519;
112
+ for (const { name, p, s, seed, type } of pairs) {
113
+ const meta = createMeta(name, seed);
114
+ const pair = !isDerived && !name && seed
115
+ ? keyring.addFromUri(seed, meta, options.type)
116
+ : keyring.addPair(createPair({ toSS58: keyring.encodeAddress, type }, { publicKey: hexToU8a(p), secretKey: hexToU8a(s) }, meta));
117
+ pair.lock = () => {
118
+ // we don't have lock/unlock functionality here
119
+ };
120
+ }
121
+ return keyring;
122
+ }
@@ -0,0 +1,25 @@
1
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
2
+ import type { KeyringOptions, KeyringPair } from './types.js';
3
+ export interface TestKeyringMap {
4
+ nobody: KeyringPair;
5
+ [index: string]: KeyringPair;
6
+ }
7
+ export interface TestKeyringMapBizinikiwi extends TestKeyringMap {
8
+ alice: KeyringPair;
9
+ bob: KeyringPair;
10
+ charlie: KeyringPair;
11
+ dave: KeyringPair;
12
+ eve: KeyringPair;
13
+ ferdie: KeyringPair;
14
+ }
15
+ export interface TestKeyringMapEthereum extends TestKeyringMap {
16
+ Alith: KeyringPair;
17
+ Baltathar: KeyringPair;
18
+ Charleth: KeyringPair;
19
+ Dorothy: KeyringPair;
20
+ Ethan: KeyringPair;
21
+ Faith: KeyringPair;
22
+ }
23
+ export type DetectMap<O extends KeyringOptions | undefined> = DetectPairType<O> extends 'ethereum' ? TestKeyringMapEthereum : TestKeyringMapBizinikiwi;
24
+ export type DetectPairType<O extends KeyringOptions | undefined> = O extends KeyringOptions ? O['type'] extends KeypairType ? O['type'] : 'sr25519' : 'sr25519';
25
+ export declare function createTestPairs<O extends KeyringOptions, M = DetectMap<O>>(options?: O, isDerived?: boolean): M;
@@ -0,0 +1,13 @@
1
+ import { nobody } from './pair/nobody.js';
2
+ import { createTestKeyring } from './testing.js';
3
+ export function createTestPairs(options, isDerived = true) {
4
+ const keyring = createTestKeyring(options, isDerived);
5
+ const pairs = keyring.getPairs();
6
+ const map = { nobody: nobody() };
7
+ for (const p of pairs) {
8
+ if (p.meta.name) {
9
+ map[p.meta.name] = p;
10
+ }
11
+ }
12
+ return map;
13
+ }
@@ -0,0 +1,111 @@
1
+ import type { HexString } from '@pezkuwi/util/types';
2
+ import type { EncryptedJson, Keypair, KeypairType, Prefix } from '@pezkuwi/util-crypto/types';
3
+ export interface KeyringOptions {
4
+ /** The ss58Format to use for address encoding (defaults to 42) */
5
+ ss58Format?: Prefix;
6
+ /** The type of keyring to create (defaults to ed25519) */
7
+ type?: KeypairType;
8
+ }
9
+ export interface KeyringPair$MetaHardware {
10
+ accountIndex?: number;
11
+ accountOffset?: number;
12
+ addressOffset?: number;
13
+ hardwareType?: 'ledger';
14
+ }
15
+ export interface KeyringPair$MetaFlags {
16
+ isDefaultAuthSelected?: boolean;
17
+ isExternal?: boolean;
18
+ isHardware?: boolean;
19
+ isHidden?: boolean;
20
+ isInjected?: boolean;
21
+ isMultisig?: boolean;
22
+ isProxied?: boolean;
23
+ isRecent?: boolean;
24
+ isTesting?: boolean;
25
+ }
26
+ export interface KeyringPair$MetaContract {
27
+ abi: string;
28
+ genesisHash?: HexString | null;
29
+ }
30
+ export interface KeyringPair$MetaExtension {
31
+ source?: string;
32
+ }
33
+ export interface KeyringPair$MetaMultisig {
34
+ threshold?: number;
35
+ who?: string[];
36
+ }
37
+ export interface KeyringPair$MetaParent {
38
+ parentAddress?: string;
39
+ parentName?: string;
40
+ }
41
+ export interface KeyringPair$Meta extends KeyringPair$MetaExtension, KeyringPair$MetaFlags, KeyringPair$MetaHardware, KeyringPair$MetaMultisig, KeyringPair$MetaParent {
42
+ address?: string;
43
+ contract?: KeyringPair$MetaContract;
44
+ genesisHash?: HexString | null;
45
+ name?: string;
46
+ suri?: string;
47
+ tags?: string[];
48
+ type?: KeypairType;
49
+ whenCreated?: number;
50
+ whenEdited?: number;
51
+ whenUsed?: number;
52
+ [key: string]: unknown;
53
+ }
54
+ export interface KeyringPair$Json extends EncryptedJson {
55
+ /** The ss58 encoded address or the hex-encoded version (the latter is for ETH-compat chains) */
56
+ address: string;
57
+ /** The underlying metadata associated with the keypair */
58
+ meta: KeyringPair$Meta;
59
+ }
60
+ export interface SignOptions {
61
+ /** Create a MultiSignature-compatible output with an indicator type */
62
+ withType?: boolean;
63
+ }
64
+ export interface KeyringPair {
65
+ readonly address: string;
66
+ readonly addressRaw: Uint8Array;
67
+ readonly meta: KeyringPair$Meta;
68
+ readonly isLocked: boolean;
69
+ readonly publicKey: Uint8Array;
70
+ readonly type: KeypairType;
71
+ decodePkcs8(passphrase?: string, encoded?: Uint8Array): void;
72
+ derive(suri: string, meta?: KeyringPair$Meta): KeyringPair;
73
+ encodePkcs8(passphrase?: string): Uint8Array;
74
+ lock(): void;
75
+ setMeta(meta: KeyringPair$Meta): void;
76
+ sign(message: string | Uint8Array, options?: SignOptions): Uint8Array;
77
+ toJson(passphrase?: string): KeyringPair$Json;
78
+ unlock(passphrase?: string): void;
79
+ verify(message: string | Uint8Array, signature: Uint8Array, signerPublic: string | Uint8Array): boolean;
80
+ vrfSign(message: string | Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array;
81
+ vrfVerify(message: string | Uint8Array, vrfResult: Uint8Array, signerPublic: string | Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): boolean;
82
+ }
83
+ export interface KeyringPairs {
84
+ add: (pair: KeyringPair) => KeyringPair;
85
+ all: () => KeyringPair[];
86
+ get: (address: string | Uint8Array) => KeyringPair;
87
+ remove: (address: string | Uint8Array) => void;
88
+ }
89
+ export interface KeyringInstance {
90
+ readonly pairs: KeyringPair[];
91
+ readonly publicKeys: Uint8Array[];
92
+ readonly type: KeypairType;
93
+ decodeAddress(encoded: string | Uint8Array, ignoreChecksum?: boolean, ss58Format?: Prefix): Uint8Array;
94
+ encodeAddress(key: Uint8Array | string, ss58Format?: Prefix): string;
95
+ setSS58Format(ss58Format: Prefix): void;
96
+ addPair(pair: KeyringPair): KeyringPair;
97
+ addFromAddress(address: string | Uint8Array, meta?: KeyringPair$Meta, encoded?: Uint8Array | null, type?: KeypairType, ignoreChecksum?: boolean): KeyringPair;
98
+ addFromJson(pair: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair;
99
+ addFromMnemonic(mnemonic: string, meta?: KeyringPair$Meta, type?: KeypairType, wordlist?: string[]): KeyringPair;
100
+ addFromPair(pair: Keypair, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
101
+ addFromSeed(seed: Uint8Array, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
102
+ addFromUri(suri: string, meta?: KeyringPair$Meta, type?: KeypairType, wordlist?: string[]): KeyringPair;
103
+ createFromJson(json: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair;
104
+ createFromPair(pair: Keypair, meta: KeyringPair$Meta, type: KeypairType): KeyringPair;
105
+ createFromUri(suri: string, meta?: KeyringPair$Meta, type?: KeypairType, wordlist?: string[]): KeyringPair;
106
+ getPair(address: string | Uint8Array): KeyringPair;
107
+ getPairs(): KeyringPair[];
108
+ getPublicKeys(): Uint8Array[];
109
+ removePair(address: string | Uint8Array): void;
110
+ toJson(address: string | Uint8Array, passphrase?: string): KeyringPair$Json;
111
+ }
package/build/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ # @pezkuwi/keyring
2
+
3
+ Key management of user accounts including generation and retrieval of keyring pairs from a variety of input combinations.
4
+
5
+ ## Usage
6
+
7
+ Installation -
8
+
9
+ ```
10
+ yarn add @pezkuwi/keyring
11
+ ```
12
+
13
+ Classes and Functions can be imported as follows:
14
+
15
+ ```js
16
+ import Keyring from 'https://deno.land/x/pezkuwi/keyring/mod.ts';
17
+ ```