This repository was archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.ts
120 lines (100 loc) · 2.97 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import BN = require('bn.js')
const stripHexPrefix = require('strip-hex-prefix')
const isHexPrefixed = require('is-hex-prefixed')
export type Endianness = 'le' | 'be'
/**
* Convenience class for creating FixedWidthBN values
* with the same width.
*/
export class Factory {
maxWidth: number
minWidth: number
constructor(maxWidth: number, minWidth: number = 0) {
this.maxWidth = maxWidth
this.minWidth = minWidth
}
fromNumber(value: number): FixWidth {
return new FixWidth(value, this.maxWidth, this.minWidth)
}
fromBN(value: BN): FixWidth {
return new FixWidth(value, this.maxWidth, this.minWidth)
}
fromString(value: string): FixWidth {
return new FixWidth(value, this.maxWidth, this.minWidth)
}
fromBuffer(value: Buffer, endian: Endianness = 'be'): FixWidth {
return FixWidth.fromBuffer(value, endian, this.maxWidth, this.minWidth)
}
}
class FixWidth extends BN {
_maxWidth: number
_minWidth: number
constructor(value: any, maxWidth: number, minWidth: number = 0) {
// bn.js still doesn't support hex prefixes...
if (typeof value === 'string' && isHexPrefixed(value)) {
super(stripHexPrefix(value), 16)
} else {
super(value, 10)
}
if (this.byteLength() * 8 > maxWidth) {
throw Error(`number must be less then ${maxWidth} bits`)
}
if (this.byteLength() * 8 < minWidth) {
throw new Error(`number must be more then ${minWidth} bits`)
}
this._maxWidth = maxWidth
this._minWidth = minWidth
}
/**
* converts a buffer to a fixed-bn.js
* @param {string | integer} value
* @param {string} endain
*/
static fromBuffer(value: any, endian: Endianness = 'be', maxWidth: number, minWidth: number = 0) {
const bn = new BN(value, 10, endian)
return new FixWidth(bn, maxWidth, minWidth)
}
/**
* checks if a BN instance is a fixed BN instance
* @param {bn.js} bn
*/
static isFixBN(bn: BN): boolean {
return bn.hasOwnProperty('_minWidth') && bn.hasOwnProperty('_maxWidth')
}
/**
* retuns Max Width
* @returns {integer}
*/
get maxWidth() {
return this._maxWidth
}
/**
* retuns Min Width
* @returns {integer}
*/
get minWidth() {
return this._minWidth
}
// This assumes Uint8Array in LSB (WASM code)
toBuffer(endian: Endianness = 'be') {
return super.toBuffer(endian, this._maxWidth / 8)
}
toArray(endian: Endianness) {
return super.toArray(endian, this._maxWidth / 8)
}
toArrayLike(_type: any, endian: Endianness): any {
return super.toArrayLike(_type, endian, this._maxWidth / 8)
}
/**
* checks if a fixed-bn instance is the same width as the contructor
* @param {bn.js} fixBN
*/
isSameWidth(fixBN: FixWidth): boolean {
return this._minWidth === fixBN.minWidth && this._maxWidth === fixBN.maxWidth
}
}
export const U256 = new Factory(256)
export const U160 = new Factory(160)
export const U128 = new Factory(128)
export const U64 = new Factory(64)
export const Address = new Factory(160, 160)