From ded02bf11f31c7b9d9cf8b2c863485a491b4c0ae Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 15 Nov 2016 15:07:17 +0100 Subject: renaming / dce --- src/wallet.ts | 146 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 71 insertions(+), 75 deletions(-) (limited to 'src/wallet.ts') diff --git a/src/wallet.ts b/src/wallet.ts index 49b0f1135..4388c61c0 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -25,17 +25,17 @@ import { AmountJson, Amounts, CheckRepurchaseResult, - Coin, + CoinRecord, CoinPaySig, Contract, CreateReserveResponse, Denomination, ExchangeHandle, - IExchangeInfo, + ExchangeRecord, Notifier, PayCoinInfo, - PreCoin, - RefreshSession, + PreCoinRecord, + RefreshSessionRecord, ReserveCreationInfo, ReserveRecord, WalletBalance, @@ -68,7 +68,7 @@ import {CryptoApi} from "./cryptoApi"; "use strict"; export interface CoinWithDenom { - coin: Coin; + coin: CoinRecord; denom: Denomination; } @@ -132,7 +132,7 @@ export class ConfirmReserveRequest { @Checkable.Class -export class Offer { +export class OfferRecord { @Checkable.Value(Contract) contract: Contract; @@ -151,7 +151,7 @@ export class Offer { @Checkable.Optional(Checkable.Number) id?: number; - static checked: (obj: any) => Offer; + static checked: (obj: any) => OfferRecord; } export interface HistoryRecord { @@ -163,10 +163,6 @@ export interface HistoryRecord { } -interface ExchangeCoins { - [exchangeUrl: string]: CoinWithDenom[]; -} - interface PayReq { amount: AmountJson; coins: CoinPaySig[]; @@ -185,7 +181,7 @@ interface PayReq { instance?: string; } -interface Transaction { +interface TransactionRecord { contractHash: string; contract: Contract; payReq: PayReq; @@ -303,20 +299,20 @@ function getWithdrawDenomList(amountAvailable: AmountJson, export namespace Stores { - class ExchangeStore extends Store { + class ExchangeStore extends Store { constructor() { super("exchanges", {keyPath: "baseUrl"}); } - pubKeyIndex = new Index(this, "pubKey", "masterPublicKey"); + pubKeyIndex = new Index(this, "pubKey", "masterPublicKey"); } - class CoinsStore extends Store { + class CoinsStore extends Store { constructor() { super("coins", {keyPath: "coinPub"}); } - exchangeBaseUrlIndex = new Index(this, "exchangeBaseUrl", "exchangeBaseUrl"); + exchangeBaseUrlIndex = new Index(this, "exchangeBaseUrl", "exchangeBaseUrl"); } class HistoryStore extends Store { @@ -330,7 +326,7 @@ export namespace Stores { timestampIndex = new Index(this, "timestamp", "timestamp"); } - class OffersStore extends Store { + class OffersStore extends Store { constructor() { super("offers", { keyPath: "id", @@ -339,12 +335,12 @@ export namespace Stores { } } - class TransactionsStore extends Store { + class TransactionsStore extends Store { constructor() { super("transactions", {keyPath: "contractHash"}); } - repurchaseIndex = new Index<[string,string],Transaction>(this, "repurchase", [ + repurchaseIndex = new Index<[string,string],TransactionRecord>(this, "repurchase", [ "contract.merchant_pub", "contract.repurchase_correlation_id" ]); @@ -354,10 +350,10 @@ export namespace Stores { export let transactions: TransactionsStore = new TransactionsStore(); export let reserves: Store = new Store("reserves", {keyPath: "reserve_pub"}); export let coins: CoinsStore = new CoinsStore(); - export let refresh: Store = new Store("refresh", {keyPath: "meltCoinPub"}); + export let refresh: Store = new Store("refresh", {keyPath: "meltCoinPub"}); export let history: HistoryStore = new HistoryStore(); export let offers: OffersStore = new OffersStore(); - export let precoins: Store = new Store("precoins", {keyPath: "coinPub"}); + export let precoins: Store = new Store("precoins", {keyPath: "coinPub"}); } @@ -442,14 +438,14 @@ export class Wallet { this.q() .iter(Stores.refresh) - .reduce((r: RefreshSession) => { + .reduce((r: RefreshSessionRecord) => { this.continueRefreshSession(r); }); // FIXME: optimize via index this.q() .iter(Stores.coins) - .reduce((c: Coin) => { + .reduce((c: CoinRecord) => { if (c.dirty && !c.transactionPending) { this.refresh(c.coinPub); } @@ -471,7 +467,7 @@ export class Wallet { console.error("db inconsistent"); continue; } - let coins: Coin[] = await this.q().iterIndex(Stores.coins.exchangeBaseUrlIndex, exchangeHandle.url).toArray(); + let coins: CoinRecord[] = await this.q().iterIndex(Stores.coins.exchangeBaseUrlIndex, exchangeHandle.url).toArray(); if (!coins || coins.length == 0) { continue; } @@ -515,7 +511,7 @@ export class Wallet { * Record all information that is necessary to * pay for a contract in the wallet's database. */ - private async recordConfirmPay(offer: Offer, + private async recordConfirmPay(offer: OfferRecord, payCoinInfo: PayCoinInfo, chosenExchange: string): Promise { let payReq: PayReq = { @@ -531,7 +527,7 @@ export class Wallet { transaction_id: offer.contract.transaction_id, instance: offer.contract.merchant.instance }; - let t: Transaction = { + let t: TransactionRecord = { contractHash: offer.H_contract, contract: offer.contract, payReq: payReq, @@ -568,7 +564,7 @@ export class Wallet { } - async saveOffer(offer: Offer): Promise { + async saveOffer(offer: OfferRecord): Promise { console.log(`saving offer in wallet.ts`); let id = await this.q().putWithResult(Stores.offers, offer); this.notifier.notify(); @@ -584,7 +580,7 @@ export class Wallet { * Add a contract to the wallet and sign coins, * but do not send them yet. */ - async confirmPay(offer: Offer): Promise { + async confirmPay(offer: OfferRecord): Promise { console.log("executing confirmPay"); let transaction = await this.q().get(Stores.transactions, offer.H_contract); @@ -618,7 +614,7 @@ export class Wallet { * Add a contract to the wallet and sign coins, * but do not send them yet. */ - async checkPay(offer: Offer): Promise { + async checkPay(offer: OfferRecord): Promise { // First check if we already payed for it. let transaction = await this.q().get(Stores.transactions, offer.H_contract); if (transaction) { @@ -645,7 +641,7 @@ export class Wallet { * with the given hash. */ async executePayment(H_contract: string): Promise { - let t = await this.q().get(Stores.transactions, H_contract); + let t = await this.q().get(Stores.transactions, H_contract); if (!t) { return { success: false, @@ -704,7 +700,7 @@ export class Wallet { } - private async processPreCoin(preCoin: PreCoin, + private async processPreCoin(preCoin: PreCoinRecord, retryDelayMs = 100): Promise { let exchange = await this.q().get(Stores.exchanges, @@ -852,7 +848,7 @@ export class Wallet { } - private async withdrawExecute(pc: PreCoin): Promise { + private async withdrawExecute(pc: PreCoinRecord): Promise { let reserve = await this.q().get(Stores.reserves, pc.reservePub); @@ -879,7 +875,7 @@ export class Wallet { let denomSig = await this.cryptoApi.rsaUnblind(r.ev_sig, pc.blindingKey, pc.denomPub); - let coin: Coin = { + let coin: CoinRecord = { coinPub: pc.coinPub, coinPriv: pc.coinPriv, denomPub: pc.denomPub, @@ -897,7 +893,7 @@ export class Wallet { * Withdraw coins from a reserve until it is empty. */ private async depleteReserve(reserve: ReserveRecord, - exchange: IExchangeInfo): Promise { + exchange: ExchangeRecord): Promise { if (!reserve.current_amount) { throw Error("can't withdraw when amount is unknown"); } @@ -947,7 +943,7 @@ export class Wallet { * by quering the reserve's exchange. */ private async updateReserve(reservePub: string, - exchange: IExchangeInfo): Promise { + exchange: ExchangeRecord): Promise { let reserve = await this.q() .get(Stores.reserves, reservePub); if (!reserve) { @@ -1037,7 +1033,7 @@ export class Wallet { * Optionally link the reserve entry to the new or existing * exchange entry in then DB. */ - async updateExchangeFromUrl(baseUrl: string): Promise { + async updateExchangeFromUrl(baseUrl: string): Promise { baseUrl = canonicalizeBaseUrl(baseUrl); let reqUrl = URI("keys").absoluteTo(baseUrl); let resp = await this.http.get(reqUrl); @@ -1048,11 +1044,11 @@ export class Wallet { return this.updateExchangeFromJson(baseUrl, exchangeKeysJson); } - private async suspendCoins(exchangeInfo: IExchangeInfo): Promise { + private async suspendCoins(exchangeInfo: ExchangeRecord): Promise { let suspendedCoins = await ( this.q() .iterIndex(Stores.coins.exchangeBaseUrlIndex, exchangeInfo.baseUrl) - .reduce((coin: Coin, suspendedCoins: Coin[]) => { + .reduce((coin: CoinRecord, suspendedCoins: CoinRecord[]) => { if (!exchangeInfo.active_denoms.find((c) => c.denom_pub == coin.denomPub)) { return Array.prototype.concat(suspendedCoins, [coin]); } @@ -1070,15 +1066,15 @@ export class Wallet { private async updateExchangeFromJson(baseUrl: string, - exchangeKeysJson: KeysJson): Promise { + exchangeKeysJson: KeysJson): Promise { const updateTimeSec = getTalerStampSec(exchangeKeysJson.list_issue_date); if (updateTimeSec === null) { throw Error("invalid update time"); } - let r = await this.q().get(Stores.exchanges, baseUrl); + let r = await this.q().get(Stores.exchanges, baseUrl); - let exchangeInfo: IExchangeInfo; + let exchangeInfo: ExchangeRecord; if (!r) { exchangeInfo = { @@ -1110,8 +1106,8 @@ export class Wallet { } - private async updateExchangeInfo(exchangeInfo: IExchangeInfo, - newKeys: KeysJson): Promise { + private async updateExchangeInfo(exchangeInfo: ExchangeRecord, + newKeys: KeysJson): Promise { if (exchangeInfo.masterPublicKey != newKeys.master_public_key) { throw Error("public keys do not match"); } @@ -1186,7 +1182,7 @@ export class Wallet { return entry; } - function collectBalances(c: Coin, balance: WalletBalance) { + function collectBalances(c: CoinRecord, balance: WalletBalance) { if (c.suspended) { return balance; } @@ -1213,7 +1209,7 @@ export class Wallet { return balance; } - function collectPendingRefresh(r: RefreshSession, balance: WalletBalance) { + function collectPendingRefresh(r: RefreshSessionRecord, balance: WalletBalance) { if (!r.finished) { return balance; } @@ -1224,7 +1220,7 @@ export class Wallet { return balance; } - function collectPayments(t: Transaction, balance: WalletBalance) { + function collectPayments(t: TransactionRecord, balance: WalletBalance) { if (t.finished) { return balance; } @@ -1235,7 +1231,7 @@ export class Wallet { return balance; } - function collectSmallestWithdraw(e: IExchangeInfo, sw: any) { + function collectSmallestWithdraw(e: ExchangeRecord, sw: any) { let min: AmountJson|undefined; for (let d of e.active_denoms) { let v = Amounts.add(d.value, d.fee_withdraw).amount; @@ -1277,8 +1273,8 @@ export class Wallet { } - async createRefreshSession(oldCoinPub: string): Promise { - let coin = await this.q().get(Stores.coins, oldCoinPub); + async createRefreshSession(oldCoinPub: string): Promise { + let coin = await this.q().get(Stores.coins, oldCoinPub); if (!coin) { throw Error("coin not found"); @@ -1312,14 +1308,14 @@ export class Wallet { } - let refreshSession: RefreshSession = await ( + let refreshSession: RefreshSessionRecord = await ( this.cryptoApi.createRefreshSession(exchange.baseUrl, 3, coin, newCoinDenoms, oldDenom.fee_refresh)); - function mutateCoin(c: Coin): Coin { + function mutateCoin(c: CoinRecord): CoinRecord { let r = Amounts.sub(c.currentAmount, refreshSession.valueWithFee); if (r.saturated) { @@ -1340,7 +1336,7 @@ export class Wallet { async refresh(oldCoinPub: string): Promise { - let refreshSession: RefreshSession|undefined; + let refreshSession: RefreshSessionRecord|undefined; let oldSession = await this.q().get(Stores.refresh, oldCoinPub); if (oldSession) { refreshSession = oldSession; @@ -1354,14 +1350,14 @@ export class Wallet { this.continueRefreshSession(refreshSession); } - async continueRefreshSession(refreshSession: RefreshSession) { + async continueRefreshSession(refreshSession: RefreshSessionRecord) { if (refreshSession.finished) { return; } if (typeof refreshSession.norevealIndex !== "number") { let coinPub = refreshSession.meltCoinPub; await this.refreshMelt(refreshSession); - let r = await this.q().get(Stores.refresh, coinPub); + let r = await this.q().get(Stores.refresh, coinPub); if (!r) { throw Error("refresh session does not exist anymore"); } @@ -1372,14 +1368,14 @@ export class Wallet { } - async refreshMelt(refreshSession: RefreshSession): Promise { + async refreshMelt(refreshSession: RefreshSessionRecord): Promise { if (refreshSession.norevealIndex != undefined) { console.error("won't melt again"); return; } - let coin = await this.q().get(Stores.coins, - refreshSession.meltCoinPub); + let coin = await this.q().get(Stores.coins, + refreshSession.meltCoinPub); if (!coin) { console.error("can't melt coin, it does not exist"); return; @@ -1429,7 +1425,7 @@ export class Wallet { } - async refreshReveal(refreshSession: RefreshSession): Promise { + async refreshReveal(refreshSession: RefreshSessionRecord): Promise { let norevealIndex = refreshSession.norevealIndex; if (norevealIndex == undefined) { throw Error("can't reveal without melting first"); @@ -1461,14 +1457,14 @@ export class Wallet { console.log("/refresh/reveal did not contain ev_sigs"); } - let exchange = await this.q().get(Stores.exchanges, - refreshSession.exchangeBaseUrl); + let exchange = await this.q().get(Stores.exchanges, + refreshSession.exchangeBaseUrl); if (!exchange) { console.error(`exchange ${refreshSession.exchangeBaseUrl} not found`); return; } - let coins: Coin[] = []; + let coins: CoinRecord[] = []; for (let i = 0; i < respJson.ev_sigs.length; i++) { let denom = exchange.all_denoms.find((d) => d.denom_pub == refreshSession.newDenoms[i]); @@ -1480,7 +1476,7 @@ export class Wallet { let denomSig = await this.cryptoApi.rsaUnblind(respJson.ev_sigs[i].ev_sig, pc.blindingKey, denom.denom_pub); - let coin: Coin = { + let coin: CoinRecord = { coinPub: pc.publicKey, coinPriv: pc.privateKey, denomPub: denom.denom_pub, @@ -1526,9 +1522,9 @@ export class Wallet { return offer; } - async getExchanges(): Promise { + async getExchanges(): Promise { return this.q() - .iter(Stores.exchanges) + .iter(Stores.exchanges) .flatMap((e) => [e]) .toArray(); } @@ -1540,17 +1536,17 @@ export class Wallet { .toArray(); } - async getCoins(exchangeBaseUrl: string): Promise { + async getCoins(exchangeBaseUrl: string): Promise { return this.q() - .iter(Stores.coins) - .filter((c: Coin) => c.exchangeBaseUrl === exchangeBaseUrl) + .iter(Stores.coins) + .filter((c: CoinRecord) => c.exchangeBaseUrl === exchangeBaseUrl) .toArray(); } - async getPreCoins(exchangeBaseUrl: string): Promise { + async getPreCoins(exchangeBaseUrl: string): Promise { return this.q() - .iter(Stores.precoins) - .filter((c: PreCoin) => c.exchangeBaseUrl === exchangeBaseUrl) + .iter(Stores.precoins) + .filter((c: PreCoinRecord) => c.exchangeBaseUrl === exchangeBaseUrl) .toArray(); } @@ -1566,7 +1562,7 @@ export class Wallet { console.log("no repurchase: no correlation id"); return {isRepurchase: false}; } - let result: Transaction|undefined = await ( + let result: TransactionRecord|undefined = await ( this.q() .getIndexed(Stores.transactions.repurchaseIndex, [ @@ -1589,16 +1585,16 @@ export class Wallet { async paymentSucceeded(contractHash: string): Promise { const doPaymentSucceeded = async() => { - let t = await this.q().get(Stores.transactions, - contractHash); + let t = await this.q().get(Stores.transactions, + contractHash); if (!t) { console.error("contract not found"); return; } t.finished = true; - let modifiedCoins: Coin[] = []; + let modifiedCoins: CoinRecord[] = []; for (let pc of t.payReq.coins) { - let c = await this.q().get(Stores.coins, pc.coin_pub); + let c = await this.q().get(Stores.coins, pc.coin_pub); if (!c) { console.error("coin not found"); return; -- cgit v1.2.3