hive-keychain-commons 1.5.0 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utils/assets.utils.js +32 -20
- package/lib/utils/crypto.utils.js +3 -3
- package/package.json +1 -1
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Price = exports.Asset = void 0;
|
|
7
|
-
const assert_1 = __importDefault(require("assert"));
|
|
8
4
|
class Asset {
|
|
9
5
|
constructor(amount, symbol) {
|
|
10
6
|
/**
|
|
@@ -34,7 +30,8 @@ class Asset {
|
|
|
34
30
|
*/
|
|
35
31
|
this.add = (amount) => {
|
|
36
32
|
const other = Asset.from(amount, this.symbol);
|
|
37
|
-
|
|
33
|
+
if (this.symbol !== other.symbol)
|
|
34
|
+
throw new Error(`can not add with different symbols`);
|
|
38
35
|
return new Asset(this.amount + other.amount, this.symbol);
|
|
39
36
|
};
|
|
40
37
|
/**
|
|
@@ -42,7 +39,8 @@ class Asset {
|
|
|
42
39
|
*/
|
|
43
40
|
this.subtract = (amount) => {
|
|
44
41
|
const other = Asset.from(amount, this.symbol);
|
|
45
|
-
|
|
42
|
+
if (this.symbol !== other.symbol)
|
|
43
|
+
throw new Error(`can not subtract with different symbols`);
|
|
46
44
|
return new Asset(this.amount - other.amount, this.symbol);
|
|
47
45
|
};
|
|
48
46
|
/**
|
|
@@ -50,7 +48,9 @@ class Asset {
|
|
|
50
48
|
*/
|
|
51
49
|
this.multiply = (factor) => {
|
|
52
50
|
const other = Asset.from(factor, this.symbol);
|
|
53
|
-
|
|
51
|
+
if (this.symbol !== other.symbol) {
|
|
52
|
+
throw new Error(`can not multiply with different symbols`);
|
|
53
|
+
}
|
|
54
54
|
return new Asset(this.amount * other.amount, this.symbol);
|
|
55
55
|
};
|
|
56
56
|
/**
|
|
@@ -58,7 +58,9 @@ class Asset {
|
|
|
58
58
|
*/
|
|
59
59
|
this.divide = (divisor) => {
|
|
60
60
|
const other = Asset.from(divisor, this.symbol);
|
|
61
|
-
|
|
61
|
+
if (this.symbol !== other.symbol) {
|
|
62
|
+
throw new Error(`can not divide with different symbols`);
|
|
63
|
+
}
|
|
62
64
|
return new Asset(this.amount / other.amount, this.symbol);
|
|
63
65
|
};
|
|
64
66
|
/**
|
|
@@ -80,14 +82,14 @@ Asset.fromString = (str, expectedSymbol) => {
|
|
|
80
82
|
const amountString = _a[0];
|
|
81
83
|
const symbol = _a[1];
|
|
82
84
|
if (!['HIVE', 'VESTS', 'HBD', 'TESTS', 'TBD', 'SBD', 'STEEM'].includes(symbol)) {
|
|
83
|
-
throw new Error(
|
|
85
|
+
throw new Error(`Invalid asset symbol: ${symbol}`);
|
|
84
86
|
}
|
|
85
87
|
if (expectedSymbol && symbol !== expectedSymbol) {
|
|
86
|
-
throw new Error(
|
|
88
|
+
throw new Error(`Invalid asset, expected symbol: ${expectedSymbol} got: ${symbol}`);
|
|
87
89
|
}
|
|
88
90
|
const amount = Number.parseFloat(amountString);
|
|
89
91
|
if (!Number.isFinite(amount)) {
|
|
90
|
-
throw new Error(
|
|
92
|
+
throw new Error(`Invalid asset amount: ${amountString}`);
|
|
91
93
|
}
|
|
92
94
|
return new Asset(amount, symbol);
|
|
93
95
|
};
|
|
@@ -99,7 +101,7 @@ Asset.fromString = (str, expectedSymbol) => {
|
|
|
99
101
|
Asset.from = (value, symbol) => {
|
|
100
102
|
if (value instanceof Asset) {
|
|
101
103
|
if (symbol && value.symbol !== symbol) {
|
|
102
|
-
throw new Error(
|
|
104
|
+
throw new Error(`Invalid asset, expected symbol: ${symbol} got: ${value.symbol}`);
|
|
103
105
|
}
|
|
104
106
|
return value;
|
|
105
107
|
}
|
|
@@ -110,21 +112,23 @@ Asset.from = (value, symbol) => {
|
|
|
110
112
|
return Asset.fromString(value, symbol);
|
|
111
113
|
}
|
|
112
114
|
else {
|
|
113
|
-
throw new Error(
|
|
115
|
+
throw new Error(`Invalid asset '${String(value)}'`);
|
|
114
116
|
}
|
|
115
117
|
};
|
|
116
118
|
/**
|
|
117
119
|
* Return the smaller of the two assets.
|
|
118
120
|
*/
|
|
119
121
|
Asset.min = (a, b) => {
|
|
120
|
-
|
|
122
|
+
if (a.symbol !== b.symbol)
|
|
123
|
+
throw new Error(`can not compare assets with different symbols`);
|
|
121
124
|
return a.amount < b.amount ? a : b;
|
|
122
125
|
};
|
|
123
126
|
/**
|
|
124
127
|
* Return the larger of the two assets.
|
|
125
128
|
*/
|
|
126
129
|
Asset.max = (a, b) => {
|
|
127
|
-
|
|
130
|
+
if (a.symbol !== b.symbol)
|
|
131
|
+
throw new Error(`can not compare assets with different symbols`);
|
|
128
132
|
return a.amount > b.amount ? a : b;
|
|
129
133
|
};
|
|
130
134
|
class Price {
|
|
@@ -141,21 +145,29 @@ class Price {
|
|
|
141
145
|
*/
|
|
142
146
|
this.convert = (asset) => {
|
|
143
147
|
if (asset.symbol === this.base.symbol) {
|
|
144
|
-
|
|
148
|
+
if (this.base.amount <= 0) {
|
|
149
|
+
throw new Error('Base amount must be greater than zero');
|
|
150
|
+
}
|
|
145
151
|
return new Asset((asset.amount * this.quote.amount) / this.base.amount, this.quote.symbol);
|
|
146
152
|
}
|
|
147
153
|
else if (asset.symbol === this.quote.symbol) {
|
|
148
|
-
|
|
154
|
+
if (this.quote.amount <= 0) {
|
|
155
|
+
throw new Error('Quote amount must be greater than zero');
|
|
156
|
+
}
|
|
149
157
|
return new Asset((asset.amount * this.base.amount) / this.quote.amount, this.base.symbol);
|
|
150
158
|
}
|
|
151
159
|
else {
|
|
152
|
-
throw new Error(
|
|
160
|
+
throw new Error(`Can not convert ${asset} with ${this}`);
|
|
153
161
|
}
|
|
154
162
|
};
|
|
155
163
|
this.base = base;
|
|
156
164
|
this.quote = quote;
|
|
157
|
-
|
|
158
|
-
|
|
165
|
+
if (base.amount === 0 || quote.amount === 0) {
|
|
166
|
+
throw new Error('base and quote assets must be non-zero');
|
|
167
|
+
}
|
|
168
|
+
if (base.symbol === quote.symbol) {
|
|
169
|
+
throw new Error('base and quote can not have the same symbol');
|
|
170
|
+
}
|
|
159
171
|
}
|
|
160
172
|
}
|
|
161
173
|
exports.Price = Price;
|
|
@@ -30,15 +30,15 @@ const sha256 = (input) => {
|
|
|
30
30
|
if (typeof input !== 'string') {
|
|
31
31
|
input = crypto_js_1.lib.WordArray.create(input);
|
|
32
32
|
}
|
|
33
|
-
const hash = Buffer.from((0, crypto_js_1.SHA256)(input).toString(
|
|
33
|
+
const hash = Buffer.from((0, crypto_js_1.SHA256)(input).toString(crypto_js_1.enc.Hex), 'hex');
|
|
34
34
|
return hash;
|
|
35
35
|
};
|
|
36
36
|
const ripemd160 = (input) => {
|
|
37
37
|
// return crypto.createHash('ripemd160').update(input).digest()
|
|
38
38
|
if (typeof input !== 'string') {
|
|
39
|
-
input =
|
|
39
|
+
input = crypto_js_1.lib.WordArray.create(input);
|
|
40
40
|
}
|
|
41
|
-
const hash = Buffer.from((0, crypto_js_1.RIPEMD160)(input).toString(
|
|
41
|
+
const hash = Buffer.from((0, crypto_js_1.RIPEMD160)(input).toString(crypto_js_1.enc.Hex), 'hex');
|
|
42
42
|
return hash;
|
|
43
43
|
};
|
|
44
44
|
const isWif = (privWif) => {
|