1 | import BN from 'bn.js'
|
2 | import { isHexString } from './internal'
|
3 | import { Address } from './address'
|
4 | import { unpadBuffer, toBuffer, ToBufferInputTypes } from './bytes'
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export type BNLike = BN | PrefixedHexString | number | Buffer
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export type BufferLike =
|
15 | | Buffer
|
16 | | Uint8Array
|
17 | | number[]
|
18 | | number
|
19 | | BN
|
20 | | TransformableToBuffer
|
21 | | PrefixedHexString
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export type PrefixedHexString = string
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export type AddressLike = Address | Buffer | PrefixedHexString
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export interface TransformableToArray {
|
38 | toArray(): Uint8Array
|
39 | toBuffer?(): Buffer
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | export interface TransformableToBuffer {
|
46 | toBuffer(): Buffer
|
47 | toArray?(): Uint8Array
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export function bnToHex(value: BN): PrefixedHexString {
|
54 | return `0x${value.toString(16)}`
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export function bnToUnpaddedBuffer(value: BN): Buffer {
|
63 |
|
64 |
|
65 | return unpadBuffer(value.toArrayLike(Buffer))
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | export function bnToRlp(value: BN): Buffer {
|
73 | return bnToUnpaddedBuffer(value)
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | export enum TypeOutput {
|
80 | Number,
|
81 | BN,
|
82 | Buffer,
|
83 | PrefixedHexString,
|
84 | }
|
85 |
|
86 | export type TypeOutputReturnType = {
|
87 | [TypeOutput.Number]: number
|
88 | [TypeOutput.BN]: BN
|
89 | [TypeOutput.Buffer]: Buffer
|
90 | [TypeOutput.PrefixedHexString]: PrefixedHexString
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | export function toType<T extends TypeOutput>(input: null, outputType: T): null
|
100 | export function toType<T extends TypeOutput>(input: undefined, outputType: T): undefined
|
101 | export function toType<T extends TypeOutput>(
|
102 | input: ToBufferInputTypes,
|
103 | outputType: T
|
104 | ): TypeOutputReturnType[T]
|
105 | export function toType<T extends TypeOutput>(
|
106 | input: ToBufferInputTypes,
|
107 | outputType: T
|
108 | ): TypeOutputReturnType[T] | undefined | null {
|
109 | if (input === null) {
|
110 | return null
|
111 | }
|
112 | if (input === undefined) {
|
113 | return undefined
|
114 | }
|
115 |
|
116 | if (typeof input === 'string' && !isHexString(input)) {
|
117 | throw new Error(`A string must be provided with a 0x-prefix, given: ${input}`)
|
118 | } else if (typeof input === 'number' && !Number.isSafeInteger(input)) {
|
119 | throw new Error(
|
120 | 'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)'
|
121 | )
|
122 | }
|
123 |
|
124 | const output = toBuffer(input)
|
125 |
|
126 | if (outputType === TypeOutput.Buffer) {
|
127 | return output as TypeOutputReturnType[T]
|
128 | } else if (outputType === TypeOutput.BN) {
|
129 | return new BN(output) as TypeOutputReturnType[T]
|
130 | } else if (outputType === TypeOutput.Number) {
|
131 | const bn = new BN(output)
|
132 | const max = new BN(Number.MAX_SAFE_INTEGER.toString())
|
133 | if (bn.gt(max)) {
|
134 | throw new Error(
|
135 | 'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)'
|
136 | )
|
137 | }
|
138 | return bn.toNumber() as TypeOutputReturnType[T]
|
139 | } else {
|
140 |
|
141 | return `0x${output.toString('hex')}` as TypeOutputReturnType[T]
|
142 | }
|
143 | }
|