/*
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 .
*/
import { NodeProvider } from '../api'
import { Account, SignerWithNodeProvider } from '../signer'
export class NodeWallet extends SignerWithNodeProvider {
public walletName: string
public accounts: Account[] | undefined
constructor(provider: NodeProvider, walletName: string, alwaysSubmitTx = true) {
super(provider, alwaysSubmitTx)
this.walletName = walletName
}
async getAccounts(): Promise {
if (typeof this.accounts === 'undefined') {
this.accounts = await this.getAllAccounts()
}
return this.accounts
}
private async getAllAccounts(): Promise {
const walletAddresses = await this.provider.wallets.getWalletsWalletNameAddresses(this.walletName)
const accounts: Account[] = walletAddresses.addresses.map((acc) => ({
publicKey: acc.publicKey,
address: acc.address,
group: acc.group
}))
return accounts
}
static async FromCliqueClient(
provider: NodeProvider,
walletName: string,
alwaysSubmitTx = true
): Promise {
return new NodeWallet(provider, walletName, alwaysSubmitTx)
}
async signRaw(signerAddress: string, hexString: string): Promise {
const currentActiveAddressResponse = await this.provider.wallets.getWalletsWalletNameAddresses(this.walletName)
const { activeAddress } = currentActiveAddressResponse
await this.provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: signerAddress })
const { signature } = await this.provider.wallets.postWalletsWalletNameSign(this.walletName, { data: hexString })
await this.provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: activeAddress }) // set the address that's active back to previous state
return signature
}
async unlock(password: string): Promise {
return await this.provider.wallets.postWalletsWalletNameUnlock(this.walletName, { password })
}
async lock(): Promise {
return await this.provider.wallets.postWalletsWalletNameLock(this.walletName)
}
}