Skip to content

Commit

Permalink
added advanced p2pk
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Dec 3, 2024
1 parent ea91e93 commit 6228292
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
29 changes: 21 additions & 8 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class CashuWallet {
privkey?: string;
requireDleq?: boolean;
blindingData?: Array<BlindingData>;
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> };
}
): Promise<Array<Proof>> {
if (typeof token === 'string') {
Expand All @@ -272,7 +273,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 @@ -310,6 +312,7 @@ class CashuWallet {
keep?: Array<BlindingData>;
send?: Array<BlindingData>;
};
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> };
}
): Promise<SendResponse> {
if (options?.includeDleq) {
Expand Down Expand Up @@ -492,6 +495,7 @@ class CashuWallet {
keep?: Array<BlindingData>;
send?: Array<BlindingData>;
};
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> };
}
): Promise<SendResponse> {
if (!options) options = {};
Expand Down Expand Up @@ -562,7 +566,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 @@ -662,6 +667,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 @@ -682,7 +688,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 @@ -790,7 +797,8 @@ class CashuWallet {
customBlindingData?: {
keep?: Array<BlindingData>;
send?: Array<BlindingData>;
}
},
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 @@ -811,7 +819,8 @@ class CashuWallet {
keyset,
counter,
pubkey,
outputAmounts?.keepAmounts
outputAmounts?.keepAmounts,
p2pk
);
}

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

Expand Down Expand Up @@ -1045,11 +1055,12 @@ class CashuWallet {
keyset: MintKeys,
counter?: number,
pubkey?: string,
outputAmounts?: Array<number>
outputAmounts?: Array<number>,
p2pk?: { pubkey: string; locktime?: number; refundKeys?: Array<string> }
): Array<BlindingData> {
let blindingData: Array<BlindingData>;
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 @@ -1061,6 +1072,8 @@ class CashuWallet {
keyset,
outputAmounts
);
} else if (p2pk) {
blindingData = BlindingData.createP2PKData(p2pk, amount, keyset);
} else {
blindingData = BlindingData.createRandomData(amount, keyset, outputAmounts);
}
Expand Down
33 changes: 27 additions & 6 deletions src/model/BlindingData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { createP2PKsecret } from '@cashu/crypto/modules/client/NUT11';
import {
Keys,
MintKeys,
Proof,
SerializedBlindedMessage,
Expand Down Expand Up @@ -64,17 +62,40 @@ export class BlindingData {
}

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 6228292

Please sign in to comment.