Skip to content

Commit

Permalink
add 'public' modifier to wallet methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 authored and rileystephens28 committed Jun 10, 2024
1 parent fe3ec86 commit 5897a8c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
14 changes: 7 additions & 7 deletions src/wallet/hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -140,20 +140,20 @@ 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;
}
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);
Expand Down Expand Up @@ -187,7 +187,7 @@ export abstract class AbstractHDWallet {

abstract sendTransaction(tx: TransactionRequest): Promise<TransactionResponse>;

connect(provider: Provider): void {
public connect(provider: Provider): void {
this.provider = provider;
}

Expand Down
68 changes: 34 additions & 34 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -68,31 +68,31 @@ 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);
});

}

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<string>} The serialized transaction.
* @throws {Error} If the UTXO transaction is invalid.
*/
async signTransaction(tx: QiTransactionRequest): Promise<string> {
const txobj = QiTransaction.from(<TransactionLike>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<string>} The serialized transaction.
* @throws {Error} If the UTXO transaction is invalid.
*/
public async signTransaction(tx: QiTransactionRequest): Promise<string> {
const txobj = QiTransaction.from(<TransactionLike>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);

Expand All @@ -108,19 +108,19 @@ export class QiHDWallet extends AbstractHDWallet {
return txobj.serialized;
}

async sendTransaction(tx: QiTransactionRequest): Promise<TransactionResponse> {
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<TransactionResponse> {
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)) {
Expand Down Expand Up @@ -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<void> {
public async scan(zone: Zone, account: number = 0): Promise<void> {
this.validateZone(zone);
// flush the existing addresses and outpoints
this._addresses = new Map();
Expand All @@ -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<void> {
public async sync(zone: Zone, account?: number): Promise<void> {
this.validateZone(zone);
if (account) {
await this._scan(zone, account);
Expand Down Expand Up @@ -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<Outpoint[]> {
private async getOutpointsByAddress(address: string): Promise<Outpoint[]> {
try {
const outpointsMap = await this.provider!.getOutpointsByAddress(address);
if (!outpointsMap) {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/quai-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class QuaiHDWallet extends AbstractHDWallet {
return changeNode.deriveChild(fromAddressInfo.index);
}

async signTransaction(tx: QuaiTransactionRequest): Promise<string> {
public async signTransaction(tx: QuaiTransactionRequest): Promise<string> {
const from = await resolveAddress(tx.from);
const fromNode = this._getHDNode(from);
const signedTx = await fromNode.signTransaction(tx);
return signedTx;
}

async sendTransaction(tx: QuaiTransactionRequest): Promise<TransactionResponse> {
public async sendTransaction(tx: QuaiTransactionRequest): Promise<TransactionResponse> {
if (!this.provider) {
throw new Error('Provider is not set');
}
Expand Down

0 comments on commit 5897a8c

Please sign in to comment.