-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.ts
87 lines (75 loc) · 2.81 KB
/
utils.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
import { ChainId, Nonce } from "@iov/bcp";
import { Encoding, Uint53 } from "@iov/encoding";
import BN from "bn.js";
const bcpChainIdPrefix = "ethereum-eip155-";
export function decodeHexQuantity(hexString: string): number {
// "0x00" in ganache response is an error: zero should be represented as "0x0"
// "0x0n" in ganache response is an error: no leading zeroes allowed, when fixed implement {1}[a-f1-9]
if (hexString.match(/^0x[a-f0-9]+$/)) {
return parseInt(hexString, 16);
}
throw new Error("invalid hex quantity input");
}
export function decodeHexQuantityString(hexString: string): string {
if (hexString.match(/^0x[a-f0-9]+$/)) {
const hexToBN = new BN(hexString.replace("0x", ""), 16);
return hexToBN.toString();
}
throw new Error("invalid hex quantity input");
}
export function decodeHexQuantityNonce(hexString: string): Nonce {
const value = new Uint53(decodeHexQuantity(hexString));
return value.toNumber() as Nonce;
}
/**
* Takes a hex representation optionally prefixed with 0x and returns an Ethereum-friendly
* representation with a prefix.
*/
export function toEthereumHex(input: string | Uint8Array): string {
const str = typeof input === "string" ? input : Encoding.toHex(input);
const match = str.match(/^(?:0x)?(.*)$/);
if (!match) {
throw new Error("Could not convert to Ethereum hex: invalid input");
}
return `0x${match[1]}`;
}
export function encodeQuantity(value: number): string {
try {
const checkedValue = new Uint53(value);
return toEthereumHex(new BN(checkedValue.toNumber()).toString(16));
} catch {
throw new Error("Input is not a unsigned safe integer");
}
}
export function encodeQuantityString(value: string): string {
if (!value.match(/^[0-9]+$/)) {
throw new Error("Input is not a valid string number");
}
return toEthereumHex(new BN(value).toString(16));
}
/**
* Takes a hex representation optionally prefixed with 0x and returns a normalized
* representation: unprefixed, padded to even characters count, lower case.
*/
export function normalizeHex(input: string): string {
const unprefixedLower = input.replace("0x", "").toLowerCase();
if (unprefixedLower.length % 2 !== 0) {
return "0" + unprefixedLower;
} else {
return unprefixedLower;
}
}
export function toBcpChainId(numericChainId: number): ChainId {
const parsed = new Uint53(numericChainId);
return `${bcpChainIdPrefix}${parsed.toString()}` as ChainId;
}
export function fromBcpChainId(chainId: ChainId): number {
if (!chainId.startsWith(bcpChainIdPrefix)) {
throw new Error(`Expected chain ID to start with '${bcpChainIdPrefix}'`);
}
const rest = chainId.slice(bcpChainIdPrefix.length);
if (rest !== "0" && !rest.match(/^[1-9]{1}[0-9]*$/)) {
throw new Error("Invalid format of EIP155 chain ID");
}
return Uint53.fromString(rest).toNumber();
}