More typescript.
This commit is contained in:
parent
9d43bd45ec
commit
e92d26a937
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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());
|
||||
|
@ -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());
|
||||
|
@ -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;
|
||||
}
|
||||
|
34
extension/lib/util.ts
Normal file
34
extension/lib/util.ts
Normal file
@ -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;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
},
|
||||
"files": [
|
||||
"background/wallet.ts",
|
||||
"background/emscriptif.ts"
|
||||
"background/emscriptif.ts",
|
||||
"lib/util.ts"
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user