@aioha/tx-digest 1.0.0
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/.prettierrc.yml +4 -0
- package/.vscode/settings.json +15 -0
- package/LICENSE +21 -0
- package/helpers/Asset.js +69 -0
- package/helpers/ByteBuffer.js +3401 -0
- package/helpers/HexBuffer.js +29 -0
- package/helpers/PublicKey.js +36 -0
- package/helpers/serializer.js +634 -0
- package/helpers/uint8Array.js +13 -0
- package/index.d.ts +7 -0
- package/index.js +26 -0
- package/package.json +23 -0
package/.prettierrc.yml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"editor.formatOnSave": true,
|
|
3
|
+
"prettier.enable": true,
|
|
4
|
+
"editor.insertSpaces": true,
|
|
5
|
+
"editor.tabSize": 2,
|
|
6
|
+
"[typescript]": {
|
|
7
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
8
|
+
},
|
|
9
|
+
"[javascript]": {
|
|
10
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
11
|
+
},
|
|
12
|
+
"[html]": {
|
|
13
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
14
|
+
}
|
|
15
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Mahdi Yari
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/helpers/Asset.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** Class representing a hive asset,
|
|
2
|
+
* e.g. `1.000 HIVE` or `12.112233 VESTS`. */
|
|
3
|
+
export class Asset {
|
|
4
|
+
/** Create a new Asset instance from a string, e.g. `42.000 HIVE`. */
|
|
5
|
+
static fromString(string, expectedSymbol = null) {
|
|
6
|
+
const [amountString, symbol] = string.split(' ')
|
|
7
|
+
if (['STEEM', 'VESTS', 'SBD', 'TESTS', 'TBD', 'HIVE', 'HBD'].indexOf(symbol) === -1) {
|
|
8
|
+
throw new Error(`Invalid asset symbol: ${symbol}`)
|
|
9
|
+
}
|
|
10
|
+
if (expectedSymbol && symbol !== expectedSymbol) {
|
|
11
|
+
throw new Error(`Invalid asset, expected symbol: ${expectedSymbol} got: ${symbol}`)
|
|
12
|
+
}
|
|
13
|
+
const amount = Number.parseFloat(amountString)
|
|
14
|
+
if (!Number.isFinite(amount)) {
|
|
15
|
+
throw new Error(`Invalid asset amount: ${amountString}`)
|
|
16
|
+
}
|
|
17
|
+
return new Asset(amount, symbol)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Convenience to create new Asset.
|
|
22
|
+
* @param symbol Symbol to use when created from number. Will also be used to validate
|
|
23
|
+
* the asset, throws if the passed value has a different symbol than this.
|
|
24
|
+
*/
|
|
25
|
+
static from(value, symbol = null) {
|
|
26
|
+
if (value instanceof Asset) {
|
|
27
|
+
if (symbol && value.symbol !== symbol) {
|
|
28
|
+
throw new Error(`Invalid asset, expected symbol: ${symbol} got: ${value.symbol}`)
|
|
29
|
+
}
|
|
30
|
+
return value
|
|
31
|
+
} else if (typeof value === 'number' && Number.isFinite(value)) {
|
|
32
|
+
return new Asset(value, symbol || 'STEEM')
|
|
33
|
+
} else if (typeof value === 'string') {
|
|
34
|
+
return Asset.fromString(value, symbol)
|
|
35
|
+
} else {
|
|
36
|
+
throw new Error(`Invalid asset '${String(value)}'`)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// We convert HIVE & HBD strings to STEEM & SBD because the serialization should be based on STEEM & SBD
|
|
41
|
+
constructor(amount, symbol) {
|
|
42
|
+
this.amount = amount
|
|
43
|
+
this.symbol = symbol === 'HIVE' ? 'STEEM' : symbol === 'HBD' ? 'SBD' : symbol
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Return asset precision. */
|
|
47
|
+
getPrecision() {
|
|
48
|
+
switch (this.symbol) {
|
|
49
|
+
case 'TESTS':
|
|
50
|
+
case 'TBD':
|
|
51
|
+
case 'STEEM':
|
|
52
|
+
case 'SBD':
|
|
53
|
+
case 'HBD':
|
|
54
|
+
case 'HIVE':
|
|
55
|
+
return 3
|
|
56
|
+
case 'VESTS':
|
|
57
|
+
return 6
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Return a string representation of this asset, e.g. `42.000 HIVE`. */
|
|
62
|
+
toString() {
|
|
63
|
+
return `${this.amount.toFixed(this.getPrecision())} ${this.symbol}`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
toJSON() {
|
|
67
|
+
return this.toString()
|
|
68
|
+
}
|
|
69
|
+
}
|