@prosopo/keyring 2.8.40 → 2.9.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +8 -6
- package/.turbo/turbo-build$colon$tsc.log +32 -0
- package/.turbo/turbo-build.log +13 -7
- package/CHANGELOG.md +296 -0
- package/dist/accounts/getPair.d.ts +5 -0
- package/dist/accounts/getPair.d.ts.map +1 -0
- package/dist/accounts/getPair.js.map +1 -0
- package/dist/accounts/index.d.ts +4 -0
- package/dist/accounts/index.d.ts.map +1 -0
- package/dist/accounts/index.js.map +1 -0
- package/dist/accounts/mnemonic.d.ts +5 -0
- package/dist/accounts/mnemonic.d.ts.map +1 -0
- package/dist/accounts/mnemonic.js.map +1 -0
- package/dist/accounts/testAccounts.d.ts +4 -0
- package/dist/accounts/testAccounts.d.ts.map +1 -0
- package/dist/accounts/testAccounts.js +6 -3
- package/dist/accounts/testAccounts.js.map +1 -0
- package/dist/cjs/accounts/testAccounts.cjs +6 -3
- package/dist/cjs/pair/index.cjs +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/keyring/index.d.ts +3 -0
- package/dist/keyring/index.d.ts.map +1 -0
- package/dist/keyring/index.js.map +1 -0
- package/dist/keyring/keyring.d.ts +36 -0
- package/dist/keyring/keyring.d.ts.map +1 -0
- package/dist/keyring/keyring.js.map +1 -0
- package/dist/keyring/pairs.d.ts +9 -0
- package/dist/keyring/pairs.d.ts.map +1 -0
- package/dist/keyring/pairs.js.map +1 -0
- package/dist/keyring/testing.d.ts +14 -0
- package/dist/keyring/testing.d.ts.map +1 -0
- package/dist/keyring/testing.js +76 -0
- package/dist/keyring/testing.js.map +1 -0
- package/dist/keyring/testingPairs.d.ts +26 -0
- package/dist/keyring/testingPairs.d.ts.map +1 -0
- package/dist/keyring/testingPairs.js +14 -0
- package/dist/keyring/testingPairs.js.map +1 -0
- package/dist/pair/decode.d.ts +6 -0
- package/dist/pair/decode.d.ts.map +1 -0
- package/dist/pair/decode.js.map +1 -0
- package/dist/pair/decode.spec.d.ts +2 -0
- package/dist/pair/decode.spec.d.ts.map +1 -0
- package/dist/pair/decode.spec.js +13 -0
- package/dist/pair/decode.spec.js.map +1 -0
- package/dist/pair/defaults.d.ts +7 -0
- package/dist/pair/defaults.d.ts.map +1 -0
- package/dist/pair/defaults.js.map +1 -0
- package/dist/pair/encode.d.ts +3 -0
- package/dist/pair/encode.d.ts.map +1 -0
- package/dist/pair/encode.js.map +1 -0
- package/dist/pair/encode.spec.d.ts +2 -0
- package/dist/pair/encode.spec.d.ts.map +1 -0
- package/dist/pair/encode.spec.js +16 -0
- package/dist/pair/encode.spec.js.map +1 -0
- package/dist/pair/index.d.ts +11 -0
- package/dist/pair/index.d.ts.map +1 -0
- package/dist/pair/index.js +22 -1
- package/dist/pair/index.js.map +1 -0
- package/dist/pair/nobody.d.ts +3 -0
- package/dist/pair/nobody.d.ts.map +1 -0
- package/dist/pair/nobody.js +48 -0
- package/dist/pair/nobody.js.map +1 -0
- package/dist/pair/toJson.d.ts +9 -0
- package/dist/pair/toJson.d.ts.map +1 -0
- package/dist/pair/toJson.js.map +1 -0
- package/dist/pair/toJson.spec.d.ts +2 -0
- package/dist/pair/toJson.spec.d.ts.map +1 -0
- package/dist/pair/toJson.spec.js +33 -0
- package/dist/pair/toJson.spec.js.map +1 -0
- package/package.json +10 -8
- package/src/accounts/getPair.ts +63 -0
- package/src/accounts/index.ts +16 -0
- package/src/accounts/mnemonic.ts +45 -0
- package/src/accounts/testAccounts.ts +66 -0
- package/src/index.ts +16 -0
- package/src/keyring/index.ts +15 -0
- package/src/keyring/keyring.ts +411 -0
- package/src/keyring/pairs.ts +44 -0
- package/src/keyring/testing.ts +119 -0
- package/src/keyring/testingPairs.ts +61 -0
- package/src/pair/decode.spec.ts +23 -0
- package/src/pair/decode.ts +65 -0
- package/src/pair/defaults.ts +22 -0
- package/src/pair/encode.spec.ts +24 -0
- package/src/pair/encode.ts +32 -0
- package/src/pair/index.ts +304 -0
- package/src/pair/nobody.ts +78 -0
- package/src/pair/toJson.spec.ts +40 -0
- package/src/pair/toJson.ts +28 -0
- package/tsconfig.cjs.json +30 -0
- package/tsconfig.json +33 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.types.json +9 -0
- package/vite.cjs.config.ts +1 -1
- package/vite.esm.config.ts +1 -1
- package/vite.test.config.ts +1 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { mnemonicGenerate, mnemonicToMiniSecret } from "@prosopo/util-crypto";
|
|
16
|
+
import type { KeypairType } from "@prosopo/util-crypto";
|
|
17
|
+
import { Keyring } from "../keyring/keyring.js";
|
|
18
|
+
|
|
19
|
+
/** Generate a mnemonic, returning the mnemonic and associated address
|
|
20
|
+
* @param keyring
|
|
21
|
+
* @param pairType
|
|
22
|
+
*/
|
|
23
|
+
export async function generateMnemonic(
|
|
24
|
+
keyring?: Keyring,
|
|
25
|
+
pairType?: KeypairType,
|
|
26
|
+
): Promise<[string, string]> {
|
|
27
|
+
if (!keyring) {
|
|
28
|
+
keyring = new Keyring({ type: pairType || "sr25519" });
|
|
29
|
+
}
|
|
30
|
+
const mnemonic = mnemonicGenerate();
|
|
31
|
+
const account = keyring.addFromMnemonic(mnemonic);
|
|
32
|
+
return [mnemonic, account.address];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Generate a secret, returning the secret and associated address
|
|
36
|
+
* @param keyring
|
|
37
|
+
* @param pairType
|
|
38
|
+
*/
|
|
39
|
+
export async function generateMiniSecret(
|
|
40
|
+
keyring?: Keyring,
|
|
41
|
+
pairType?: KeypairType,
|
|
42
|
+
): Promise<[Uint8Array, string]> {
|
|
43
|
+
const [mnemonic, address] = await generateMnemonic(keyring, pairType);
|
|
44
|
+
return [mnemonicToMiniSecret(mnemonic), address];
|
|
45
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//sr25519 dev site keys
|
|
15
|
+
import {
|
|
16
|
+
CaptchaType,
|
|
17
|
+
ClientSettingsSchema,
|
|
18
|
+
type IProviderAccount,
|
|
19
|
+
type ISite,
|
|
20
|
+
} from "@prosopo/types";
|
|
21
|
+
import { DEV_PHRASE } from "../keyring/index.js";
|
|
22
|
+
import { getPair } from "./getPair.js";
|
|
23
|
+
|
|
24
|
+
export function getDefaultSiteKeys(): ISite[] {
|
|
25
|
+
const captchaTypes = [
|
|
26
|
+
CaptchaType.image,
|
|
27
|
+
CaptchaType.pow,
|
|
28
|
+
CaptchaType.frictionless,
|
|
29
|
+
CaptchaType.puzzle,
|
|
30
|
+
];
|
|
31
|
+
const sites: ISite[] = [];
|
|
32
|
+
for (const captchaType of captchaTypes) {
|
|
33
|
+
const secret = `${DEV_PHRASE}//${captchaType}`;
|
|
34
|
+
const pair = getPair(secret);
|
|
35
|
+
// Settings are written explicitly rather than relying on schema defaults
|
|
36
|
+
// so dev seeds are self-describing and stay stable when defaults change.
|
|
37
|
+
sites.push({
|
|
38
|
+
pair: pair,
|
|
39
|
+
address: pair.address,
|
|
40
|
+
secret: secret,
|
|
41
|
+
settings: ClientSettingsSchema.parse({
|
|
42
|
+
captchaType: captchaType,
|
|
43
|
+
domains: ["localhost"],
|
|
44
|
+
imageMaxRounds: 2,
|
|
45
|
+
frictionlessThreshold: 0.8,
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return sites;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function getDefaultProviders(): IProviderAccount[] {
|
|
53
|
+
const pair = getPair(
|
|
54
|
+
"puppy cream effort carbon despair leg pyramid cotton endorse immense drill peasant",
|
|
55
|
+
);
|
|
56
|
+
return [
|
|
57
|
+
{
|
|
58
|
+
url: "https://localhost:9229",
|
|
59
|
+
pair: pair,
|
|
60
|
+
address: pair.address,
|
|
61
|
+
datasetFile: "./dev/data/captchas.json",
|
|
62
|
+
captchaDatasetId:
|
|
63
|
+
"0x3e067b8f6aeae82fdbe2b2557b9c2d90aaabff241df651bf0f36667d2ab0018d",
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export * from "./accounts/index.js";
|
|
15
|
+
export * from "./keyring/index.js";
|
|
16
|
+
export * from "./pair/index.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export * from "./keyring.js";
|
|
15
|
+
export * from "./pairs.js";
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { stringToU8a } from "@polkadot/util";
|
|
16
|
+
import { hexToU8a } from "@polkadot/util/hex";
|
|
17
|
+
import { isHex } from "@polkadot/util/is";
|
|
18
|
+
import type { KeyringPair$Json, KeyringPair$Meta } from "@prosopo/types";
|
|
19
|
+
import type { KeyringInstance, KeyringPair } from "@prosopo/types";
|
|
20
|
+
import { decodeAddress, mnemonicToMiniSecret } from "@prosopo/util-crypto";
|
|
21
|
+
import { encodeAddress } from "@prosopo/util-crypto";
|
|
22
|
+
import { base64Decode } from "@prosopo/util-crypto";
|
|
23
|
+
import { sr25519FromSeed } from "@prosopo/util-crypto";
|
|
24
|
+
import { keyExtractSuri, keyFromPath } from "@prosopo/util-crypto";
|
|
25
|
+
import type {
|
|
26
|
+
EncryptedJsonEncoding,
|
|
27
|
+
Keypair,
|
|
28
|
+
KeypairType,
|
|
29
|
+
} from "@prosopo/util-crypto";
|
|
30
|
+
import { createPair } from "../pair/index.js";
|
|
31
|
+
import { Pairs } from "./pairs.js";
|
|
32
|
+
|
|
33
|
+
export const DEV_PHRASE =
|
|
34
|
+
"bottom drive obey lake curtain smoke basket hold race lonely fit walk";
|
|
35
|
+
|
|
36
|
+
const PairFromSeed = {
|
|
37
|
+
sr25519: (seed: Uint8Array): Keypair => sr25519FromSeed(seed),
|
|
38
|
+
ed25519: () => {
|
|
39
|
+
throw new Error("Not Implemented");
|
|
40
|
+
},
|
|
41
|
+
ecdsa: () => {
|
|
42
|
+
throw new Error("Not Implemented");
|
|
43
|
+
},
|
|
44
|
+
ethereum: () => {
|
|
45
|
+
throw new Error("Not Implemented");
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
interface KeyringOptions {
|
|
50
|
+
type?: KeypairType; // "sr25519" only
|
|
51
|
+
ss58Format?: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function pairToPublic({ publicKey }: KeyringPair): Uint8Array {
|
|
55
|
+
return publicKey;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* # @polkadot/keyring
|
|
60
|
+
*
|
|
61
|
+
* ## Overview
|
|
62
|
+
*
|
|
63
|
+
* @name Keyring
|
|
64
|
+
* @summary Keyring management of user accounts
|
|
65
|
+
* @description Allows generation of keyring pairs from a variety of input combinations, such as
|
|
66
|
+
* json object containing account address or public key, account metadata, and account encoded using
|
|
67
|
+
* `addFromJson`, or by providing those values as arguments separately to `addFromAddress`,
|
|
68
|
+
* or by providing the mnemonic (seed phrase) and account metadata as arguments to `addFromMnemonic`.
|
|
69
|
+
* Stores the keyring pairs in a keyring pair dictionary. Removal of the keyring pairs from the keyring pair
|
|
70
|
+
* dictionary is achieved using `removePair`. Retrieval of all the stored pairs via `getPairs` or perform
|
|
71
|
+
* lookup of a pair for a given account address or public key using `getPair`. JSON metadata associated with
|
|
72
|
+
* an account may be obtained using `toJson` accompanied by the account passphrase.
|
|
73
|
+
*/
|
|
74
|
+
export class Keyring implements KeyringInstance {
|
|
75
|
+
readonly #pairs: Pairs;
|
|
76
|
+
|
|
77
|
+
readonly #type: KeypairType;
|
|
78
|
+
|
|
79
|
+
#ss58?: number | undefined;
|
|
80
|
+
|
|
81
|
+
public decodeAddress = decodeAddress;
|
|
82
|
+
|
|
83
|
+
constructor(options: KeyringOptions = {}) {
|
|
84
|
+
options.type = options.type || "sr25519";
|
|
85
|
+
|
|
86
|
+
if (!["sr25519"].includes(options.type || "undefined")) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`Expected a keyring type of either 'sr25519', found '${options.type || "unknown"}`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.#pairs = new Pairs();
|
|
93
|
+
this.#ss58 = options.ss58Format;
|
|
94
|
+
this.#type = options.type;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @description retrieve the pairs (alias for getPairs)
|
|
99
|
+
*/
|
|
100
|
+
public get pairs(): KeyringPair[] {
|
|
101
|
+
return this.getPairs();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @description retrieve the publicKeys (alias for getPublicKeys)
|
|
106
|
+
*/
|
|
107
|
+
public get publicKeys(): Uint8Array[] {
|
|
108
|
+
return this.getPublicKeys();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
|
|
113
|
+
*/
|
|
114
|
+
public get type(): KeypairType {
|
|
115
|
+
return this.#type;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @name addPair
|
|
120
|
+
* @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
121
|
+
*/
|
|
122
|
+
public addPair(pair: KeyringPair): KeyringPair {
|
|
123
|
+
return this.#pairs.add(pair);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @name addFromAddress
|
|
128
|
+
* @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
129
|
+
* @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
|
|
130
|
+
* the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
|
|
131
|
+
* of an account backup), and then generates a keyring pair from them that it passes to
|
|
132
|
+
* `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.
|
|
133
|
+
*/
|
|
134
|
+
public addFromAddress(
|
|
135
|
+
address: string | Uint8Array,
|
|
136
|
+
meta: KeyringPair$Meta = {},
|
|
137
|
+
encoded: Uint8Array | null = null,
|
|
138
|
+
type: KeypairType = this.type,
|
|
139
|
+
ignoreChecksum?: boolean,
|
|
140
|
+
encType?: EncryptedJsonEncoding[],
|
|
141
|
+
): KeyringPair {
|
|
142
|
+
const publicKey = this.decodeAddress(address, ignoreChecksum);
|
|
143
|
+
|
|
144
|
+
return this.addPair(
|
|
145
|
+
createPair(
|
|
146
|
+
{ toSS58: this.encodeAddress, type },
|
|
147
|
+
{ publicKey, secretKey: new Uint8Array() },
|
|
148
|
+
meta,
|
|
149
|
+
encoded,
|
|
150
|
+
encType,
|
|
151
|
+
),
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @name addFromJson
|
|
157
|
+
* @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
158
|
+
* @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
|
|
159
|
+
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
160
|
+
* `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.
|
|
161
|
+
*/
|
|
162
|
+
public addFromJson(
|
|
163
|
+
json: KeyringPair$Json,
|
|
164
|
+
ignoreChecksum?: boolean,
|
|
165
|
+
): KeyringPair {
|
|
166
|
+
return this.addPair(this.createFromJson(json, ignoreChecksum));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @name addFromMnemonic
|
|
171
|
+
* @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
172
|
+
* @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
|
|
173
|
+
* argument and a metadata argument that contains account information (that may be obtained from the json file
|
|
174
|
+
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
175
|
+
* `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.
|
|
176
|
+
*/
|
|
177
|
+
public addFromMnemonic(
|
|
178
|
+
mnemonic: string,
|
|
179
|
+
meta: KeyringPair$Meta = {},
|
|
180
|
+
type: KeypairType = this.type,
|
|
181
|
+
): KeyringPair {
|
|
182
|
+
return this.addFromUri(mnemonic, meta, type);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @name addFromPair
|
|
187
|
+
* @summary Stores an account created from an explicit publicKey/secreteKey combination
|
|
188
|
+
*/
|
|
189
|
+
public addFromPair(
|
|
190
|
+
pair: Keypair,
|
|
191
|
+
meta: KeyringPair$Meta = {},
|
|
192
|
+
type: KeypairType = this.type,
|
|
193
|
+
): KeyringPair {
|
|
194
|
+
return this.addPair(this.createFromPair(pair, meta, type));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @name addFromSeed
|
|
199
|
+
* @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
200
|
+
* @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
|
|
201
|
+
* Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
|
|
202
|
+
* `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.
|
|
203
|
+
*/
|
|
204
|
+
public addFromSeed(
|
|
205
|
+
seed: Uint8Array,
|
|
206
|
+
meta: KeyringPair$Meta = {},
|
|
207
|
+
type: KeypairType = this.type,
|
|
208
|
+
): KeyringPair {
|
|
209
|
+
return this.addPair(
|
|
210
|
+
createPair(
|
|
211
|
+
{ toSS58: this.encodeAddress, type },
|
|
212
|
+
PairFromSeed[type](seed),
|
|
213
|
+
meta,
|
|
214
|
+
null,
|
|
215
|
+
),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @name addFromUri
|
|
221
|
+
* @summary Creates an account via an suri
|
|
222
|
+
* @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)
|
|
223
|
+
*/
|
|
224
|
+
public addFromUri(
|
|
225
|
+
suri: string,
|
|
226
|
+
meta: KeyringPair$Meta = {},
|
|
227
|
+
type: KeypairType = this.type,
|
|
228
|
+
): KeyringPair {
|
|
229
|
+
return this.addPair(this.createFromUri(suri, meta, type));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @name createFromJson
|
|
234
|
+
* @description Creates a pair from a JSON keyfile
|
|
235
|
+
*/
|
|
236
|
+
public createFromJson(
|
|
237
|
+
{
|
|
238
|
+
address,
|
|
239
|
+
encoded,
|
|
240
|
+
encoding: { content, type, version },
|
|
241
|
+
meta,
|
|
242
|
+
}: KeyringPair$Json,
|
|
243
|
+
ignoreChecksum?: boolean,
|
|
244
|
+
): KeyringPair {
|
|
245
|
+
if (version === "3" && content[0] !== "pkcs8") {
|
|
246
|
+
throw new Error(
|
|
247
|
+
`Unable to decode non-pkcs8 type, [${content.join(",")}] found}`,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const cryptoType =
|
|
252
|
+
version === "0" || !Array.isArray(content) ? this.type : content[1];
|
|
253
|
+
|
|
254
|
+
if (!cryptoType) {
|
|
255
|
+
throw new Error("cryptoType is undefined");
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const encType = !Array.isArray(type) ? [type] : type;
|
|
259
|
+
|
|
260
|
+
if (!["sr25519"].includes(cryptoType)) {
|
|
261
|
+
throw new Error(`Unknown crypto type ${cryptoType}`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 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
|
|
265
|
+
const publicKey = isHex(address)
|
|
266
|
+
? hexToU8a(address)
|
|
267
|
+
: this.decodeAddress(address, ignoreChecksum);
|
|
268
|
+
const decoded = isHex(encoded) ? hexToU8a(encoded) : base64Decode(encoded);
|
|
269
|
+
|
|
270
|
+
return createPair(
|
|
271
|
+
{ toSS58: this.encodeAddress, type: cryptoType as KeypairType },
|
|
272
|
+
{ publicKey, secretKey: new Uint8Array() },
|
|
273
|
+
meta,
|
|
274
|
+
decoded,
|
|
275
|
+
encType,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* @name createFromPair
|
|
281
|
+
* @summary Creates a pair from an explicit publicKey/secreteKey combination
|
|
282
|
+
*/
|
|
283
|
+
public createFromPair(
|
|
284
|
+
pair: Keypair,
|
|
285
|
+
meta: KeyringPair$Meta = {},
|
|
286
|
+
type: KeypairType = this.type,
|
|
287
|
+
): KeyringPair {
|
|
288
|
+
return createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* @name createFromUri
|
|
293
|
+
* @summary Creates a Keypair from an suri
|
|
294
|
+
* @description This creates a pair from the suri, but does not add it to the keyring
|
|
295
|
+
*/
|
|
296
|
+
public createFromUri(
|
|
297
|
+
_suri: string,
|
|
298
|
+
meta: KeyringPair$Meta = {},
|
|
299
|
+
type: KeypairType = this.type,
|
|
300
|
+
): KeyringPair {
|
|
301
|
+
// here we only aut-add the dev phrase if we have a hard-derived path
|
|
302
|
+
const suri = _suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri;
|
|
303
|
+
const { derivePath, password, path, phrase } = keyExtractSuri(suri);
|
|
304
|
+
|
|
305
|
+
let seed: Uint8Array;
|
|
306
|
+
const isPhraseHex = isHex(phrase, 256);
|
|
307
|
+
|
|
308
|
+
if (isPhraseHex) {
|
|
309
|
+
seed = hexToU8a(phrase);
|
|
310
|
+
} else {
|
|
311
|
+
const parts = phrase.split(" ");
|
|
312
|
+
|
|
313
|
+
if ([12, 15, 18, 21, 24].includes(parts.length)) {
|
|
314
|
+
seed =
|
|
315
|
+
type === "ethereum"
|
|
316
|
+
? (() => {
|
|
317
|
+
throw new Error(
|
|
318
|
+
"Not implemented - Prosopo Keyring supports sr25519 only",
|
|
319
|
+
);
|
|
320
|
+
})()
|
|
321
|
+
: mnemonicToMiniSecret(phrase, password);
|
|
322
|
+
} else {
|
|
323
|
+
if (phrase.length > 32) {
|
|
324
|
+
throw new Error(
|
|
325
|
+
"specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes",
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
seed = stringToU8a(phrase.padEnd(32));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const derived = keyFromPath(PairFromSeed[type](seed), path, type);
|
|
333
|
+
|
|
334
|
+
return createPair(
|
|
335
|
+
{ toSS58: this.encodeAddress, type },
|
|
336
|
+
derived,
|
|
337
|
+
meta,
|
|
338
|
+
null,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* @name encodeAddress
|
|
344
|
+
* @description Encodes the input into an ss58 representation
|
|
345
|
+
*/
|
|
346
|
+
public encodeAddress = (
|
|
347
|
+
address: Uint8Array | string,
|
|
348
|
+
ss58Format?: number,
|
|
349
|
+
): string => {
|
|
350
|
+
return encodeAddress(address, ss58Format ?? this.#ss58);
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* @name getPair
|
|
355
|
+
* @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
|
|
356
|
+
* @description Returns a keyring pair value from the keyring pair dictionary by performing
|
|
357
|
+
* a key lookup using the provided account address or public key (after decoding it).
|
|
358
|
+
*/
|
|
359
|
+
public getPair(address: string | Uint8Array): KeyringPair {
|
|
360
|
+
return this.#pairs.get(address);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* @name getPairs
|
|
365
|
+
* @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
|
|
366
|
+
* @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
|
|
367
|
+
*/
|
|
368
|
+
public getPairs(): KeyringPair[] {
|
|
369
|
+
return this.#pairs.all();
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* @name getPublicKeys
|
|
374
|
+
* @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
|
|
375
|
+
* @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.
|
|
376
|
+
*/
|
|
377
|
+
public getPublicKeys(): Uint8Array[] {
|
|
378
|
+
return this.#pairs.all().map(pairToPublic);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @name removePair
|
|
383
|
+
* @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
|
|
384
|
+
*/
|
|
385
|
+
public removePair(address: string | Uint8Array): void {
|
|
386
|
+
this.#pairs.remove(address);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* @name setSS58Format;
|
|
391
|
+
* @description Sets the ss58 format for the keyring
|
|
392
|
+
*/
|
|
393
|
+
public setSS58Format(ss58: number): void {
|
|
394
|
+
this.#ss58 = ss58;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* @name toJson
|
|
399
|
+
* @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
|
|
400
|
+
* @description Returns a JSON object containing the metadata associated with an account
|
|
401
|
+
* when valid address or public key and when the account passphrase is provided if the account secret
|
|
402
|
+
* is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
|
|
403
|
+
* may backup their account to a JSON file that contains this information.
|
|
404
|
+
*/
|
|
405
|
+
public toJson(
|
|
406
|
+
address: string | Uint8Array,
|
|
407
|
+
passphrase?: string,
|
|
408
|
+
): KeyringPair$Json {
|
|
409
|
+
return this.#pairs.get(address).toJson(passphrase);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/keyring authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { isHex, isU8a, u8aToU8a } from "@polkadot/util";
|
|
5
|
+
import type { KeyringPair, KeyringPairs } from "@prosopo/types";
|
|
6
|
+
|
|
7
|
+
import { u8aToHex } from "@prosopo/util";
|
|
8
|
+
import { decodeAddress } from "@prosopo/util-crypto";
|
|
9
|
+
|
|
10
|
+
type KeyringPairMap = Record<string, KeyringPair>;
|
|
11
|
+
|
|
12
|
+
export class Pairs implements KeyringPairs {
|
|
13
|
+
readonly #map: KeyringPairMap = {};
|
|
14
|
+
|
|
15
|
+
public add(pair: KeyringPair): KeyringPair {
|
|
16
|
+
this.#map[decodeAddress(pair.address).toString()] = pair;
|
|
17
|
+
|
|
18
|
+
return pair;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public all(): KeyringPair[] {
|
|
22
|
+
return Object.values(this.#map);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public get(address: string | Uint8Array): KeyringPair {
|
|
26
|
+
const pair = this.#map[decodeAddress(address).toString()];
|
|
27
|
+
|
|
28
|
+
if (!pair) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Unable to retrieve keypair '${
|
|
31
|
+
isU8a(address) || isHex(address)
|
|
32
|
+
? u8aToHex(u8aToU8a(address))
|
|
33
|
+
: address
|
|
34
|
+
}'`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return pair;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public remove(address: string | Uint8Array): void {
|
|
42
|
+
delete this.#map[decodeAddress(address).toString()];
|
|
43
|
+
}
|
|
44
|
+
}
|