From c6233094306cd264f8faa2041388dff01ff8cf01 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 21 Nov 2019 23:09:43 +0100 Subject: WIP: simplification and error handling --- src/crypto/cryptoApi-test.ts | 23 ++++++++++++----------- src/crypto/cryptoImplementation.ts | 16 ++++++++++------ 2 files changed, 22 insertions(+), 17 deletions(-) (limited to 'src/crypto') diff --git a/src/crypto/cryptoApi-test.ts b/src/crypto/cryptoApi-test.ts index 39f46c5c3..d9d42081c 100644 --- a/src/crypto/cryptoApi-test.ts +++ b/src/crypto/cryptoApi-test.ts @@ -22,6 +22,7 @@ import { DenominationRecord, DenominationStatus, ReserveRecord, + ReserveRecordStatus, } from "../dbTypes"; import { CryptoApi } from "./cryptoApi"; @@ -86,18 +87,18 @@ test("precoin creation", async t => { const crypto = new CryptoApi(new NodeCryptoWorkerFactory()); const { priv, pub } = await crypto.createEddsaKeypair(); const r: ReserveRecord = { - created: 0, - current_amount: null, - exchange_base_url: "https://example.com/exchange", + created: { t_ms: 0 }, + currentAmount: null, + exchangeBaseUrl: "https://example.com/exchange", hasPayback: false, - precoin_amount: { currency: "PUDOS", value: 0, fraction: 0 }, - requested_amount: { currency: "PUDOS", value: 0, fraction: 0 }, - reserve_priv: priv, - reserve_pub: pub, - timestamp_confirmed: 0, - timestamp_depleted: 0, - timestamp_reserve_info_posted: 0, - exchangeWire: "payto://foo" + precoinAmount: { currency: "PUDOS", value: 0, fraction: 0 }, + requestedAmount: { currency: "PUDOS", value: 0, fraction: 0 }, + reservePriv: priv, + reservePub: pub, + timestampConfirmed: undefined, + timestampReserveInfoPosted: undefined, + exchangeWire: "payto://foo", + reserveStatus: ReserveRecordStatus.UNCONFIRMED, }; const precoin = await crypto.createPreCoin(denomValid1, r); diff --git a/src/crypto/cryptoImplementation.ts b/src/crypto/cryptoImplementation.ts index d50d40027..7dd019c18 100644 --- a/src/crypto/cryptoImplementation.ts +++ b/src/crypto/cryptoImplementation.ts @@ -45,6 +45,7 @@ import * as native from "./emscInterface"; import { AmountJson } from "../amounts"; import * as Amounts from "../amounts"; import * as timer from "../timer"; +import { getRandomBytes, encodeCrock } from "./nativeCrypto"; export class CryptoImplementation { static enableTracing: boolean = false; @@ -60,9 +61,9 @@ export class CryptoImplementation { reserve: ReserveRecord, ): PreCoinRecord { const reservePriv = new native.EddsaPrivateKey(this.emsc); - reservePriv.loadCrock(reserve.reserve_priv); + reservePriv.loadCrock(reserve.reservePriv); const reservePub = new native.EddsaPublicKey(this.emsc); - reservePub.loadCrock(reserve.reserve_pub); + reservePub.loadCrock(reserve.reservePub); const denomPub = native.RsaPublicKey.fromCrock(this.emsc, denom.denomPub); const coinPriv = native.EddsaPrivateKey.create(this.emsc); const coinPub = coinPriv.getPublicKey(); @@ -103,7 +104,7 @@ export class CryptoImplementation { coinValue: denom.value, denomPub: denomPub.toCrock(), denomPubHash: denomPubHash.toCrock(), - exchangeBaseUrl: reserve.exchange_base_url, + exchangeBaseUrl: reserve.exchangeBaseUrl, isFromTip: false, reservePub: reservePub.toCrock(), withdrawSig: sig.toCrock(), @@ -199,14 +200,14 @@ export class CryptoImplementation { isValidWireFee(type: string, wf: WireFee, masterPub: string): boolean { const p = new native.MasterWireFeePS(this.emsc, { closing_fee: new native.Amount(this.emsc, wf.closingFee).toNbo(), - end_date: native.AbsoluteTimeNbo.fromStampSeconds(this.emsc, wf.endStamp), + end_date: native.AbsoluteTimeNbo.fromStampSeconds(this.emsc, (wf.endStamp.t_ms / 1000)), h_wire_method: native.ByteArray.fromStringWithNull( this.emsc, type, ).hash(), start_date: native.AbsoluteTimeNbo.fromStampSeconds( this.emsc, - wf.startStamp, + Math.floor(wf.startStamp.t_ms / 1000), ), wire_fee: new native.Amount(this.emsc, wf.wireFee).toNbo(), }); @@ -354,7 +355,7 @@ export class CryptoImplementation { const newAmount = new native.Amount(this.emsc, cd.coin.currentAmount); newAmount.sub(coinSpend); cd.coin.currentAmount = newAmount.toJson(); - cd.coin.status = CoinStatus.PurchasePending; + cd.coin.status = CoinStatus.Dirty; const d = new native.DepositRequestPS(this.emsc, { amount_with_fee: coinSpend.toNbo(), @@ -505,7 +506,10 @@ export class CryptoImplementation { valueOutput = Amounts.add(valueOutput, denom.value).amount; } + const refreshSessionId = encodeCrock(getRandomBytes(32)); + const refreshSession: RefreshSessionRecord = { + refreshSessionId, confirmSig, exchangeBaseUrl, finished: false, -- cgit v1.2.3