git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 1cb861903ccb26cbd39eaad7ab6060463380986e

Files: 1cb861903ccb26cbd39eaad7ab6060463380986e / u256.js

904 bytesRaw
1const BN = require('bn.js')
2const ethUtils = require('ethereumjs-util')
3
4module.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 () {
21 return this._value.toBuffer('be', 32)
22 }
23
24 sub (u256) {
25 return new U256(this._value.sub(u256._value))
26 }
27
28 add (u256) {
29 return new U256(this._value.add(u256._value))
30 }
31
32 mul (u256) {
33 return new U256(this._value.mul(u256._value))
34 }
35
36 div (u256) {
37 return new U256(this._value.div(u256._value))
38 }
39
40 lt (u256) {
41 return this._value.lt(u256._value)
42 }
43
44 gt (u256) {
45 return this._value.gt(u256._value)
46 }
47}
48

Built with git-ssb-web