Skip to content

Commit

Permalink
Merge pull request #134 from capitalist42/use-cashu-crypto-library
Browse files Browse the repository at this point in the history
Use cashu crypto library functions, remove dhke and secrets module
  • Loading branch information
gandlafbtc authored May 21, 2024
2 parents 3d8343e + 4535e0f commit 3e20f45
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 357 deletions.
57 changes: 45 additions & 12 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { bytesToHex, randomBytes } from '@noble/hashes/utils';
import { CashuMint } from './CashuMint.js';
import * as dhke from './DHKE.js';
import { BlindedMessage } from './model/BlindedMessage.js';
import {
type AmountPreference,
Expand All @@ -20,7 +19,8 @@ import {
type SplitPayload,
type Token,
type TokenEntry,
CheckStateEnum
CheckStateEnum,
SerializedBlindedSignature
} from './model/types/index.js';
import {
bytesToNumber,
Expand All @@ -29,13 +29,21 @@ import {
getDefaultAmountPreference,
splitAmount
} from './utils.js';
import { deriveBlindingFactor, deriveSecret, deriveSeedFromMnemonic } from './secrets.js';
import { validateMnemonic } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { hashToCurve, pointFromHex } from '@cashu/crypto/modules/common';
import {
blindMessage,
constructProofFromPromise,
serializeProof
} from '@cashu/crypto/modules/client';
import {
deriveBlindingFactor,
deriveSecret,
deriveSeedFromMnemonic
} from '@cashu/crypto/modules/client/NUT09';
import { createP2PKsecret, getSignedProofs } from '@cashu/crypto/modules/client/NUT11';
import { type Proof as NUT11Proof } from '@cashu/crypto/modules/common/index';
import { serializeProof } from '@cashu/crypto/modules/client';
import { pointFromHex } from './DHKE';

/**
* Class that represents a Cashu wallet.
Expand Down Expand Up @@ -189,7 +197,7 @@ class CashuWallet {
options?.privkey
);
const { signatures } = await CashuMint.split(tokenEntry.mint, payload);
const newProofs = dhke.constructProofs(
const newProofs = this.constructProofs(
signatures,
blindedMessages.rs,
blindedMessages.secrets,
Expand Down Expand Up @@ -260,7 +268,7 @@ class CashuWallet {
options?.privkey
);
const { signatures } = await this.mint.split(payload);
const proofs = dhke.constructProofs(
const proofs = this.constructProofs(
signatures,
blindedMessages.rs,
blindedMessages.secrets,
Expand Down Expand Up @@ -316,7 +324,7 @@ class CashuWallet {
);

return {
proofs: dhke.constructProofs(promises, validRs, validSecrets, keys)
proofs: this.constructProofs(promises, validRs, validSecrets, keys)
};
}

Expand Down Expand Up @@ -387,7 +395,7 @@ class CashuWallet {
};
const { signatures } = await this.mint.mint(mintPayload);
return {
proofs: dhke.constructProofs(signatures, rs, secrets, keyset)
proofs: this.constructProofs(signatures, rs, secrets, keyset)
};
}

Expand Down Expand Up @@ -435,7 +443,7 @@ class CashuWallet {
isPaid: meltResponse.paid ?? false,
preimage: meltResponse.payment_preimage,
change: meltResponse?.change
? dhke.constructProofs(meltResponse.change, rs, secrets, keys)
? this.constructProofs(meltResponse.change, rs, secrets, keys)
: []
};
}
Expand Down Expand Up @@ -572,7 +580,7 @@ class CashuWallet {
*/
async checkProofsSpent<T extends { secret: string }>(proofs: Array<T>): Promise<Array<T>> {
const enc = new TextEncoder();
const Ys = proofs.map((p) => dhke.hashToCurve(enc.encode(p.secret)).toHex(true));
const Ys = proofs.map((p) => hashToCurve(enc.encode(p.secret)).toHex(true));
const payload = {
// array of Ys of proofs to check
Ys: Ys
Expand Down Expand Up @@ -652,7 +660,7 @@ class CashuWallet {
secretBytes = new TextEncoder().encode(secretHex);
}
secrets.push(secretBytes);
const { B_, r } = dhke.blindMessage(secretBytes, deterministicR);
const { B_, r } = blindMessage(secretBytes, deterministicR);
rs.push(r);
const blindedMessage = new BlindedMessage(amounts[i], B_, keysetId);
blindedMessages.push(blindedMessage.getSerializedBlindedMessage());
Expand Down Expand Up @@ -682,6 +690,31 @@ class CashuWallet {
const { blindedMessages, rs, secrets } = this.createBlindedMessages(amounts, keysetId, counter);
return { blindedMessages, secrets, rs };
}

/**
* construct proofs from @params promises, @params rs, @params secrets, and @params keyset
* @param promises array of serialized blinded signatures
* @param rs arrays of binding factors
* @param secrets array of secrets
* @param keyset mint keyset
* @returns array of serialized proofs
*/
private constructProofs(
promises: Array<SerializedBlindedSignature>,
rs: Array<bigint>,
secrets: Array<Uint8Array>,
keyset: MintKeys
): Array<Proof> {
return promises
.map((p: SerializedBlindedSignature, i: number) => {
const blindSignature = { id: p.id, amount: p.amount, C_: pointFromHex(p.C_) };
const r = rs[i];
const secret = secrets[i];
const A = pointFromHex(keyset.keys[p.amount]);
return constructProofFromPromise(blindSignature, r, secret, A);
})
.map((p) => serializeProof(p) as Proof);
}
}

export { CashuWallet };
72 changes: 0 additions & 72 deletions src/DHKE.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CashuMint } from './CashuMint.js';
import { CashuWallet } from './CashuWallet.js';
import { setGlobalRequestOptions } from './request.js';
import { generateNewMnemonic, deriveSeedFromMnemonic } from './secrets.js';
import { generateNewMnemonic, deriveSeedFromMnemonic } from '@cashu/crypto/modules/client/NUT09';
import { getEncodedToken, getDecodedToken, deriveKeysetId } from './utils.js';

export * from './model/types/index.js';
Expand Down
62 changes: 0 additions & 62 deletions src/secrets.ts

This file was deleted.

72 changes: 0 additions & 72 deletions test/crypto.scheme.test.ts

This file was deleted.

Loading

0 comments on commit 3e20f45

Please sign in to comment.