Skip to content

Commit

Permalink
Merge branch 'locktime' into blinding
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Dec 6, 2024
2 parents 2b9e1a8 + 6228292 commit a5ad719
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
29 changes: 21 additions & 8 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class CashuWallet {
privkey?: string;
requireDleq?: boolean;
blindingData?: Array<BlindingDataLike>;
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> };
}
): Promise<Array<Proof>> {
if (typeof token === 'string') {
Expand All @@ -274,7 +275,8 @@ class CashuWallet {
options?.counter,
options?.pubkey,
options?.privkey,
options?.blindingData ? { keep: options.blindingData, send: [] } : undefined
options?.blindingData ? { keep: options.blindingData, send: [] } : undefined,
options?.p2pk
);
const { signatures } = await this.mint.swap(swapTransaction.payload);
return swapTransaction.blindingData.map((d, i) => d.toProof(signatures[i], keys));
Expand Down Expand Up @@ -312,6 +314,7 @@ class CashuWallet {
keep?: Array<BlindingDataLike>;
send?: Array<BlindingDataLike>;
};
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> };
}
): Promise<SendResponse> {
if (options?.includeDleq) {
Expand Down Expand Up @@ -494,6 +497,7 @@ class CashuWallet {
keep?: Array<BlindingDataLike>;
send?: Array<BlindingDataLike>;
};
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> };
}
): Promise<SendResponse> {
if (!options) options = {};
Expand Down Expand Up @@ -564,7 +568,8 @@ class CashuWallet {
options?.counter,
options?.pubkey,
options?.privkey,
options?.customBlindingData
options?.customBlindingData,
options?.p2pk
);
const { signatures } = await this.mint.swap(swapTransaction.payload);
const swapProofs = swapTransaction.blindingData.map((d, i) => d.toProof(signatures[i], keyset));
Expand Down Expand Up @@ -664,6 +669,7 @@ class CashuWallet {
proofsWeHave?: Array<Proof>;
counter?: number;
pubkey?: string;
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> };
}
): Promise<Array<Proof>> {
const keyset = await this.getKeys(options?.keysetId);
Expand All @@ -684,7 +690,8 @@ class CashuWallet {
keyset,
options?.counter,
options?.pubkey,
options?.outputAmounts?.keepAmounts
options?.outputAmounts?.keepAmounts,
options?.p2pk
);
const mintPayload: MintPayload = {
outputs: blindingData.map((d) => d.blindedMessage),
Expand Down Expand Up @@ -792,7 +799,8 @@ class CashuWallet {
customBlindingData?: {
keep?: Array<BlindingDataLike>;
send?: Array<BlindingDataLike>;
}
},
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> }
): SwapTransaction {
const totalAmount = proofsToSend.reduce((total: number, curr: Proof) => total + curr.amount, 0);
if (outputAmounts && outputAmounts.sendAmounts && !outputAmounts.keepAmounts) {
Expand All @@ -813,7 +821,8 @@ class CashuWallet {
keyset,
counter,
pubkey,
outputAmounts?.keepAmounts
outputAmounts?.keepAmounts,
p2pk
);
}

Expand All @@ -825,7 +834,8 @@ class CashuWallet {
keyset,
counter,
pubkey,
outputAmounts?.sendAmounts
outputAmounts?.sendAmounts,
p2pk
);
}

Expand Down Expand Up @@ -1047,11 +1057,12 @@ class CashuWallet {
keyset: MintKeys,
counter?: number,
pubkey?: string,
outputAmounts?: Array<number>
outputAmounts?: Array<number>,
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> }
): Array<BlindingDataLike> {
let blindingData: Array<BlindingDataLike>;
if (pubkey) {
blindingData = BlindingData.createP2PKData(pubkey, amount, keyset, outputAmounts);
blindingData = BlindingData.createP2PKData({ pubkey }, amount, keyset, outputAmounts);
} else if (counter || counter === 0) {
if (!this._seed) {
throw new Error('cannot create deterministic messages without seed');
Expand All @@ -1063,6 +1074,8 @@ class CashuWallet {
keyset,
outputAmounts
);
} else if (p2pk) {
blindingData = BlindingData.createP2PKData(p2pk, amount, keyset);
} else {
blindingData = BlindingData.createRandomData(amount, keyset, outputAmounts);
}
Expand Down
32 changes: 27 additions & 5 deletions src/model/BlindingData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createP2PKsecret } from '@cashu/crypto/modules/client/NUT11';
import {
MintKeys,
Proof,
Expand Down Expand Up @@ -71,17 +70,40 @@ export class BlindingData implements BlindingDataLike {
}

static createP2PKData(
pubkey: string,
p2pk: { pubkey: string; locktime?: number; refundKeys?: Array<string> },
amount: number,
keyset: MintKeys,
customSplit?: Array<number>
) {
const amounts = splitAmount(amount, keyset.keys, customSplit);
return amounts.map((a) => this._createP2PKData(pubkey, a, keyset.id));
return amounts.map((a) =>
this._createP2PKData(p2pk.pubkey, a, keyset.id, p2pk.locktime, p2pk.refundKeys)
);
}

private static _createP2PKData(pubkey: string, amount: number, keysetId: string) {
const secretBytes = createP2PKsecret(pubkey);
private static _createP2PKData(
pubkey: string,
amount: number,
keysetId: string,
locktime?: number,
refundKeys?: Array<string>
) {
const newSecret: [string, { nonce: string; data: string; tags: Array<any> }] = [
'P2PK',
{
nonce: bytesToHex(randomBytes(32)),
data: pubkey,
tags: []
}
];
if (locktime) {
newSecret[1].tags.push(['locktime', locktime]);
}
if (refundKeys) {
newSecret[1].tags.push(['refund', refundKeys]);
}
const parsed = JSON.stringify(newSecret);
const secretBytes = new TextEncoder().encode(parsed);
const { r, B_ } = blindMessage(secretBytes);
return new BlindingData(
new BlindedMessage(amount, B_, keysetId).getSerializedBlindedMessage(),
Expand Down

0 comments on commit a5ad719

Please sign in to comment.