Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / utils / account.ts
blobe9bcc441fadaaaa692bdf3d3e4c7445751840e8b
1 import { type WasmAccount, WasmApiBitcoinAddressesCreationPayload } from '@proton/andromeda';
2 import { type DecryptedAddressKey } from '@proton/shared/lib/interfaces';
4 import { signData } from './crypto';
6 export const computeAddress = async (
7     wasmAccount: WasmAccount,
8     walletAccountAddressKey: DecryptedAddressKey,
9     bitcoinAddressIndex?: number | null
10 ) => {
11     const { address, index } = bitcoinAddressIndex
12         ? await wasmAccount.peekReceiveAddress(bitcoinAddressIndex)
13         : await wasmAccount.getNextReceiveAddress();
15     const signature = await signData(address, 'wallet.bitcoin-address', [walletAccountAddressKey.privateKey]);
17     return {
18         BitcoinAddressIndex: index,
19         BitcoinAddress: address,
20         BitcoinAddressSignature: signature,
21     };
24 export const removeMasterPrefix = (derivationPath: string) => {
25     return derivationPath.replace(/m\//, '');
28 export const generateBitcoinAddressesPayloadToFillPool = async ({
29     addressesToCreate,
30     wasmAccount,
31     walletAccountAddressKey,
32 }: {
33     addressesToCreate: number;
34     wasmAccount: WasmAccount;
35     walletAccountAddressKey: DecryptedAddressKey;
36 }) => {
37     if (addressesToCreate > 0) {
38         const payload = new WasmApiBitcoinAddressesCreationPayload();
40         for (let i = 1; i <= addressesToCreate; i++) {
41             try {
42                 const addressData = await computeAddress(wasmAccount, walletAccountAddressKey);
43                 payload.push(addressData);
44             } catch (e) {
45                 console.error('Could not create bitcoin address creation payload', e);
46             }
47         }
49         return payload;
50     }