-
Notifications
You must be signed in to change notification settings - Fork 384
/
localStorage.js
68 lines (61 loc) · 2.08 KB
/
localStorage.js
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
import { getUnlockedMnemonicAndSeed } from './../wallet-seed';
import * as bip32 from 'bip32';
import nacl from 'tweetnacl';
import { Account } from '@solana/web3.js';
import bs58 from 'bs58';
import { derivePath } from 'ed25519-hd-key';
export const DERIVATION_PATH = {
deprecated: undefined,
bip44: 'bip44',
bip44Change: 'bip44Change',
bip44Root: 'bip44Root', // Ledger only.
};
export function getAccountFromSeed(
seed,
walletIndex,
dPath = undefined,
accountIndex = 0,
) {
const derivedSeed = deriveSeed(seed, walletIndex, dPath, accountIndex);
return new Account(nacl.sign.keyPair.fromSeed(derivedSeed).secretKey);
}
function deriveSeed(seed, walletIndex, derivationPath, accountIndex) {
switch (derivationPath) {
case DERIVATION_PATH.deprecated:
const path = `m/501'/${walletIndex}'/0/${accountIndex}`;
return bip32.fromSeed(seed).derivePath(path).privateKey;
case DERIVATION_PATH.bip44:
const path44 = `m/44'/501'/${walletIndex}'`;
return derivePath(path44, seed).key;
case DERIVATION_PATH.bip44Change:
const path44Change = `m/44'/501'/${walletIndex}'/0'`;
return derivePath(path44Change, seed).key;
default:
throw new Error(`invalid derivation path: ${derivationPath}`);
}
}
export class LocalStorageWalletProvider {
constructor(args) {
this.account = args.account;
this.publicKey = this.account.publicKey;
}
init = async () => {
const { seed } = await getUnlockedMnemonicAndSeed();
this.listAddresses = async (walletCount) => {
const seedBuffer = Buffer.from(seed, 'hex');
return [...Array(walletCount).keys()].map((walletIndex) => {
let address = getAccountFromSeed(seedBuffer, walletIndex).publicKey;
let name = localStorage.getItem(`name${walletIndex}`);
return { index: walletIndex, address, name };
});
};
return this;
};
signTransaction = async (transaction) => {
transaction.partialSign(this.account);
return transaction;
};
createSignature = (message) => {
return bs58.encode(nacl.sign.detached(message, this.account.secretKey));
};
}