@pezkuwi/keyring 14.0.23 → 14.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle-pezkuwi-keyring.js +1 -1
- package/cjs/packageInfo.js +1 -1
- package/cjs/pair/defaults.d.ts +2 -2
- package/package.json +11 -11
- package/packageInfo.js +1 -1
- package/pair/defaults.d.ts +2 -2
- package/build/LICENSE +0 -201
- package/build/README.md +0 -17
- package/build/bundle.d.ts +0 -7
- package/build/bundle.js +0 -7
- package/build/cjs/bundle.d.ts +0 -7
- package/build/cjs/bundle.js +0 -19
- package/build/cjs/defaults.d.ts +0 -2
- package/build/cjs/defaults.js +0 -5
- package/build/cjs/index.d.ts +0 -4
- package/build/cjs/index.js +0 -7
- package/build/cjs/keyring.d.ts +0 -145
- package/build/cjs/keyring.js +0 -261
- package/build/cjs/package.json +0 -3
- package/build/cjs/packageDetect.d.ts +0 -1
- package/build/cjs/packageDetect.js +0 -7
- package/build/cjs/packageInfo.d.ts +0 -6
- package/build/cjs/packageInfo.js +0 -4
- package/build/cjs/pair/decode.d.ts +0 -12
- package/build/cjs/pair/decode.js +0 -45
- package/build/cjs/pair/defaults.d.ts +0 -12
- package/build/cjs/pair/defaults.js +0 -15
- package/build/cjs/pair/encode.d.ts +0 -5
- package/build/cjs/pair/encode.js +0 -22
- package/build/cjs/pair/index.d.ts +0 -40
- package/build/cjs/pair/index.js +0 -183
- package/build/cjs/pair/nobody.d.ts +0 -2
- package/build/cjs/pair/nobody.js +0 -43
- package/build/cjs/pair/toJson.d.ts +0 -8
- package/build/cjs/pair/toJson.js +0 -11
- package/build/cjs/pair/types.d.ts +0 -5
- package/build/cjs/pair/types.js +0 -2
- package/build/cjs/pairs.d.ts +0 -8
- package/build/cjs/pairs.js +0 -28
- package/build/cjs/testing.d.ts +0 -20
- package/build/cjs/testing.js +0 -126
- package/build/cjs/testingPairs.d.ts +0 -25
- package/build/cjs/testingPairs.js +0 -16
- package/build/cjs/types.d.ts +0 -111
- package/build/cjs/types.js +0 -2
- package/build/defaults.d.ts +0 -2
- package/build/defaults.js +0 -2
- package/build/index.d.ts +0 -4
- package/build/index.js +0 -4
- package/build/keyring.d.ts +0 -145
- package/build/keyring.js +0 -257
- package/build/package.json +0 -294
- package/build/packageDetect.d.ts +0 -1
- package/build/packageDetect.js +0 -5
- package/build/packageInfo.d.ts +0 -6
- package/build/packageInfo.js +0 -1
- package/build/pair/decode.d.ts +0 -12
- package/build/pair/decode.js +0 -42
- package/build/pair/defaults.d.ts +0 -12
- package/build/pair/defaults.js +0 -12
- package/build/pair/encode.d.ts +0 -5
- package/build/pair/encode.js +0 -19
- package/build/pair/index.d.ts +0 -40
- package/build/pair/index.js +0 -180
- package/build/pair/nobody.d.ts +0 -2
- package/build/pair/nobody.js +0 -40
- package/build/pair/toJson.d.ts +0 -8
- package/build/pair/toJson.js +0 -8
- package/build/pair/types.d.ts +0 -5
- package/build/pair/types.js +0 -1
- package/build/pairs.d.ts +0 -8
- package/build/pairs.js +0 -24
- package/build/testing.d.ts +0 -20
- package/build/testing.js +0 -122
- package/build/testingPairs.d.ts +0 -25
- package/build/testingPairs.js +0 -13
- package/build/types.d.ts +0 -111
- package/build/types.js +0 -1
- package/build-deno/README.md +0 -17
- package/build-deno/bundle.ts +0 -10
- package/build-deno/defaults.ts +0 -4
- package/build-deno/index.ts +0 -8
- package/build-deno/keyring.ts +0 -305
- package/build-deno/mod.ts +0 -2
- package/build-deno/packageDetect.ts +0 -9
- package/build-deno/packageInfo.ts +0 -3
- package/build-deno/pair/decode.ts +0 -54
- package/build-deno/pair/defaults.ts +0 -18
- package/build-deno/pair/encode.ts +0 -28
- package/build-deno/pair/index.ts +0 -218
- package/build-deno/pair/nobody.ts +0 -58
- package/build-deno/pair/toJson.ts +0 -18
- package/build-deno/pair/types.ts +0 -6
- package/build-deno/pairs.ts +0 -39
- package/build-deno/testing.ts +0 -150
- package/build-deno/testingPairs.ts +0 -54
- package/build-deno/types.ts +0 -129
- package/build-tsc/bundle.d.ts +0 -7
- package/build-tsc/defaults.d.ts +0 -2
- package/build-tsc/index.d.ts +0 -4
- package/build-tsc/keyring.d.ts +0 -145
- package/build-tsc/packageDetect.d.ts +0 -1
- package/build-tsc/packageInfo.d.ts +0 -6
- package/build-tsc/pair/decode.d.ts +0 -12
- package/build-tsc/pair/defaults.d.ts +0 -12
- package/build-tsc/pair/encode.d.ts +0 -5
- package/build-tsc/pair/index.d.ts +0 -40
- package/build-tsc/pair/nobody.d.ts +0 -2
- package/build-tsc/pair/toJson.d.ts +0 -8
- package/build-tsc/pair/types.d.ts +0 -5
- package/build-tsc/pairs.d.ts +0 -8
- package/build-tsc/testing.d.ts +0 -20
- package/build-tsc/testingPairs.d.ts +0 -25
- package/build-tsc/types.d.ts +0 -111
- package/build-tsc-cjs/bundle.js +0 -19
- package/build-tsc-cjs/defaults.js +0 -5
- package/build-tsc-cjs/index.js +0 -7
- package/build-tsc-cjs/keyring.js +0 -261
- package/build-tsc-cjs/packageDetect.js +0 -7
- package/build-tsc-cjs/packageInfo.js +0 -4
- package/build-tsc-cjs/pair/decode.js +0 -45
- package/build-tsc-cjs/pair/defaults.js +0 -15
- package/build-tsc-cjs/pair/encode.js +0 -22
- package/build-tsc-cjs/pair/index.js +0 -183
- package/build-tsc-cjs/pair/nobody.js +0 -43
- package/build-tsc-cjs/pair/toJson.js +0 -11
- package/build-tsc-cjs/pair/types.js +0 -2
- package/build-tsc-cjs/pairs.js +0 -28
- package/build-tsc-cjs/testing.js +0 -126
- package/build-tsc-cjs/testingPairs.js +0 -16
- package/build-tsc-cjs/types.js +0 -2
- package/build-tsc-esm/bundle.js +0 -7
- package/build-tsc-esm/defaults.js +0 -2
- package/build-tsc-esm/index.js +0 -4
- package/build-tsc-esm/keyring.js +0 -257
- package/build-tsc-esm/packageDetect.js +0 -5
- package/build-tsc-esm/packageInfo.js +0 -1
- package/build-tsc-esm/pair/decode.js +0 -42
- package/build-tsc-esm/pair/defaults.js +0 -12
- package/build-tsc-esm/pair/encode.js +0 -19
- package/build-tsc-esm/pair/index.js +0 -180
- package/build-tsc-esm/pair/nobody.js +0 -40
- package/build-tsc-esm/pair/toJson.js +0 -8
- package/build-tsc-esm/pair/types.js +0 -1
- package/build-tsc-esm/pairs.js +0 -24
- package/build-tsc-esm/testing.js +0 -122
- package/build-tsc-esm/testingPairs.js +0 -13
- package/build-tsc-esm/types.js +0 -1
- package/src/bundle.ts +0 -16
- package/src/defaults.ts +0 -8
- package/src/index.spec.ts +0 -609
- package/src/index.ts +0 -10
- package/src/keyring.ts +0 -307
- package/src/mod.ts +0 -4
- package/src/packageDetect.ts +0 -13
- package/src/packageInfo.ts +0 -6
- package/src/pair/decode.spec.ts +0 -26
- package/src/pair/decode.ts +0 -56
- package/src/pair/defaults.ts +0 -20
- package/src/pair/encode.spec.ts +0 -28
- package/src/pair/encode.ts +0 -30
- package/src/pair/index.spec.ts +0 -189
- package/src/pair/index.ts +0 -220
- package/src/pair/nobody.ts +0 -62
- package/src/pair/toJson.spec.ts +0 -42
- package/src/pair/toJson.ts +0 -20
- package/src/pair/types.ts +0 -8
- package/src/pair/vrf.spec.ts +0 -47
- package/src/pairs.ts +0 -41
- package/src/suri.spec.ts +0 -109
- package/src/testing.ts +0 -156
- package/src/testingPairs.spec.ts +0 -79
- package/src/testingPairs.ts +0 -56
- package/src/types.ts +0 -131
- package/tsconfig.build.json +0 -16
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.spec.json +0 -18
- package/tsconfig.spec.tsbuildinfo +0 -1
package/build-tsc-esm/keyring.js
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { hexToU8a, isHex, stringToU8a } from '@pezkuwi/util';
|
|
2
|
-
import { base64Decode, decodeAddress, ed25519PairFromSeed as ed25519FromSeed, encodeAddress, ethereumEncode, hdEthereum, keyExtractSuri, keyFromPath, mnemonicToLegacySeed, mnemonicToMiniSecret, secp256k1PairFromSeed as secp256k1FromSeed, sr25519PairFromSeed as sr25519FromSeed } from '@pezkuwi/util-crypto';
|
|
3
|
-
import { createPair } from './pair/index.js';
|
|
4
|
-
import { DEV_PHRASE } from './defaults.js';
|
|
5
|
-
import { Pairs } from './pairs.js';
|
|
6
|
-
const PairFromSeed = {
|
|
7
|
-
ecdsa: (seed) => secp256k1FromSeed(seed),
|
|
8
|
-
ed25519: (seed) => ed25519FromSeed(seed),
|
|
9
|
-
ethereum: (seed) => secp256k1FromSeed(seed),
|
|
10
|
-
sr25519: (seed) => sr25519FromSeed(seed)
|
|
11
|
-
};
|
|
12
|
-
function pairToPublic({ publicKey }) {
|
|
13
|
-
return publicKey;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* # @pezkuwi/keyring
|
|
17
|
-
*
|
|
18
|
-
* ## Overview
|
|
19
|
-
*
|
|
20
|
-
* @name Keyring
|
|
21
|
-
* @summary Keyring management of user accounts
|
|
22
|
-
* @description Allows generation of keyring pairs from a variety of input combinations, such as
|
|
23
|
-
* json object containing account address or public key, account metadata, and account encoded using
|
|
24
|
-
* `addFromJson`, or by providing those values as arguments separately to `addFromAddress`,
|
|
25
|
-
* or by providing the mnemonic (seed phrase) and account metadata as arguments to `addFromMnemonic`.
|
|
26
|
-
* Stores the keyring pairs in a keyring pair dictionary. Removal of the keyring pairs from the keyring pair
|
|
27
|
-
* dictionary is achieved using `removePair`. Retrieval of all the stored pairs via `getPairs` or perform
|
|
28
|
-
* lookup of a pair for a given account address or public key using `getPair`. JSON metadata associated with
|
|
29
|
-
* an account may be obtained using `toJson` accompanied by the account passphrase.
|
|
30
|
-
*/
|
|
31
|
-
export class Keyring {
|
|
32
|
-
#pairs;
|
|
33
|
-
#type;
|
|
34
|
-
#ss58;
|
|
35
|
-
decodeAddress = decodeAddress;
|
|
36
|
-
constructor(options = {}) {
|
|
37
|
-
options.type = options.type || 'ed25519';
|
|
38
|
-
if (!['ecdsa', 'ethereum', 'ed25519', 'sr25519'].includes(options.type || 'undefined')) {
|
|
39
|
-
throw new Error(`Expected a keyring type of either 'ed25519', 'sr25519', 'ethereum' or 'ecdsa', found '${options.type || 'unknown'}`);
|
|
40
|
-
}
|
|
41
|
-
this.#pairs = new Pairs();
|
|
42
|
-
this.#ss58 = options.ss58Format;
|
|
43
|
-
this.#type = options.type;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* @description retrieve the pairs (alias for getPairs)
|
|
47
|
-
*/
|
|
48
|
-
get pairs() {
|
|
49
|
-
return this.getPairs();
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* @description retrieve the publicKeys (alias for getPublicKeys)
|
|
53
|
-
*/
|
|
54
|
-
get publicKeys() {
|
|
55
|
-
return this.getPublicKeys();
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
|
|
59
|
-
*/
|
|
60
|
-
get type() {
|
|
61
|
-
return this.#type;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* @name addPair
|
|
65
|
-
* @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
66
|
-
*/
|
|
67
|
-
addPair(pair) {
|
|
68
|
-
return this.#pairs.add(pair);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* @name addFromAddress
|
|
72
|
-
* @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
73
|
-
* @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
|
|
74
|
-
* the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
|
|
75
|
-
* of an account backup), and then generates a keyring pair from them that it passes to
|
|
76
|
-
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
77
|
-
*/
|
|
78
|
-
addFromAddress(address, meta = {}, encoded = null, type = this.type, ignoreChecksum, encType) {
|
|
79
|
-
const publicKey = this.decodeAddress(address, ignoreChecksum);
|
|
80
|
-
return this.addPair(createPair({ toSS58: this.encodeAddress, type }, { publicKey, secretKey: new Uint8Array() }, meta, encoded, encType));
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* @name addFromJson
|
|
84
|
-
* @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
85
|
-
* @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
|
|
86
|
-
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
87
|
-
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
88
|
-
*/
|
|
89
|
-
addFromJson(json, ignoreChecksum) {
|
|
90
|
-
return this.addPair(this.createFromJson(json, ignoreChecksum));
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* @name addFromMnemonic
|
|
94
|
-
* @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
95
|
-
* @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
|
|
96
|
-
* argument and a metadata argument that contains account information (that may be obtained from the json file
|
|
97
|
-
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
98
|
-
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
99
|
-
*/
|
|
100
|
-
addFromMnemonic(mnemonic, meta = {}, type = this.type, wordlist) {
|
|
101
|
-
return this.addFromUri(mnemonic, meta, type, wordlist);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* @name addFromPair
|
|
105
|
-
* @summary Stores an account created from an explicit publicKey/secreteKey combination
|
|
106
|
-
*/
|
|
107
|
-
addFromPair(pair, meta = {}, type = this.type) {
|
|
108
|
-
return this.addPair(this.createFromPair(pair, meta, type));
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* @name addFromSeed
|
|
112
|
-
* @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
113
|
-
* @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
|
|
114
|
-
* Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
|
|
115
|
-
* `addPair` to store in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
116
|
-
*/
|
|
117
|
-
addFromSeed(seed, meta = {}, type = this.type) {
|
|
118
|
-
return this.addPair(createPair({ toSS58: this.encodeAddress, type }, PairFromSeed[type](seed), meta, null));
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* @name addFromUri
|
|
122
|
-
* @summary Creates an account via an suri
|
|
123
|
-
* @description Extracts the phrase, path and password from a SURI format for specifying secret keys `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` maybe repeated and mixed). The secret can be a hex string, mnemonic phrase or a string (to be padded)
|
|
124
|
-
*/
|
|
125
|
-
addFromUri(suri, meta = {}, type = this.type, wordlist) {
|
|
126
|
-
return this.addPair(this.createFromUri(suri, meta, type, wordlist));
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* @name createFromJson
|
|
130
|
-
* @description Creates a pair from a JSON keyfile
|
|
131
|
-
*/
|
|
132
|
-
createFromJson({ address, encoded, encoding: { content, type, version }, meta }, ignoreChecksum) {
|
|
133
|
-
if (version === '3' && content[0] !== 'pkcs8') {
|
|
134
|
-
throw new Error(`Unable to decode non-pkcs8 type, [${content.join(',')}] found}`);
|
|
135
|
-
}
|
|
136
|
-
const cryptoType = version === '0' || !Array.isArray(content)
|
|
137
|
-
? this.type
|
|
138
|
-
: content[1];
|
|
139
|
-
const encType = !Array.isArray(type)
|
|
140
|
-
? [type]
|
|
141
|
-
: type;
|
|
142
|
-
if (!['ed25519', 'sr25519', 'ecdsa', 'ethereum'].includes(cryptoType)) {
|
|
143
|
-
throw new Error(`Unknown crypto type ${cryptoType}`);
|
|
144
|
-
}
|
|
145
|
-
// Here the address and publicKey are 32 bytes and isomorphic. This is why the address field needs to be the public key for ethereum type pairs
|
|
146
|
-
const publicKey = isHex(address)
|
|
147
|
-
? hexToU8a(address)
|
|
148
|
-
: this.decodeAddress(address, ignoreChecksum);
|
|
149
|
-
const decoded = isHex(encoded)
|
|
150
|
-
? hexToU8a(encoded)
|
|
151
|
-
: base64Decode(encoded);
|
|
152
|
-
return createPair({ toSS58: this.encodeAddress, type: cryptoType }, { publicKey, secretKey: new Uint8Array() }, meta, decoded, encType);
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* @name createFromPair
|
|
156
|
-
* @summary Creates a pair from an explicit publicKey/secreteKey combination
|
|
157
|
-
*/
|
|
158
|
-
createFromPair(pair, meta = {}, type = this.type) {
|
|
159
|
-
return createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* @name createFromUri
|
|
163
|
-
* @summary Creates a Keypair from an suri
|
|
164
|
-
* @description This creates a pair from the suri, but does not add it to the keyring
|
|
165
|
-
*/
|
|
166
|
-
createFromUri(_suri, meta = {}, type = this.type, wordlist) {
|
|
167
|
-
// here we only aut-add the dev phrase if we have a hard-derived path
|
|
168
|
-
const suri = _suri.startsWith('//')
|
|
169
|
-
? `${DEV_PHRASE}${_suri}`
|
|
170
|
-
: _suri;
|
|
171
|
-
const { derivePath, password, path, phrase } = keyExtractSuri(suri);
|
|
172
|
-
let seed;
|
|
173
|
-
const isPhraseHex = isHex(phrase, 256);
|
|
174
|
-
if (isPhraseHex) {
|
|
175
|
-
seed = hexToU8a(phrase);
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
const parts = phrase.split(' ');
|
|
179
|
-
if ([12, 15, 18, 21, 24].includes(parts.length)) {
|
|
180
|
-
seed = type === 'ethereum'
|
|
181
|
-
? mnemonicToLegacySeed(phrase, '', false, 64)
|
|
182
|
-
: mnemonicToMiniSecret(phrase, password, wordlist);
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
if (phrase.length > 32) {
|
|
186
|
-
throw new Error('specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes');
|
|
187
|
-
}
|
|
188
|
-
seed = stringToU8a(phrase.padEnd(32));
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
const derived = type === 'ethereum'
|
|
192
|
-
? isPhraseHex
|
|
193
|
-
? PairFromSeed[type](seed) // for eth, if the private key is provided as suri, it must be derived only once
|
|
194
|
-
: hdEthereum(seed, derivePath.substring(1))
|
|
195
|
-
: keyFromPath(PairFromSeed[type](seed), path, type);
|
|
196
|
-
return createPair({ toSS58: this.encodeAddress, type }, derived, meta, null);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* @name encodeAddress
|
|
200
|
-
* @description Encodes the input into an ss58 representation
|
|
201
|
-
*/
|
|
202
|
-
encodeAddress = (address, ss58Format) => {
|
|
203
|
-
return this.type === 'ethereum'
|
|
204
|
-
? ethereumEncode(address)
|
|
205
|
-
: encodeAddress(address, ss58Format ?? this.#ss58);
|
|
206
|
-
};
|
|
207
|
-
/**
|
|
208
|
-
* @name getPair
|
|
209
|
-
* @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
|
|
210
|
-
* @description Returns a keyring pair value from the keyring pair dictionary by performing
|
|
211
|
-
* a key lookup using the provided account address or public key (after decoding it).
|
|
212
|
-
*/
|
|
213
|
-
getPair(address) {
|
|
214
|
-
return this.#pairs.get(address);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* @name getPairs
|
|
218
|
-
* @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
|
|
219
|
-
* @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
|
|
220
|
-
*/
|
|
221
|
-
getPairs() {
|
|
222
|
-
return this.#pairs.all();
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* @name getPublicKeys
|
|
226
|
-
* @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
|
|
227
|
-
* @description Returns an array list of all the public keys associated with each of the keyring pair values that are stored in the keyring pair dictionary.
|
|
228
|
-
*/
|
|
229
|
-
getPublicKeys() {
|
|
230
|
-
return this.#pairs.all().map(pairToPublic);
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* @name removePair
|
|
234
|
-
* @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
|
|
235
|
-
*/
|
|
236
|
-
removePair(address) {
|
|
237
|
-
this.#pairs.remove(address);
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* @name setSS58Format;
|
|
241
|
-
* @description Sets the ss58 format for the keyring
|
|
242
|
-
*/
|
|
243
|
-
setSS58Format(ss58) {
|
|
244
|
-
this.#ss58 = ss58;
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* @name toJson
|
|
248
|
-
* @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
|
|
249
|
-
* @description Returns a JSON object containing the metadata associated with an account
|
|
250
|
-
* when valid address or public key and when the account passphrase is provided if the account secret
|
|
251
|
-
* is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
|
|
252
|
-
* may backup their account to a JSON file that contains this information.
|
|
253
|
-
*/
|
|
254
|
-
toJson(address, passphrase) {
|
|
255
|
-
return this.#pairs.get(address).toJson(passphrase);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { detectPackage } from '@pezkuwi/util';
|
|
2
|
-
import { packageInfo as utilInfo } from '@pezkuwi/util/packageInfo';
|
|
3
|
-
import { packageInfo as cryptoInfo } from '@pezkuwi/util-crypto/packageInfo';
|
|
4
|
-
import { packageInfo } from './packageInfo.js';
|
|
5
|
-
detectPackage(packageInfo, null, [cryptoInfo, utilInfo]);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const packageInfo = { name: '@pezkuwi/keyring', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '14.0.23' };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { u8aEq } from '@pezkuwi/util';
|
|
2
|
-
import { jsonDecryptData } from '@pezkuwi/util-crypto';
|
|
3
|
-
import { PAIR_DIV, PAIR_HDR, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH } from './defaults.js';
|
|
4
|
-
const SEED_OFFSET = PAIR_HDR.length;
|
|
5
|
-
/**
|
|
6
|
-
* Decode a pair, taking into account the generation-specific formats and headers
|
|
7
|
-
*
|
|
8
|
-
* For divisor/headers, don't rely on the magic being static. These will
|
|
9
|
-
* change between generations, aka with the long-awaited 4th generation
|
|
10
|
-
* of the format. The external decode interface is the only way to use and decode these.
|
|
11
|
-
**/
|
|
12
|
-
export function decodePair(passphrase, encrypted, _encType) {
|
|
13
|
-
const encType = Array.isArray(_encType) || _encType === undefined
|
|
14
|
-
? _encType
|
|
15
|
-
: [_encType];
|
|
16
|
-
const decrypted = jsonDecryptData(encrypted, passphrase, encType);
|
|
17
|
-
const header = decrypted.subarray(0, PAIR_HDR.length);
|
|
18
|
-
// check the start header (generations 1-3)
|
|
19
|
-
if (!u8aEq(header, PAIR_HDR)) {
|
|
20
|
-
throw new Error('Invalid encoding header found in body');
|
|
21
|
-
}
|
|
22
|
-
// setup for generation 3 format
|
|
23
|
-
let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + SEC_LENGTH);
|
|
24
|
-
let divOffset = SEED_OFFSET + SEC_LENGTH;
|
|
25
|
-
let divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
|
|
26
|
-
// old-style (generation 1 & 2), we have the seed here
|
|
27
|
-
if (!u8aEq(divider, PAIR_DIV)) {
|
|
28
|
-
divOffset = SEED_OFFSET + SEED_LENGTH;
|
|
29
|
-
secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
|
|
30
|
-
divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
|
|
31
|
-
// check the divisior at this point (already checked for generation 3)
|
|
32
|
-
if (!u8aEq(divider, PAIR_DIV)) {
|
|
33
|
-
throw new Error('Invalid encoding divider found in body');
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
const pubOffset = divOffset + PAIR_DIV.length;
|
|
37
|
-
const publicKey = decrypted.subarray(pubOffset, pubOffset + PUB_LENGTH);
|
|
38
|
-
return {
|
|
39
|
-
publicKey,
|
|
40
|
-
secretKey
|
|
41
|
-
};
|
|
42
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/** public/secret section divider (generation 1-3, will change in 4, don't rely on value) */
|
|
2
|
-
export const PAIR_DIV = new Uint8Array([161, 35, 3, 33, 0]);
|
|
3
|
-
/** public/secret start block (generation 1-3, will change in 4, don't rely on value) */
|
|
4
|
-
export const PAIR_HDR = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
|
|
5
|
-
/** length of a public key */
|
|
6
|
-
export const PUB_LENGTH = 32;
|
|
7
|
-
/** length of a salt */
|
|
8
|
-
export const SALT_LENGTH = 32;
|
|
9
|
-
/** length of a secret key */
|
|
10
|
-
export const SEC_LENGTH = 64;
|
|
11
|
-
/** length of a user-input seed */
|
|
12
|
-
export const SEED_LENGTH = 32;
|
|
@@ -1,19 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,180 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/build-tsc-esm/pairs.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
}
|