diff --git a/src/wallet/hdwallet.ts b/src/wallet/hdwallet.ts index eb7ff5e4..6f9a8eef 100644 --- a/src/wallet/hdwallet.ts +++ b/src/wallet/hdwallet.ts @@ -77,14 +77,14 @@ export abstract class AbstractHDWallet { addrIndex++; // put a hard limit on the number of addresses to derive if (addrIndex - startingIndex > MAX_ADDRESS_DERIVATION_ATTEMPTS) { - throw new Error(`Failed to derive a valid address for the zone ${zone} after MAX_ADDRESS_DERIVATION_ATTEMPTS attempts.`); + throw new Error(`Failed to derive a valid address for the zone ${zone} after ${MAX_ADDRESS_DERIVATION_ATTEMPTS} attempts.`); } } while (!isValidAddressForZone(addressNode.address)); return addressNode; } - addAddress(account: number, addressIndex: number, zone: Zone): NeuteredAddressInfo { + public addAddress(account: number, addressIndex: number, zone: Zone): NeuteredAddressInfo { if (!this._accounts.has(account)) { this.addAccount(account); } @@ -111,7 +111,7 @@ export abstract class AbstractHDWallet { return neuteredAddressInfo; } - getNextAddress(accountIndex: number, zone: Zone): NeuteredAddressInfo { + public getNextAddress(accountIndex: number, zone: Zone): NeuteredAddressInfo { this.validateZone(zone); if (!this._accounts.has(accountIndex)) { this.addAccount(accountIndex); @@ -140,7 +140,7 @@ export abstract class AbstractHDWallet { return neuteredAddressInfo; } - getAddressInfo(address: string): NeuteredAddressInfo | null { + public getAddressInfo(address: string): NeuteredAddressInfo | null { const addressInfo = this._addresses.get(address); if (!addressInfo) { return null; @@ -148,12 +148,12 @@ export abstract class AbstractHDWallet { return addressInfo; } - getAddressesForAccount(account: number): NeuteredAddressInfo[] { + public getAddressesForAccount(account: number): NeuteredAddressInfo[] { const addresses = this._addresses.values(); return Array.from(addresses).filter((addressInfo) => addressInfo.account === account); } - getAddressesForZone(zone: Zone): NeuteredAddressInfo[] { + public getAddressesForZone(zone: Zone): NeuteredAddressInfo[] { this.validateZone(zone); const addresses = this._addresses.values(); return Array.from(addresses).filter((addressInfo) => addressInfo.zone === zone); @@ -187,7 +187,7 @@ export abstract class AbstractHDWallet { abstract sendTransaction(tx: TransactionRequest): Promise; - connect(provider: Provider): void { + public connect(provider: Provider): void { this.provider = provider; } diff --git a/src/wallet/qi-hdwallet.ts b/src/wallet/qi-hdwallet.ts index 9ba85d8e..62aceea7 100644 --- a/src/wallet/qi-hdwallet.ts +++ b/src/wallet/qi-hdwallet.ts @@ -42,7 +42,7 @@ export class QiHDWallet extends AbstractHDWallet { super(root, provider); } - getNextChangeAddress(account: number, zone: Zone): NeuteredAddressInfo { + public getNextChangeAddress(account: number, zone: Zone): NeuteredAddressInfo { this.validateZone(zone); if (!this._accounts.has(account)) { this.addAccount(account); @@ -68,7 +68,7 @@ export class QiHDWallet extends AbstractHDWallet { return neuteredAddressInfo; } - importOutpoints(outpoints: OutpointInfo[]): void { + public importOutpoints(outpoints: OutpointInfo[]): void { outpoints.forEach((outpoint) => { this.validateZone(outpoint.zone); this._outpoints.push(outpoint); @@ -76,23 +76,23 @@ export class QiHDWallet extends AbstractHDWallet { } - getOutpoints(zone: Zone): OutpointInfo[] { + public getOutpoints(zone: Zone): OutpointInfo[] { this.validateZone(zone); return this._outpoints.filter((outpoint) => outpoint.zone === zone); } - /** - * Signs a Qi transaction and returns the serialized transaction - * - * @param {QiTransactionRequest} tx - The transaction to sign. - * - * @returns {Promise} The serialized transaction. - * @throws {Error} If the UTXO transaction is invalid. - */ - async signTransaction(tx: QiTransactionRequest): Promise { - const txobj = QiTransaction.from(tx); - if (!txobj.txInputs || txobj.txInputs.length == 0 || !txobj.txOutputs) - throw new Error('Invalid UTXO transaction, missing inputs or outputs'); + /** + * Signs a Qi transaction and returns the serialized transaction + * + * @param {QiTransactionRequest} tx - The transaction to sign. + * + * @returns {Promise} The serialized transaction. + * @throws {Error} If the UTXO transaction is invalid. + */ + public async signTransaction(tx: QiTransactionRequest): Promise { + const txobj = QiTransaction.from(tx); + if (!txobj.txInputs || txobj.txInputs.length == 0 || !txobj.txOutputs) + throw new Error('Invalid UTXO transaction, missing inputs or outputs'); const hash = keccak_256(txobj.unsignedSerialized); @@ -108,19 +108,19 @@ export class QiHDWallet extends AbstractHDWallet { return txobj.serialized; } - async sendTransaction(tx: QiTransactionRequest): Promise { - if (!this.provider) { - throw new Error('Provider is not set'); - } - if (!tx.inputs || tx.inputs.length === 0) { - throw new Error('Transaction has no inputs'); - } - const input = tx.inputs[0]; - const address = computeAddress(hexlify(input.pub_key)); - const shard = getZoneForAddress(address); - if (!shard) { - throw new Error(`Address ${address} not found in any shard`); - } + public async sendTransaction(tx: QiTransactionRequest): Promise { + if (!this.provider) { + throw new Error("Provider is not set"); + } + if (!tx.inputs || tx.inputs.length === 0) { + throw new Error('Transaction has no inputs'); + } + const input = tx.inputs[0]; + const address = computeAddress(hexlify(input.pub_key)); + const shard = getZoneForAddress(address); + if (!shard) { + throw new Error(`Address ${address} not found in any shard`); + } // verify all inputs are from the same shard if (tx.inputs.some((input) => getZoneForAddress(computeAddress(hexlify(input.pub_key))) !== shard)) { @@ -200,7 +200,7 @@ export class QiHDWallet extends AbstractHDWallet { // scan scans the specified zone for addresses with unspent outputs. // Starting at index 0, tt will generate new addresses until // the gap limit is reached for both gap and change addresses. - async scan(zone: Zone, account: number = 0): Promise { + public async scan(zone: Zone, account: number = 0): Promise { this.validateZone(zone); // flush the existing addresses and outpoints this._addresses = new Map(); @@ -216,7 +216,7 @@ export class QiHDWallet extends AbstractHDWallet { // Starting at the last address index, it will generate new addresses until // the gap limit is reached for both gap and change addresses. // If no account is specified, it will scan all accounts known to the wallet - async sync(zone: Zone, account?: number): Promise { + public async sync(zone: Zone, account?: number): Promise { this.validateZone(zone); if (account) { await this._scan(zone, account); @@ -285,7 +285,7 @@ export class QiHDWallet extends AbstractHDWallet { // getOutpointsByAddress queries the network node for the outpoints of the specified address - private async getOutpointsByAddress(address: string): Promise { + private async getOutpointsByAddress(address: string): Promise { try { const outpointsMap = await this.provider!.getOutpointsByAddress(address); if (!outpointsMap) { @@ -297,19 +297,19 @@ export class QiHDWallet extends AbstractHDWallet { } } - getChangeAddressesForZone(zone: Zone): NeuteredAddressInfo[] { + public getChangeAddressesForZone(zone: Zone): NeuteredAddressInfo[] { this.validateZone(zone); const changeAddresses = this._changeAddresses.values(); return Array.from(changeAddresses).filter((addressInfo) => addressInfo.zone === zone); } - getGapAddressesForZone(zone: Zone): NeuteredAddressInfo[] { + public getGapAddressesForZone(zone: Zone): NeuteredAddressInfo[] { this.validateZone(zone); const gapAddresses = this._gapAddresses.filter((addressInfo) => addressInfo.zone === zone); return gapAddresses; } - getGapChangeAddressesForZone(zone: Zone): NeuteredAddressInfo[] { + public getGapChangeAddressesForZone(zone: Zone): NeuteredAddressInfo[] { this.validateZone(zone); const gapChangeAddresses = this._gapChangeAddresses.filter((addressInfo) => addressInfo.zone === zone); return gapChangeAddresses; diff --git a/src/wallet/quai-hdwallet.ts b/src/wallet/quai-hdwallet.ts index 500ec048..cfa76c9f 100644 --- a/src/wallet/quai-hdwallet.ts +++ b/src/wallet/quai-hdwallet.ts @@ -26,14 +26,14 @@ export class QuaiHDWallet extends AbstractHDWallet { return changeNode.deriveChild(fromAddressInfo.index); } - async signTransaction(tx: QuaiTransactionRequest): Promise { + public async signTransaction(tx: QuaiTransactionRequest): Promise { const from = await resolveAddress(tx.from); const fromNode = this._getHDNode(from); const signedTx = await fromNode.signTransaction(tx); return signedTx; } - async sendTransaction(tx: QuaiTransactionRequest): Promise { + public async sendTransaction(tx: QuaiTransactionRequest): Promise { if (!this.provider) { throw new Error('Provider is not set'); }