/
merge_key_store.ts
135 lines (124 loc) · 4.31 KB
/
merge_key_store.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { KeyPair } from '@near-js/crypto';
import { KeyStore } from './keystore';
/**
* Keystore which can be used to merge multiple key stores into one virtual key store.
*
* @example
* ```js
* const { homedir } = require('os');
* import { connect, keyStores, utils } from 'near-api-js';
*
* const privateKey = '.......';
* const keyPair = utils.KeyPair.fromString(privateKey);
*
* const inMemoryKeyStore = new keyStores.InMemoryKeyStore();
* inMemoryKeyStore.setKey('testnet', 'example-account.testnet', keyPair);
*
* const fileSystemKeyStore = new keyStores.UnencryptedFileSystemKeyStore(`${homedir()}/.near-credentials`);
*
* const keyStore = new MergeKeyStore([
* inMemoryKeyStore,
* fileSystemKeyStore
* ]);
* const config = {
* keyStore, // instance of MergeKeyStore
* networkId: 'testnet',
* nodeUrl: 'https://rpc.testnet.near.org',
* walletUrl: 'https://wallet.testnet.near.org',
* helperUrl: 'https://helper.testnet.near.org',
* explorerUrl: 'https://explorer.testnet.near.org'
* };
*
* // inside an async function
* const near = await connect(config)
* ```
*/
interface MergeKeyStoreOptions {
writeKeyStoreIndex: number;
}
export class MergeKeyStore extends KeyStore {
private options: MergeKeyStoreOptions;
keyStores: KeyStore[];
/**
* @param keyStores read calls are attempted from start to end of array
* @param options KeyStore options
* @param options.writeKeyStoreIndex the keystore index that will receive all write calls
*/
constructor(keyStores: KeyStore[], options: MergeKeyStoreOptions = { writeKeyStoreIndex: 0 }) {
super();
this.options = options;
this.keyStores = keyStores;
}
/**
* Store a {@link KeyPair} to the first index of a key store array
* @param networkId The targeted network. (ex. default, betanet, etc…)
* @param accountId The NEAR account tied to the key pair
* @param keyPair The key pair to store in local storage
*/
async setKey(networkId: string, accountId: string, keyPair: KeyPair): Promise<void> {
await this.keyStores[this.options.writeKeyStoreIndex].setKey(networkId, accountId, keyPair);
}
/**
* Gets a {@link KeyPair} from the array of key stores
* @param networkId The targeted network. (ex. default, betanet, etc…)
* @param accountId The NEAR account tied to the key pair
* @returns {Promise<KeyPair>}
*/
async getKey(networkId: string, accountId: string): Promise<KeyPair> {
for (const keyStore of this.keyStores) {
const keyPair = await keyStore.getKey(networkId, accountId);
if (keyPair) {
return keyPair;
}
}
return null;
}
/**
* Removes a {@link KeyPair} from the array of key stores
* @param networkId The targeted network. (ex. default, betanet, etc…)
* @param accountId The NEAR account tied to the key pair
*/
async removeKey(networkId: string, accountId: string): Promise<void> {
for (const keyStore of this.keyStores) {
await keyStore.removeKey(networkId, accountId);
}
}
/**
* Removes all items from each key store
*/
async clear(): Promise<void> {
for (const keyStore of this.keyStores) {
await keyStore.clear();
}
}
/**
* Get the network(s) from the array of key stores
* @returns {Promise<string[]>}
*/
async getNetworks(): Promise<string[]> {
const result = new Set<string>();
for (const keyStore of this.keyStores) {
for (const network of await keyStore.getNetworks()) {
result.add(network);
}
}
return Array.from(result);
}
/**
* Gets the account(s) from the array of key stores
* @param networkId The targeted network. (ex. default, betanet, etc…)
*/
async getAccounts(networkId: string): Promise<string[]> {
const result = new Set<string>();
for (const keyStore of this.keyStores) {
for (const account of await keyStore.getAccounts(networkId)) {
result.add(account);
}
}
return Array.from(result);
}
/** @hidden */
toString(): string {
return `MergeKeyStore(${this.keyStores.join(', ')})`;
}
}