Skip to content

Commit

Permalink
keep same target ordering
Browse files Browse the repository at this point in the history
previously, the silent payment outputs were being added at the end
of the target array, which changes the order from what the caller requested.

instead, keep track of the original index while creating silent payment
outputs and return the targets in the original order.
  • Loading branch information
josibake authored and Overtorment committed May 31, 2024
1 parent ac3faa0 commit 1e3ff96
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type Target = {

export type SilentPaymentGroup = {
Bscan: Buffer;
BmValues: Array<[Buffer, number | undefined]>;
BmValues: Array<[Buffer, number | undefined, number]>;
};

function taggedHash(tag: string, data: Buffer): Buffer {
Expand All @@ -45,12 +45,13 @@ export class SilentPayment {
* Numeric values (if present) for targets are passed through.
*/
createTransaction(utxos: UTXO[], targets: Target[]): Target[] {
const ret: Target[] = [];
const ret: Target[] = new Array(targets.length);

const silentPaymentGroups: Array<SilentPaymentGroup> = [];
for (const target of targets) {
for (let i = 0; i < targets.length; i++) {
const target = targets[i];
if (!target.address?.startsWith("sp1")) {
ret.push(target); // passthrough
ret[i] = target; // passthrough
continue;
}

Expand All @@ -66,11 +67,11 @@ export class SilentPayment {
// Addresses with the same Bscan key all belong to the same recipient
const recipient = silentPaymentGroups.find((group) => Buffer.compare(group.Bscan, Bscan) === 0);
if (recipient) {
recipient.BmValues.push([Bm, target.value]);
recipient.BmValues.push([Bm, target.value, i]);
} else {
silentPaymentGroups.push({
Bscan: Bscan,
BmValues: [[Bm, target.value]],
BmValues: [[Bm, target.value, i]],
});
}
}
Expand All @@ -87,7 +88,7 @@ export class SilentPayment {
const ecdh_shared_secret = Buffer.from(ecc.getSharedSecret(ecdh_shared_secret_step1, group.Bscan) as Uint8Array);

let k = 0;
for (const [Bm, amount] of group.BmValues) {
for (const [Bm, amount, i] of group.BmValues) {
const tk = taggedHash("BIP0352/SharedSecret", Buffer.concat([ecdh_shared_secret!, SilentPayment._ser32(k)]));

// Let Pmk = tk·G + Bm
Expand All @@ -97,7 +98,7 @@ export class SilentPayment {
const address = SilentPayment.pubkeyToAddress(Pmk.slice(1).toString("hex"));
const newTarget: Target = { address };
newTarget.value = amount;
ret.push(newTarget);
ret[i] = newTarget;
k += 1;
}
}
Expand Down

0 comments on commit 1e3ff96

Please sign in to comment.