diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-14 02:13:06 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-14 02:13:06 +0200 |
commit | ab538922312a37da5ee302e34fb72af7a0f8bae7 (patch) | |
tree | b632cbc674d81f9057820098a7ad2996d1203b13 /lib/wallet/wallet.ts | |
parent | 0b198e08888830890622e983445c75f947186b4c (diff) | |
download | wallet-core-ab538922312a37da5ee302e34fb72af7a0f8bae7.tar.xz |
working refresh prototype
Diffstat (limited to 'lib/wallet/wallet.ts')
-rw-r--r-- | lib/wallet/wallet.ts | 72 |
1 files changed, 67 insertions, 5 deletions
diff --git a/lib/wallet/wallet.ts b/lib/wallet/wallet.ts index 264eef250..43f4227dd 100644 --- a/lib/wallet/wallet.ts +++ b/lib/wallet/wallet.ts @@ -1121,27 +1121,89 @@ export class Wallet { let availableDenoms: Denomination[] = exchange.active_denoms; - let newCoinDenoms = getWithdrawDenomList(coin.currentAmount, + let availableAmount = Amounts.sub(coin.currentAmount, + oldDenom.fee_refresh).amount; + + let newCoinDenoms = getWithdrawDenomList(availableAmount, availableDenoms); + newCoinDenoms = [newCoinDenoms[0]]; console.log("refreshing into", newCoinDenoms); let refreshSession: RefreshSession = await ( - this.cryptoApi.createWithdrawSession(3, + this.cryptoApi.createRefreshSession(exchange.baseUrl, + 3, coin, newCoinDenoms, - coin.currentAmount, oldDenom.fee_refresh)); - let reqUrl = URI("reserve/withdraw").absoluteTo(exchange!.baseUrl); - let resp = await this.http.postJson(reqUrl, {}); + let reqUrl = URI("refresh/melt").absoluteTo(exchange!.baseUrl); + let meltCoin = { + coin_pub: coin.coinPub, + denom_pub: coin.denomPub, + denom_sig: coin.denomSig, + confirm_sig: refreshSession.confirmSig, + value_with_fee: refreshSession.valueWithFee, + }; + let coinEvs = refreshSession.preCoinsForGammas.map((x) => x.map((y) => y.coinEv)); + let req = { + "new_denoms": newCoinDenoms.map((d) => d.denom_pub), + "melt_coin": meltCoin, + "transfer_pubs": refreshSession.transferPubs, + "coin_evs": coinEvs, + }; + console.log("melt request:", req); + let resp = await this.http.postJson(reqUrl, req); + console.log("melt request:", req); console.log("melt response:", resp.responseText); + if (resp.status != 200) { + console.error(resp.responseText); + throw Error("refresh failed"); + } + + let respJson = JSON.parse(resp.responseText); + + if (!respJson) { + throw Error("exchange responded with garbage"); + } + + let norevealIndex = respJson.noreveal_index; + + if (typeof norevealIndex != "number") { + throw Error("invalid response"); + } + + refreshSession.norevealIndex = norevealIndex; + + this.refreshReveal(refreshSession); + // FIXME: implement rest } + async refreshReveal(refreshSession: RefreshSession): Promise<void> { + let norevealIndex = refreshSession.norevealIndex; + if (norevealIndex == undefined) { + throw Error("can't reveal without melting first"); + } + let privs = Array.from(refreshSession.transferPrivs); + privs.splice(norevealIndex, 1); + + let req = { + "session_hash": refreshSession.hash, + "transfer_privs": privs, + }; + + let reqUrl = URI("refresh/reveal").absoluteTo(refreshSession.exchangeBaseUrl); + console.log("reveal request:", req); + let resp = await this.http.postJson(reqUrl, req); + + console.log("session:", refreshSession); + console.log("reveal response:", resp); + } + /** * Retrive the full event history for this wallet. |