Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of own addresses #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions js/pivx_shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export class PIVXShield {
*/
private pendingUnspentNotes: Map<string, Note[]> = new Map();

/**
* Array in which own addresses are stored
*/
private ownAddresses: string[] = [];

private promises: Map<
string,
{ res: (...args: any) => void; rej: (...args: any) => void }
Expand Down Expand Up @@ -275,6 +280,7 @@ export class PIVXShield {
);
pivxShield.diversifierIndex = shieldData.diversifierIndex;
pivxShield.unspentNotes = shieldData.unspentNotes;
await pivxShield.loadAddresses();
return pivxShield;
}

Expand Down Expand Up @@ -444,9 +450,45 @@ export class PIVXShield {
this.isTestnet,
);
this.diversifierIndex = diversifier_index;
this.ownAddresses.push(address);
return address;
}

/**
* @param address_to_check - shield address
* @returns true iff the shield address belongs to the wallet
*/
isMyAddress(address_to_check: string) {
return this.ownAddresses.includes(address_to_check);
}

/**
* loads used addresses
*/
async loadAddresses() {
let currentDiversifierIndex = new Array<number>(11).fill(0);
const totIterations = this.diversifierIndex.reduce(
(s, n, i) => s + n * 256 ** i,
0,
);
let j = 0;
while (j <= totIterations) {
const { address, diversifier_index } = await this.callWorker<{
address: string;
diversifier_index: number[];
}>(
"generate_next_shielding_payment_address",
this.extfvk,
currentDiversifierIndex,
this.isTestnet,
);
currentDiversifierIndex = diversifier_index;
j = currentDiversifierIndex.reduce((s, n, i) => s + n * 256 ** i, 0);
this.ownAddresses.push(address);
}
return false;
}

/**
* Load sapling prover. Must be done to create a transaction,
* But will be done lazily if note called explicitally.
Expand Down