diff options
-rw-r--r-- | extension/background/emscriptif.js | 27 | ||||
-rw-r--r-- | extension/background/emscriptif.ts | 31 | ||||
-rw-r--r-- | extension/background/wallet.js | 23 | ||||
-rw-r--r-- | extension/background/wallet.ts | 24 |
4 files changed, 86 insertions, 19 deletions
diff --git a/extension/background/emscriptif.js b/extension/background/emscriptif.js index d1e92df4b..5d8623b3e 100644 --- a/extension/background/emscriptif.js +++ b/extension/background/emscriptif.js @@ -242,6 +242,12 @@ class PackedArenaObject extends ArenaObject { emsc.free(d); return s; } + toJson() { + // Per default, the json encoding of + // packed arena objects is just the crockford encoding. + // Subclasses typically want to override this. + return this.toCrock(); + } loadCrock(s) { this.alloc(); // We need to get the javascript string @@ -286,6 +292,14 @@ class AmountNbo extends PackedArenaObject { size() { return 24; } + toJson() { + let a = new DefaultArena(); + let am = new Amount(null, a); + am.fromNbo(this); + let json = am.toJson(); + a.destroy(); + return json; + } } class EddsaPrivateKey extends PackedArenaObject { static create(a) { @@ -468,6 +482,19 @@ class SignatureStruct { let ba = new ByteArray(totalSize, buf, a); return new EccSignaturePurpose(this.purpose(), ba); } + toJson() { + let res = {}; + for (let f of this.fieldTypes()) { + let name = f[0]; + let member = this.members[name]; + if (!member) { + throw Error(format("Member {0} not set", name)); + } + res[name] = member.toJson(); + } + res["purpose"] = this.purpose(); + return res; + } set(name, value) { let typemap = {}; for (let f of this.fieldTypes()) { diff --git a/extension/background/emscriptif.ts b/extension/background/emscriptif.ts index 73b5d1acd..3daf27b17 100644 --- a/extension/background/emscriptif.ts +++ b/extension/background/emscriptif.ts @@ -387,6 +387,13 @@ abstract class PackedArenaObject extends ArenaObject { return s; } + toJson(): any { + // Per default, the json encoding of + // packed arena objects is just the crockford encoding. + // Subclasses typically want to override this. + return this.toCrock(); + } + loadCrock(s: string) { this.alloc(); // We need to get the javascript string @@ -420,7 +427,6 @@ abstract class PackedArenaObject extends ArenaObject { return x; } - hexdump() { let bytes: string[] = []; for (let i = 0; i < this.size(); i++) { @@ -441,6 +447,14 @@ class AmountNbo extends PackedArenaObject { size() { return 24; } + toJson(): any { + let a = new DefaultArena(); + let am = new Amount(null, a); + am.fromNbo(this); + let json = am.toJson(); + a.destroy(); + return json; + } } @@ -673,6 +687,21 @@ abstract class SignatureStruct { return new EccSignaturePurpose(this.purpose(), ba); } + + toJson() { + let res: any = {}; + for (let f of this.fieldTypes()) { + let name = f[0]; + let member = this.members[name]; + if (!member) { + throw Error(format("Member {0} not set", name)); + } + res[name] = member.toJson(); + } + res["purpose"] = this.purpose(); + return res; + } + protected set(name: string, value: PackedArenaObject) { let typemap: any = {}; for (let f of this.fieldTypes()) { diff --git a/extension/background/wallet.js b/extension/background/wallet.js index 905febf09..f1711b435 100644 --- a/extension/background/wallet.js +++ b/extension/background/wallet.js @@ -34,21 +34,24 @@ function signDeposit(db, offer, cds) { } amountSpent.add(coinSpend); amountRemaining.sub(coinSpend); - let d = new DepositRequestPS({ + let newAmount = new Amount(cd.coin.currentAmount); + newAmount.sub(coinSpend); + cd.coin.currentAmount = newAmount.toJson(); + let args = { h_contract: HashCode.fromCrock(offer.H_contract), h_wire: HashCode.fromCrock(offer.contract.H_wire), - amount_with_fee: new Amount(cd.coin.currentAmount).toNbo(), + amount_with_fee: coinSpend.toNbo(), coin_pub: EddsaPublicKey.fromCrock(cd.coin.coinPub), deposit_fee: new Amount(cd.denom.fee_deposit).toNbo(), merchant: EddsaPublicKey.fromCrock(offer.contract.merchant_pub), refund_deadline: AbsoluteTimeNbo.fromTalerString(offer.contract.refund_deadline), timestamp: AbsoluteTimeNbo.fromTalerString(offer.contract.timestamp), transaction_id: UInt64.fromNumber(offer.contract.transaction_id), - }); - let newAmount = new Amount(cd.coin.currentAmount); - newAmount.sub(coinSpend); - cd.coin.currentAmount = newAmount.toJson(); - console.log("DepositRequestPS: ", d.toPurpose().hexdump()); + }; + let d = new DepositRequestPS(args); + console.log("Deposit request #" + ret.length); + console.log("DepositRequestPS: \n", d.toJson()); + console.log("DepositRequestPS sig: \n", d.toPurpose().hexdump()); let coinSig = eddsaSign(d.toPurpose(), EddsaPrivateKey.fromCrock(cd.coin.coinPriv)) .toCrock(); let s = { @@ -56,7 +59,7 @@ function signDeposit(db, offer, cds) { coin_pub: cd.coin.coinPub, ub_sig: cd.coin.denomSig, denom_pub: cd.coin.denomPub, - f: amountSpent.toJson(), + f: coinSpend.toJson(), }; ret.push({ sig: s, updatedCoin: cd.coin }); } @@ -126,6 +129,7 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints) let minAmount = new Amount(paymentAmount); let accFee = new Amount(coins[0].c.denom.fee_deposit); let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency); + let usableCoins = []; nextCoin: for (let i = 0; i < coins.length; i++) { let coinAmount = new Amount(coins[i].c.coin.currentAmount); let coinFee = coins[i].a; @@ -138,8 +142,9 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints) console.log("too much fees"); continue nextMint; } + usableCoins.push(coins[i].c); if (accAmount.cmp(minAmount) >= 0) { - ret[key] = m[key]; + ret[key] = usableCoins; continue nextMint; } } diff --git a/extension/background/wallet.ts b/extension/background/wallet.ts index d9b3080c1..bf91c3e27 100644 --- a/extension/background/wallet.ts +++ b/extension/background/wallet.ts @@ -97,23 +97,27 @@ function signDeposit(db: IDBDatabase, amountSpent.add(coinSpend); amountRemaining.sub(coinSpend); - let d = new DepositRequestPS({ + let newAmount = new Amount(cd.coin.currentAmount); + newAmount.sub(coinSpend); + cd.coin.currentAmount = newAmount.toJson(); + + let args: DepositRequestPS_Args = { h_contract: HashCode.fromCrock(offer.H_contract), h_wire: HashCode.fromCrock(offer.contract.H_wire), - amount_with_fee: new Amount(cd.coin.currentAmount).toNbo(), + amount_with_fee: coinSpend.toNbo(), coin_pub: EddsaPublicKey.fromCrock(cd.coin.coinPub), deposit_fee: new Amount(cd.denom.fee_deposit).toNbo(), merchant: EddsaPublicKey.fromCrock(offer.contract.merchant_pub), refund_deadline: AbsoluteTimeNbo.fromTalerString(offer.contract.refund_deadline), timestamp: AbsoluteTimeNbo.fromTalerString(offer.contract.timestamp), transaction_id: UInt64.fromNumber(offer.contract.transaction_id), - }); + }; - let newAmount = new Amount(cd.coin.currentAmount); - newAmount.sub(coinSpend); - cd.coin.currentAmount = newAmount.toJson(); + let d = new DepositRequestPS(args); - console.log("DepositRequestPS: ", d.toPurpose().hexdump()); + console.log("Deposit request #" + ret.length); + console.log("DepositRequestPS: \n", d.toJson()); + console.log("DepositRequestPS sig: \n", d.toPurpose().hexdump()); let coinSig = eddsaSign(d.toPurpose(), EddsaPrivateKey.fromCrock(cd.coin.coinPriv)) @@ -124,7 +128,7 @@ function signDeposit(db: IDBDatabase, coin_pub: cd.coin.coinPub, ub_sig: cd.coin.denomSig, denom_pub: cd.coin.denomPub, - f: amountSpent.toJson(), + f: coinSpend.toJson(), }; ret.push({sig: s, updatedCoin: cd.coin}); } @@ -206,6 +210,7 @@ function getPossibleMintCoins(db: IDBDatabase, let minAmount = new Amount(paymentAmount); let accFee = new Amount(coins[0].c.denom.fee_deposit); let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency); + let usableCoins: Db.CoinWithDenom[] = []; nextCoin: for (let i = 0; i < coins.length; i++) { let coinAmount = new Amount(coins[i].c.coin.currentAmount); @@ -219,8 +224,9 @@ function getPossibleMintCoins(db: IDBDatabase, console.log("too much fees"); continue nextMint; } + usableCoins.push(coins[i].c); if (accAmount.cmp(minAmount) >= 0) { - ret[key] = m[key]; + ret[key] = usableCoins; continue nextMint; } } |