diff options
author | Florian Dold <florian.dold@gmail.com> | 2015-12-14 11:26:55 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2015-12-14 11:26:55 +0100 |
commit | e92d26a93781820391174e95c484956aa90cf8fb (patch) | |
tree | f500f49973f90cafe87cecc6b00e2527aa4ca316 | |
parent | 9d43bd45ecdaa2874dc2b110f3f1f03019ef1bad (diff) | |
download | wallet-core-e92d26a93781820391174e95c484956aa90cf8fb.tar.xz |
More typescript.
-rw-r--r-- | extension/background/emscriptif.js | 7 | ||||
-rw-r--r-- | extension/background/emscriptif.ts | 8 | ||||
-rw-r--r-- | extension/background/wallet.js | 17 | ||||
-rw-r--r-- | extension/background/wallet.ts | 19 | ||||
-rw-r--r-- | extension/lib/util.js | 122 | ||||
-rw-r--r-- | extension/lib/util.ts | 34 | ||||
-rw-r--r-- | extension/tsconfig.json | 3 |
7 files changed, 88 insertions, 122 deletions
diff --git a/extension/background/emscriptif.js b/extension/background/emscriptif.js index ee864c60d..a4e50b89e 100644 --- a/extension/background/emscriptif.js +++ b/extension/background/emscriptif.js @@ -127,7 +127,8 @@ class Amount extends ArenaObject { * Perform saturating subtraction on amounts. */ sub(a) { - let res = emsc.amount_subtract(this.nativePtr, a.nativePtr, this.nativePtr); + // this = this - a + let res = emsc.amount_subtract(this.nativePtr, this.nativePtr, a.nativePtr); if (res == 0) { // Underflow return false; @@ -135,7 +136,7 @@ class Amount extends ArenaObject { if (res > 0) { return true; } - throw "Incompatible currencies"; + throw Error("Incompatible currencies"); } cmp(a) { return emsc.amount_cmp(this.nativePtr, a.nativePtr); @@ -256,7 +257,7 @@ class SignatureStruct { let name = f[0]; let member = this.members[name]; if (!member) { - throw { error: "Member not set", key: name }; + throw Error(format("Member {0} not set", name)); } totalSize += this.members[name].size(); } diff --git a/extension/background/emscriptif.ts b/extension/background/emscriptif.ts index 5ca4fef24..49ed3c943 100644 --- a/extension/background/emscriptif.ts +++ b/extension/background/emscriptif.ts @@ -19,6 +19,7 @@ declare var Module : any; + // Size of a native pointer. const PTR_SIZE = 4; @@ -202,7 +203,8 @@ class Amount extends ArenaObject { * Perform saturating subtraction on amounts. */ sub(a) { - let res = emsc.amount_subtract(this.nativePtr, a.nativePtr, this.nativePtr); + // this = this - a + let res = emsc.amount_subtract(this.nativePtr, this.nativePtr, a.nativePtr); if (res == 0) { // Underflow return false; @@ -210,7 +212,7 @@ class Amount extends ArenaObject { if (res > 0) { return true; } - throw "Incompatible currencies"; + throw Error("Incompatible currencies"); } cmp(a) { @@ -365,7 +367,7 @@ abstract class SignatureStruct { let name = f[0]; let member = this.members[name]; if (!member) { - throw {error: "Member not set", key: name}; + throw Error(format("Member {0} not set", name)); } totalSize += this.members[name].size(); } diff --git a/extension/background/wallet.js b/extension/background/wallet.js index 3281c0d07..55b22c127 100644 --- a/extension/background/wallet.js +++ b/extension/background/wallet.js @@ -107,8 +107,11 @@ function confirmReserve(db, detail, sendResponse) { function copy(o) { return JSON.parse(JSON.stringify(o)); } -function rankDenom(o1, o2) { - return (-1) * o1.cmp(o2); +function rankDenom(denom1, denom2) { + // Slow ... we should find a better way than to convert it evert time. + let v1 = new Amount(denom1.value); + let v2 = new Amount(denom2.value); + return (-1) * v1.cmp(v2); } function withdraw(denom, reserve, mint) { let wd = { @@ -128,6 +131,7 @@ function withdraw(denom, reserve, mint) { // Signature let withdrawRequest = new WithdrawRequestPS(); withdrawRequest.set("reserve_pub", reservePub); + // ... var sig = eddsaSign(withdrawRequest.toPurpose(), reservePriv); } /** @@ -140,13 +144,14 @@ function depleteReserve(db, reserve, mint) { for (let i = 0; i < 1000; i++) { let found = false; for (let d of denoms) { - let cost = new Amount(); - cost.add(new Amount(d.value)); + let cost = new Amount(d.value); cost.add(new Amount(d.fee_withdraw)); if (remaining.cmp(cost) < 0) { continue; } found = true; + console.log("Subbing " + JSON.stringify(remaining.toJson())); + console.log("With " + JSON.stringify(cost.toJson())); remaining.sub(cost); withdraw(d, reserve, mint); } @@ -159,10 +164,10 @@ function updateReserve(db, reservePub, mint) { let reserve; return new Promise((resolve, reject) => { let tx = db.transaction(['reserves']); - tx.objectStore('reserves').get(reservePub).onsuccess = (e) => { + tx.objectStore('reserves').get(reservePub.stringEncode()).onsuccess = (e) => { let reserve = e.target.result; let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl); - reqUrl.query({ 'reserve_pub': reservePub }); + reqUrl.query({ 'reserve_pub': reservePub.stringEncode() }); let myRequest = new XMLHttpRequest(); console.log("making request to " + reqUrl.href()); myRequest.open('get', reqUrl.href()); diff --git a/extension/background/wallet.ts b/extension/background/wallet.ts index 751087c7c..7f7994ab6 100644 --- a/extension/background/wallet.ts +++ b/extension/background/wallet.ts @@ -118,8 +118,11 @@ function copy(o) { } -function rankDenom(o1: Amount, o2: Amount) { - return (-1) * o1.cmp(o2); +function rankDenom(denom1: any, denom2: any) { + // Slow ... we should find a better way than to convert it evert time. + let v1 = new Amount(denom1.value); + let v2 = new Amount(denom2.value); + return (-1) * v1.cmp(v2); } @@ -141,6 +144,7 @@ function withdraw(denom, reserve, mint) { // Signature let withdrawRequest = new WithdrawRequestPS(); withdrawRequest.set("reserve_pub", reservePub); + // ... var sig = eddsaSign(withdrawRequest.toPurpose(), reservePriv); } @@ -155,13 +159,14 @@ function depleteReserve(db, reserve, mint) { for (let i = 0; i < 1000; i++) { let found = false; for (let d of denoms) { - let cost = new Amount(); - cost.add(new Amount(d.value)); + let cost = new Amount(d.value); cost.add(new Amount(d.fee_withdraw)); if (remaining.cmp(cost) < 0) { continue; } found = true; + console.log("Subbing " + JSON.stringify(remaining.toJson())); + console.log("With " + JSON.stringify(cost.toJson())); remaining.sub(cost); withdraw(d, reserve, mint); } @@ -173,14 +178,14 @@ function depleteReserve(db, reserve, mint) { } -function updateReserve(db, reservePub, mint) { +function updateReserve(db, reservePub: EddsaPublicKey, mint) { let reserve; return new Promise((resolve, reject) => { let tx = db.transaction(['reserves']); - tx.objectStore('reserves').get(reservePub).onsuccess = (e) => { + tx.objectStore('reserves').get(reservePub.stringEncode()).onsuccess = (e) => { let reserve = e.target.result; let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl); - reqUrl.query({'reserve_pub': reservePub}); + reqUrl.query({'reserve_pub': reservePub.stringEncode()}); let myRequest = new XMLHttpRequest(); console.log("making request to " + reqUrl.href()); myRequest.open('get', reqUrl.href()); diff --git a/extension/lib/util.js b/extension/lib/util.js index d364d9593..801d7d189 100644 --- a/extension/lib/util.js +++ b/extension/lib/util.js @@ -1,112 +1,30 @@ 'use strict'; - -/** - * Format amount as String. - * - * @param amount - * Amount to be formatted. - * - * @return String, e.g. "1.23" - */ -function amount_format (amount) -{ - let separator = "." // FIXME: depends on locale - return amount.value + separator + amount.fraction.toString().replace(/0+$/, ""); -} - - /** * Parse an amount that is specified like '5.42 EUR'. * Returns a {currency,value,fraction} object or null * if the input is invalid. */ function amount_parse_pretty(s) { - let pattern = /(\d+)(.\d+)?\s*([a-zA-Z]+)/; - let matches = pattern.exec(s); - if (null == matches) { - return null; - } - return { - // Always succeeds due to regex - value: parseInt(matches[1]), - // Should we warn / fail on lost precision? - fraction: Math.round(parseFloat(matches[2] || 0) * 1000000), - currency: matches[3], - }; -} - - -/** - * Format amount with currency as String. - * - * @param amount - * Amount to be formatted. - * - * @return String, e.g. "1.23 EUR" - */ -function amount_format_currency (amount) -{ - return amount_format(amount) + " " + amount.currency; -} - - -/** - * Convert Date to String. - * - * Format: YYYY-MM-DD HH:mm - * - * @param date - * Date to be converted. - * - * @return String - */ -function date_format (date) -{ - function pad (number) { - if (number < 10) { - return '0' + number; + let pattern = /(\d+)(.\d+)?\s*([a-zA-Z]+)/; + let matches = pattern.exec(s); + if (null == matches) { + return null; } - return number; - } - - return date.getUTCFullYear() + - '-' + pad(date.getUTCMonth() + 1) + - '-' + pad(date.getUTCDate()) + - ' ' + pad(date.getUTCHours()) + - ':' + pad(date.getUTCMinutes()); - //':' + pad(date.getUTCSeconds()); + return { + // Always succeeds due to regex + value: parseInt(matches[1]), + // Should we warn / fail on lost precision? + fraction: Math.round(parseFloat(matches[2] || "0") * 1000000), + currency: matches[3], + }; } - - -/** - * Send HTTP request. - * - * @param method - * HTTP method. - * @param url - * URL to send to. - * @param content - * Content of request. - * @param content_type - * Content-Type HTTP header. - * @param onsuccess - * Function called by XMLHttpRequest on success. - * @param onerror - * Function called by XMLHttpRequest on error. - * - */ -function http_req (method, url, content, content_type, onsuccess, onerror) { - var req = new XMLHttpRequest(); - - req.onload = function(mintEvt) { - if (req.readyState == 4) - onsuccess(req.status, req.responseText); - }; - - req.onerror = onerror; - req.open(method, url, true); - req.setRequestHeader('Content-Type', content_type); - req.send(content); - - return req; +function format(s, ...args) { + function r(m, n) { + let i = parseInt(n); + return args[i]; + } + s = s.replace(/{{/g, '{'); + s = s.replace(/}}/g, '}'); + s = s.replace(/{([0-9]+)}/g, r); + return s; } diff --git a/extension/lib/util.ts b/extension/lib/util.ts new file mode 100644 index 000000000..106e22970 --- /dev/null +++ b/extension/lib/util.ts @@ -0,0 +1,34 @@ +'use strict'; + +/** + * Parse an amount that is specified like '5.42 EUR'. + * Returns a {currency,value,fraction} object or null + * if the input is invalid. + */ +function amount_parse_pretty(s) { + let pattern = /(\d+)(.\d+)?\s*([a-zA-Z]+)/; + let matches = pattern.exec(s); + if (null == matches) { + return null; + } + return { + // Always succeeds due to regex + value: parseInt(matches[1]), + // Should we warn / fail on lost precision? + fraction: Math.round(parseFloat(matches[2] || "0") * 1000000), + currency: matches[3], + }; +} + + +function format(s: string, ...args: any[]) { + function r(m, n) { + let i = parseInt(n); + return args[i]; + } + s = s.replace(/{{/g, '{'); + s = s.replace(/}}/g, '}'); + s = s.replace(/{([0-9]+)}/g, r); + return s; +} + diff --git a/extension/tsconfig.json b/extension/tsconfig.json index bbcb28e43..6c72403bc 100644 --- a/extension/tsconfig.json +++ b/extension/tsconfig.json @@ -4,6 +4,7 @@ }, "files": [ "background/wallet.ts", - "background/emscriptif.ts" + "background/emscriptif.ts", + "lib/util.ts" ] } |