-
Notifications
You must be signed in to change notification settings - Fork 7
/
pairs.ts
42 lines (31 loc) · 1.23 KB
/
pairs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2017-2022 @polkadot/keyring authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from 'https://deno.land/x/polkadot@0.0.6/util/types.ts';
import type { KeyringPair, KeyringPairs } from './types.ts';
import { isHex, isU8a, u8aToHex, u8aToU8a } from 'https://deno.land/x/polkadot@0.0.6/util/mod.ts';
import { decodeAddress } from 'https://deno.land/x/polkadot@0.0.6/util-crypto/mod.ts';
type KeyringPairMap = Record<string, KeyringPair>;
export class Pairs implements KeyringPairs {
readonly #map: KeyringPairMap = {};
public add (pair: KeyringPair): KeyringPair {
this.#map[decodeAddress(pair.address).toString()] = pair;
return pair;
}
public all (): KeyringPair[] {
return Object.values(this.#map);
}
public get (address: HexString | string | Uint8Array): KeyringPair {
const pair = this.#map[decodeAddress(address).toString()];
if (!pair) {
throw new Error(`Unable to retrieve keypair '${
isU8a(address) || isHex(address)
? u8aToHex(u8aToU8a(address))
: address
}'`);
}
return pair;
}
public remove (address: HexString | string | Uint8Array): void {
delete this.#map[decodeAddress(address).toString()];
}
}