diff options
author | Florian Dold <florian@dold.me> | 2021-10-18 21:48:22 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-10-18 21:48:45 +0200 |
commit | 269022a526b670d602ca146f4df02850983bb72e (patch) | |
tree | e0a66e8b15ef305c99274c76964a119d0d9512ba /packages/taler-wallet-core/src | |
parent | 3a69f27412782872c1264e8a4dd1be13d57a8a80 (diff) | |
download | wallet-core-269022a526b670d602ca146f4df02850983bb72e.tar.xz |
move some more crypto to taler-util package
Diffstat (limited to 'packages/taler-wallet-core/src')
3 files changed, 34 insertions, 69 deletions
diff --git a/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts b/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts index 4ca0576b0..c42ece778 100644 --- a/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts +++ b/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts @@ -34,7 +34,14 @@ import { CoinSourceType, } from "../../db.js"; -import { CoinDepositPermission, RecoupRequest, RefreshPlanchetInfo } from "@gnu-taler/taler-util"; +import { + buildSigPS, + CoinDepositPermission, + RecoupRequest, + RefreshPlanchetInfo, + SignaturePurposeBuilder, + TalerSignaturePurpose, +} from "@gnu-taler/taler-util"; // FIXME: These types should be internal to the wallet! import { BenchmarkResult, @@ -80,24 +87,6 @@ import bigint from "big-integer"; const logger = new Logger("cryptoImplementation.ts"); -enum SignaturePurpose { - MERCHANT_TRACK_TRANSACTION = 1103, - WALLET_RESERVE_WITHDRAW = 1200, - WALLET_COIN_DEPOSIT = 1201, - MASTER_DENOMINATION_KEY_VALIDITY = 1025, - MASTER_WIRE_FEES = 1028, - MASTER_WIRE_DETAILS = 1030, - WALLET_COIN_MELT = 1202, - TEST = 4242, - MERCHANT_PAYMENT_OK = 1104, - MERCHANT_CONTRACT = 1101, - WALLET_COIN_RECOUP = 1203, - WALLET_COIN_LINK = 1204, - EXCHANGE_CONFIRM_RECOUP = 1039, - EXCHANGE_CONFIRM_RECOUP_REFRESH = 1041, - SYNC_BACKUP_UPLOAD = 1450, -} - function amountToBuffer(amount: AmountJson): Uint8Array { const buffer = new ArrayBuffer(8 + 4 + 12); const dvbuf = new DataView(buffer); @@ -139,38 +128,6 @@ function timestampRoundedToBuffer(ts: Timestamp): Uint8Array { return new Uint8Array(b); } -class SignaturePurposeBuilder { - private chunks: Uint8Array[] = []; - - constructor(private purposeNum: number) { } - - put(bytes: Uint8Array): SignaturePurposeBuilder { - this.chunks.push(Uint8Array.from(bytes)); - return this; - } - - build(): Uint8Array { - let payloadLen = 0; - for (const c of this.chunks) { - payloadLen += c.byteLength; - } - const buf = new ArrayBuffer(4 + 4 + payloadLen); - const u8buf = new Uint8Array(buf); - let p = 8; - for (const c of this.chunks) { - u8buf.set(c, p); - p += c.byteLength; - } - const dvbuf = new DataView(buf); - dvbuf.setUint32(0, payloadLen + 4 + 4); - dvbuf.setUint32(4, this.purposeNum); - return u8buf; - } -} - -function buildSigPS(purposeNum: number): SignaturePurposeBuilder { - return new SignaturePurposeBuilder(purposeNum); -} export class CryptoImplementation { static enableTracing = false; @@ -192,7 +149,9 @@ export class CryptoImplementation { const denomPubHash = hash(denomPub); const evHash = hash(ev); - const withdrawRequest = buildSigPS(SignaturePurpose.WALLET_RESERVE_WITHDRAW) + const withdrawRequest = buildSigPS( + TalerSignaturePurpose.WALLET_RESERVE_WITHDRAW, + ) .put(reservePub) .put(amountToBuffer(amountWithFee)) .put(denomPubHash) @@ -236,7 +195,7 @@ export class CryptoImplementation { } signTrackTransaction(req: SignTrackTransactionRequest): string { - const p = buildSigPS(SignaturePurpose.MERCHANT_TRACK_TRANSACTION) + const p = buildSigPS(TalerSignaturePurpose.MERCHANT_TRACK_TRANSACTION) .put(decodeCrock(req.contractTermsHash)) .put(decodeCrock(req.wireHash)) .put(decodeCrock(req.merchantPub)) @@ -249,7 +208,7 @@ export class CryptoImplementation { * Create and sign a message to recoup a coin. */ createRecoupRequest(coin: CoinRecord): RecoupRequest { - const p = buildSigPS(SignaturePurpose.WALLET_COIN_RECOUP) + const p = buildSigPS(TalerSignaturePurpose.WALLET_COIN_RECOUP) .put(decodeCrock(coin.coinPub)) .put(decodeCrock(coin.denomPubHash)) .put(decodeCrock(coin.blindingKey)) @@ -276,7 +235,7 @@ export class CryptoImplementation { contractHash: string, merchantPub: string, ): boolean { - const p = buildSigPS(SignaturePurpose.MERCHANT_PAYMENT_OK) + const p = buildSigPS(TalerSignaturePurpose.MERCHANT_PAYMENT_OK) .put(decodeCrock(contractHash)) .build(); const sigBytes = decodeCrock(sig); @@ -288,7 +247,7 @@ export class CryptoImplementation { * Check if a wire fee is correctly signed. */ isValidWireFee(type: string, wf: WireFee, masterPub: string): boolean { - const p = buildSigPS(SignaturePurpose.MASTER_WIRE_FEES) + const p = buildSigPS(TalerSignaturePurpose.MASTER_WIRE_FEES) .put(hash(stringToBytes(type + "\0"))) .put(timestampRoundedToBuffer(wf.startStamp)) .put(timestampRoundedToBuffer(wf.endStamp)) @@ -304,7 +263,7 @@ export class CryptoImplementation { * Check if the signature of a denomination is valid. */ isValidDenom(denom: DenominationRecord, masterPub: string): boolean { - const p = buildSigPS(SignaturePurpose.MASTER_DENOMINATION_KEY_VALIDITY) + const p = buildSigPS(TalerSignaturePurpose.MASTER_DENOMINATION_KEY_VALIDITY) .put(decodeCrock(masterPub)) .put(timestampRoundedToBuffer(denom.stampStart)) .put(timestampRoundedToBuffer(denom.stampExpireWithdraw)) @@ -334,7 +293,9 @@ export class CryptoImplementation { stringToBytes(paytoUri + "\0"), new Uint8Array(0), ); - const p = buildSigPS(SignaturePurpose.MASTER_WIRE_DETAILS).put(h).build(); + const p = buildSigPS(TalerSignaturePurpose.MASTER_WIRE_DETAILS) + .put(h) + .build(); return eddsaVerify(p, decodeCrock(sig), decodeCrock(masterPub)); } @@ -344,7 +305,7 @@ export class CryptoImplementation { merchantPub: string, ): boolean { const cthDec = decodeCrock(contractTermsHash); - const p = buildSigPS(SignaturePurpose.MERCHANT_CONTRACT) + const p = buildSigPS(TalerSignaturePurpose.MERCHANT_CONTRACT) .put(cthDec) .build(); return eddsaVerify(p, decodeCrock(sig), decodeCrock(merchantPub)); @@ -364,8 +325,8 @@ export class CryptoImplementation { eddsaGetPublic(key: string): { priv: string; pub: string } { return { priv: key, - pub: encodeCrock(eddsaGetPublic(decodeCrock(key))) - } + pub: encodeCrock(eddsaGetPublic(decodeCrock(key))), + }; } /** @@ -392,7 +353,7 @@ export class CryptoImplementation { * and deposit permissions for each given coin. */ signDepositPermission(depositInfo: DepositInfo): CoinDepositPermission { - const d = buildSigPS(SignaturePurpose.WALLET_COIN_DEPOSIT) + const d = buildSigPS(TalerSignaturePurpose.WALLET_COIN_DEPOSIT) .put(decodeCrock(depositInfo.contractTermsHash)) .put(decodeCrock(depositInfo.wireInfoHash)) .put(decodeCrock(depositInfo.denomPubHash)) @@ -503,7 +464,7 @@ export class CryptoImplementation { } const sessionHash = sessionHc.finish(); - const confirmData = buildSigPS(SignaturePurpose.WALLET_COIN_MELT) + const confirmData = buildSigPS(TalerSignaturePurpose.WALLET_COIN_MELT) .put(sessionHash) .put(decodeCrock(meltCoinDenomPubHash)) .put(amountToBuffer(valueWithFee)) @@ -549,7 +510,7 @@ export class CryptoImplementation { coinEv: string, ): string { const coinEvHash = hash(decodeCrock(coinEv)); - const coinLink = buildSigPS(SignaturePurpose.WALLET_COIN_LINK) + const coinLink = buildSigPS(TalerSignaturePurpose.WALLET_COIN_LINK) .put(decodeCrock(newDenomHash)) .put(decodeCrock(transferPub)) .put(coinEvHash) @@ -622,9 +583,7 @@ export class CryptoImplementation { } else { hOld = new Uint8Array(64); } - const sigBlob = new SignaturePurposeBuilder( - SignaturePurpose.SYNC_BACKUP_UPLOAD, - ) + const sigBlob = buildSigPS(TalerSignaturePurpose.SYNC_BACKUP_UPLOAD) .put(hOld) .put(hNew) .build(); diff --git a/packages/taler-wallet-core/src/operations/pay.ts b/packages/taler-wallet-core/src/operations/pay.ts index 079ffcd9a..8fad55994 100644 --- a/packages/taler-wallet-core/src/operations/pay.ts +++ b/packages/taler-wallet-core/src/operations/pay.ts @@ -763,6 +763,8 @@ async function processDownloadProposalImpl( proposalResp.contract_terms, ); + logger.info(`Contract terms hash: ${contractTermsHash}`); + let parsedContractTerms: ContractTerms; try { diff --git a/packages/taler-wallet-core/src/util/contractTerms.ts b/packages/taler-wallet-core/src/util/contractTerms.ts index 652ef707d..b064079e9 100644 --- a/packages/taler-wallet-core/src/util/contractTerms.ts +++ b/packages/taler-wallet-core/src/util/contractTerms.ts @@ -14,7 +14,7 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { canonicalJson } from "@gnu-taler/taler-util"; +import { canonicalJson, Logger } from "@gnu-taler/taler-util"; import { kdf } from "@gnu-taler/taler-util"; import { decodeCrock, @@ -24,6 +24,8 @@ import { stringToBytes, } from "@gnu-taler/taler-util"; +const logger = new Logger("contractTerms.ts"); + export namespace ContractTermsUtil { export type PathPredicate = (path: string[]) => boolean; @@ -222,6 +224,8 @@ export namespace ContractTermsUtil { export function hashContractTerms(contractTerms: unknown): string { const cleaned = scrub(contractTerms); const canon = canonicalJson(cleaned) + "\0"; - return encodeCrock(hash(stringToBytes(canon))); + const bytes = stringToBytes(canon); + logger.info(`contract terms before hashing: ${encodeCrock(bytes)}`); + return encodeCrock(hash(bytes)); } } |