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

meltTokens signs P2PK proofs #165

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ class CashuWallet {
* @param proofsToSend proofs to melt
* @param options.keysetId? optionally set keysetId for blank outputs for returned change.
* @param options.counter? optionally set counter to derive secret deterministically. CashuWallet class must be initialized with seed phrase to take effect
* @param options.privkey? optionally set a private key to unlock P2PK locked secrets
* @returns
*/
async meltTokens(
Expand All @@ -423,6 +424,7 @@ class CashuWallet {
options?: {
keysetId?: string;
counter?: number;
privkey?: string;
}
): Promise<MeltTokensResponse> {
const keys = await this.getKeys(options?.keysetId);
Expand All @@ -432,6 +434,19 @@ class CashuWallet {
keys.id,
options?.counter
);
if (options?.privkey != undefined) {
proofsToSend = getSignedProofs(
proofsToSend.map((p) => {
return {
amount: p.amount,
C: pointFromHex(p.C),
id: p.id,
secret: new TextEncoder().encode(p.secret)
};
Comment on lines +439 to +445
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we refactor this into getSignedProofs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw it's in cashu/crypto and the Proof type has a different definition:

@cashu/cashu-ts

 */
export type Proof = {
	id: string;
	amount: number;
	secret: string;
	C: string;
};

vs @cashu/crypto:

export type Proof = {
	C: ProjPointType<bigint>;
	secret: Uint8Array;
	amount: number;
	id: string;
	witness?: Witness;
};

☠️

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thats not great. Unrelated to this PR, but we should add a utility that hides this back-and-forth between the two formats in the near future

}),
options.privkey
).map((p: NUT11Proof) => serializeProof(p));
}
const meltPayload: MeltPayload = {
quote: meltQuote.quote,
inputs: proofsToSend,
Expand All @@ -456,6 +471,7 @@ class CashuWallet {
* @param meltQuote melt quote for the invoice
* @param options.keysetId? optionally set keysetId for blank outputs for returned change.
* @param options.counter? optionally set counter to derive secret deterministically. CashuWallet class must be initialized with seed phrase to take effect
* @param options.privkey? optionally set a private key to unlock P2PK locked secrets
* @returns
*/
async payLnInvoice(
Expand All @@ -465,14 +481,16 @@ class CashuWallet {
options?: {
keysetId?: string;
counter?: number;
privkey?: string;
}
): Promise<MeltTokensResponse> {
if (!meltQuote) {
meltQuote = await this.mint.createMeltQuote({ unit: this._unit, request: invoice });
}
return await this.meltTokens(meltQuote, proofsToSend, {
keysetId: options?.keysetId,
counter: options?.counter
counter: options?.counter,
privkey: options?.privkey
});
}

Expand Down
23 changes: 23 additions & 0 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,27 @@ describe('mint api', () => {
}, 0)
).toBe(64);
});

test('mint and melt p2pk', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);

const privKeyBob = secp256k1.utils.randomPrivateKey();
const pubKeyBob = secp256k1.getPublicKey(privKeyBob);

const mintRequest = await wallet.createMintQuote(3000);

const proofs = await wallet.mintTokens(3000, mintRequest.quote, {
pubkey: bytesToHex(pubKeyBob)
});

const meltRequest = await wallet.createMeltQuote(externalInvoice);
const fee = meltRequest.fee_reserve;
expect(fee).toBeGreaterThan(0);
const response = await wallet.meltTokens(meltRequest, proofs.proofs, {
privkey: bytesToHex(privKeyBob)
});
expect(response).toBeDefined();
expect(response.isPaid).toBe(true);
});
});
Loading