Files: d3a050a5491cb0c8f228c86cf23e8cf4725e2038 / u256.js
1004 bytesRaw
1 | const BN = require('bn.js') |
2 | const ethUtils = require('ethereumjs-util') |
3 | |
4 | module.exports = class U256 { |
5 | constructor (value) { |
6 | if ((typeof value === 'string') && ethUtils.isHexPrefixed(value)) { |
7 | this._value = new BN(value, 16) |
8 | } else { |
9 | this._value = new BN(value, 10) |
10 | } |
11 | } |
12 | |
13 | toString (radix = 10) { |
14 | if (radix === 16) { |
15 | return '0x' + this._value.toString(16) |
16 | } |
17 | return this._value.toString(radix) |
18 | } |
19 | |
20 | toBuffer (width) { |
21 | if (width <= 0 || width > 32) { |
22 | throw new Error('Invalid U256 width') |
23 | } |
24 | return this._value.toBuffer('le', width || 32) |
25 | } |
26 | |
27 | sub (u256) { |
28 | return new U256(this._value.sub(u256._value)) |
29 | } |
30 | |
31 | add (u256) { |
32 | return new U256(this._value.add(u256._value)) |
33 | } |
34 | |
35 | mul (u256) { |
36 | return new U256(this._value.mul(u256._value)) |
37 | } |
38 | |
39 | div (u256) { |
40 | return new U256(this._value.div(u256._value)) |
41 | } |
42 | |
43 | lt (u256) { |
44 | return this._value.lt(u256._value) |
45 | } |
46 | |
47 | gt (u256) { |
48 | return this._value.gt(u256._value) |
49 | } |
50 | } |
51 |
Built with git-ssb-web