@exodus/bigint 1.1.0 → 2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bigint",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "bigint wrappers for native BigUnt, bn.js and others",
5
5
  "main": "src/index.js",
6
6
  "author": "Exodus Movement, Inc.",
@@ -16,13 +16,9 @@
16
16
  "lodash": "^4.17.11",
17
17
  "minimalistic-assert": "^1.0.1"
18
18
  },
19
- "devDependencies": {
20
- "eslint": "^8.43.0",
21
- "jest": "^29.3.1"
22
- },
23
19
  "scripts": {
24
- "test": "jest",
25
- "lint": "eslint . --ignore-path ../../.gitignore",
20
+ "test": "run -T jest",
21
+ "lint": "run -T eslint . --ignore-path ../../.gitignore",
26
22
  "lint:fix": "yarn lint --fix"
27
23
  },
28
24
  "repository": {
@@ -32,5 +28,5 @@
32
28
  "bugs": {
33
29
  "url": "https://github.com/ExodusMovement/exodus-core/issues?q=is%3Aissue+is%3Aopen+label%3Abigint"
34
30
  },
35
- "gitHead": "e5bfe616c692bc9f8f28af812777646895306a76"
31
+ "gitHead": "8e7de47c76a1bfcb1e29a7663a445ee8cf7268c7"
36
32
  }
package/src/bn.js CHANGED
@@ -8,40 +8,34 @@ const FACTORY_SYMBOL = Symbol('bn-wrapper')
8
8
 
9
9
  const unwrap = (value) => (value instanceof Wrapper ? value.unwrap() : value)
10
10
 
11
+ const BI_ZERO = new BN(0)
12
+
11
13
  export default class Wrapper {
12
14
  static name = 'bn.js'
13
15
  static isUnderlyingInstance(a) {
14
16
  return BN.isBN(a)
15
17
  }
16
18
 
17
- static wrap(value, base) {
18
- return new Wrapper(FACTORY_SYMBOL, value, base)
19
- }
19
+ static wrap(value, base = 10) {
20
+ if (!BN.isBN(value)) {
21
+ if (typeof value !== 'string' && typeof value !== 'number') {
22
+ throw new TypeError(`Unsupported type: ${typeof value}`)
23
+ }
20
24
 
21
- static get ZERO() {
22
- return ZERO
23
- }
25
+ if (base !== 10 && base !== 16) throw new TypeError(`Unsupported base: ${base}`)
24
26
 
25
- static get TEN() {
26
- return TEN
27
+ value = new BN(value, base)
28
+ }
29
+
30
+ return new Wrapper(FACTORY_SYMBOL, value)
27
31
  }
28
32
 
29
33
  __value__
30
34
 
31
- constructor(factorySymbol, value, base = 10) {
35
+ constructor(factorySymbol, value) {
32
36
  if (factorySymbol !== FACTORY_SYMBOL) throw new Error('use wrap() instead')
33
37
 
34
- if (BN.isBN(value)) {
35
- this.__value__ = value
36
- } else {
37
- if (typeof value !== 'string' && typeof value !== 'number') {
38
- throw new TypeError(`Unsupported type: ${typeof value}`)
39
- }
40
-
41
- if (base !== 10 && base !== 16) throw new TypeError(`Unsupported base: ${base}`)
42
-
43
- this.__value__ = new BN(value, base)
44
- }
38
+ this.__value__ = value
45
39
  }
46
40
 
47
41
  unwrap() {
@@ -138,7 +132,7 @@ export default class Wrapper {
138
132
  }
139
133
 
140
134
  isPositive() {
141
- return this.__value__.gt(ZERO.unwrap())
135
+ return this.__value__.gt(BI_ZERO)
142
136
  }
143
137
 
144
138
  toNumber() {
@@ -157,7 +151,8 @@ export default class Wrapper {
157
151
  toBaseBufferBE(length) {
158
152
  return this.__value__.toBuffer('be', length)
159
153
  }
160
- }
161
154
 
162
- const ZERO = new Wrapper(FACTORY_SYMBOL, 0)
163
- const TEN = new Wrapper(FACTORY_SYMBOL, 10)
155
+ isBigIntWrapper() {
156
+ return true
157
+ }
158
+ }
@@ -1,17 +1,13 @@
1
1
  // TODO: calculator delegator should wrap/unwrap all BigInt instances
2
2
  // so that callers never gett a raw BigInt instance back
3
3
 
4
- import { memoize } from 'lodash'
5
4
  import assert from 'minimalistic-assert'
6
5
 
7
6
  const FACTORY_SYMBOL = Symbol('bigint-wrapper')
8
7
 
9
8
  const unwrap = (value) => (value instanceof Wrapper ? value.unwrap() : value)
10
9
 
11
- const memoizedWrap = memoize((value) => Wrapper.wrap(value))
12
-
13
10
  const BI_ZERO = BigInt(0)
14
- const BI_TEN = BigInt(10)
15
11
 
16
12
  export default class Wrapper {
17
13
  static name = 'native-bigint'
@@ -35,14 +31,6 @@ export default class Wrapper {
35
31
  return typeof value === 'bigint'
36
32
  }
37
33
 
38
- static get ZERO() {
39
- return memoizedWrap(BI_ZERO)
40
- }
41
-
42
- static get TEN() {
43
- return memoizedWrap(BI_TEN)
44
- }
45
-
46
34
  __value__
47
35
 
48
36
  constructor(factorySymbol, value) {
@@ -173,4 +161,8 @@ export default class Wrapper {
173
161
  const hex = this.__value__.toString(16)
174
162
  return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
175
163
  }
164
+
165
+ isBigIntWrapper() {
166
+ return true
167
+ }
176
168
  }