@alephium/web3-wallet
Version:
Simple wallets for Alephium
48 lines • 2.44 kB
JavaScript
import { web3, SignerProviderWithMultipleAccounts, groupOfAddress } from '@alephium/web3';
export class NodeWallet extends SignerProviderWithMultipleAccounts {
constructor(walletName, nodeProvider, explorerProvider) {
super();
this.walletName = walletName;
this.nodeProvider = nodeProvider ?? web3.getCurrentNodeProvider();
this.explorerProvider = explorerProvider ?? web3.getCurrentExplorerProvider();
}
async setSelectedAccount(address) {
await this.nodeProvider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: address });
}
async getAccounts() {
const walletAddresses = await this.nodeProvider.wallets.getWalletsWalletNameAddresses(this.walletName);
const accounts = walletAddresses.addresses.map((acc) => ({
keyType: 'default',
publicKey: acc.publicKey,
address: acc.address,
group: acc.group
}));
return accounts;
}
async unsafeGetSelectedAccount() {
const response = await this.nodeProvider.wallets.getWalletsWalletNameAddresses(this.walletName);
const selectedAddressInfo = response.addresses.find((info) => info.address === response.activeAddress);
return {
keyType: 'default',
address: selectedAddressInfo.address,
group: groupOfAddress(selectedAddressInfo.address),
publicKey: selectedAddressInfo.publicKey
};
}
async signRaw(signerAddress, hexString) {
const provider = this.nodeProvider;
const currentActiveAddressResponse = await provider.wallets.getWalletsWalletNameAddresses(this.walletName);
const { activeAddress } = currentActiveAddressResponse;
await provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: signerAddress });
const { signature } = await provider.wallets.postWalletsWalletNameSign(this.walletName, { data: hexString });
await provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: activeAddress });
return signature;
}
async unlock(password) {
return await this.nodeProvider.wallets.postWalletsWalletNameUnlock(this.walletName, { password });
}
async lock() {
return await this.nodeProvider.wallets.postWalletsWalletNameLock(this.walletName);
}
}
//# sourceMappingURL=node-wallet.js.map