@alephium/web3-wallet
Version:
Simple wallets for Alephium
69 lines (65 loc) • 3.29 kB
JavaScript
;
/*
Copyright 2018 - 2022 The Alephium Authors
This file is part of the alephium project.
The library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeWallet = void 0;
const web3_1 = require("@alephium/web3");
class NodeWallet extends web3_1.SignerProviderWithMultipleAccounts {
constructor(walletName, nodeProvider, explorerProvider) {
super();
this.walletName = walletName;
this.nodeProvider = nodeProvider ?? web3_1.web3.getCurrentNodeProvider();
this.explorerProvider = explorerProvider ?? web3_1.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: (0, web3_1.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 }); // set the address that's active back to previous state
return signature;
}
async unlock(password) {
return await this.nodeProvider.wallets.postWalletsWalletNameUnlock(this.walletName, { password });
}
async lock() {
return await this.nodeProvider.wallets.postWalletsWalletNameLock(this.walletName);
}
}
exports.NodeWallet = NodeWallet;