-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathencodedData.ts
155 lines (137 loc) · 4.17 KB
/
encodedData.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Record } from "@renproject/react-components";
import base58 from "bs58";
export enum Encodings {
AUTO = "auto",
UNKNOWN = "unknown",
HEX = "hex",
BASE64 = "base64",
BASE58 = "base58",
BUFFER = "buffer",
}
const DefaultEncodedData = {
value: "" as string | Buffer,
encoding: Encodings.AUTO,
};
const parse = (
param: string | Buffer | typeof DefaultEncodedData,
encoding?: Encodings,
) => {
if (encoding !== undefined) {
if (typeof param === "string" && encoding !== Encodings.BUFFER) {
param = {
value: param,
encoding,
};
} else if (param instanceof Buffer && encoding === Encodings.BUFFER) {
param = {
value: param,
encoding,
};
}
}
if (typeof param === "string") {
param = {
value: param,
encoding: Encodings.AUTO,
};
}
if (param instanceof Buffer) {
param = {
value: param,
encoding: Encodings.BUFFER,
};
}
if (param.encoding === Encodings.AUTO) {
if (typeof param.value === "string") {
if (
param.value === "" ||
param.value.slice(0, 2) === "0x" ||
/^[A-Fa-f0-9]+$/.exec(param.value)
) {
param.encoding = Encodings.HEX;
} else if (/^[A-Za-z0-9+/=]+$/.exec(param.value)) {
param.encoding = Encodings.BASE64;
}
} else if (param.value instanceof Buffer) {
param.encoding = Encodings.BUFFER;
}
}
if (
param.encoding === Encodings.BUFFER &&
!(param.value instanceof Buffer)
) {
throw new Error("invalid buffer");
}
if (param.encoding === Encodings.HEX) {
if (typeof param.value !== "string") {
throw new Error("invalid hex");
}
if (param.value.slice(0, 2) === "0x") {
param.value = param.value.slice(2);
}
if (param.value === "") {
param.value = "00";
}
if (param.value.length % 2 === 1) {
param.value = `0${param.value}`;
}
if (!/^[A-Fa-f0-9]+$/.exec(param.value)) {
throw new Error("invalid hex");
}
}
return param;
};
export class EncodedData extends Record(DefaultEncodedData) {
public static Encodings = Encodings;
constructor(
param: EncodedData | string | Buffer | typeof DefaultEncodedData,
encoding?: Encodings,
) {
if (param instanceof EncodedData) {
param = { value: param.value, encoding: param.encoding };
}
param = parse(param, encoding);
super(param);
}
public toHex(this: EncodedData, prefix: string = "0x"): string {
switch (this.encoding) {
case Encodings.HEX:
return prefix + this.value.toString();
default:
return prefix + this.toBuffer().toString("hex");
}
}
public toBase64(this: EncodedData): string {
switch (this.encoding) {
case Encodings.BASE64:
return this.value as string;
default:
return this.toBuffer().toString("base64");
}
}
public toBase58(this: EncodedData): string {
switch (this.encoding) {
case Encodings.BASE58:
return this.value as string;
default:
return base58.encode(this.toBuffer());
}
}
public toBuffer(this: EncodedData): Buffer {
switch (this.encoding) {
case Encodings.HEX:
return Buffer.from(this.value as string, "hex");
case Encodings.BASE64:
return Buffer.from(this.value as string, "base64");
case Encodings.BASE58:
return Buffer.from(base58.decode(this.value as string));
case Encodings.BUFFER:
return this.value as Buffer;
default:
throw new Error("Unable to convert data");
}
}
public toString(this: EncodedData): string {
return this.toHex();
}
}