This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
/
address.ts
162 lines (139 loc) · 4.67 KB
/
address.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
156
157
158
159
160
161
162
import { Amino, Type } from "@node-a-team/ts-amino";
const { Field, DefineType } = Amino;
import bech32 from "bech32";
import { Address } from "../crypto";
import { Buffer } from "buffer/";
import { Bech32Config } from "../core/bech32Config";
let bech32Config: Bech32Config | undefined;
let globalBech32Config: Bech32Config | undefined;
const bech32ConfigStack: Bech32Config[] = [];
export function useGlobalBech32Config(config: Bech32Config | undefined) {
globalBech32Config = config;
if (bech32ConfigStack.length === 0) {
bech32Config = config;
}
}
/**
* If bech32config is just global variable, it make conflicts when you use mutiple chain api in an application.
* So, to overcome this, each config should be used in separated way.
* Use Bech32 encoding, decoding within this function.
* But if you just use only one chain api in general way, you can use [[useGlobalBech32Config]] for convenience
*/
export function useBech32Config<T>(config: Bech32Config, fn: () => T): T {
bech32ConfigStack.push(config);
bech32Config = config;
const result = fn();
bech32ConfigStack.pop();
bech32Config =
bech32ConfigStack.length > 0
? bech32ConfigStack[bech32ConfigStack.length - 1]
: globalBech32Config;
return result;
}
export async function useBech32ConfigPromise<T>(
config: Bech32Config,
fn: () => Promise<T>
): Promise<T> {
bech32ConfigStack.push(config);
bech32Config = config;
const result = await fn();
bech32ConfigStack.pop();
bech32Config =
bech32ConfigStack.length > 0
? bech32ConfigStack[bech32ConfigStack.length - 1]
: globalBech32Config;
return result;
}
@DefineType()
export class AccAddress {
public static fromBech32(bech32Addr: string): AccAddress {
if (!bech32Config) {
throw new Error("bech32 config is null");
}
const { prefix: b32Prefix, words } = bech32.decode(bech32Addr);
if (b32Prefix !== bech32Config.bech32PrefixAccAddr) {
throw new Error("Prefix doesn't match");
}
return new AccAddress(bech32.fromWords(words));
}
@Field.Array(0, { type: Type.Uint8 })
private address: Uint8Array;
constructor(address: Uint8Array | Address) {
this.address = address instanceof Address ? address.toBytes() : address;
}
public toBech32(): string {
if (!bech32Config) {
throw new Error("bech32 config is null");
}
const words = bech32.toWords(Buffer.from(this.address) as any);
return bech32.encode(bech32Config.bech32PrefixAccAddr, words);
}
public toBytes(): Uint8Array {
return new Uint8Array(this.address);
}
public marshalJSON(): Uint8Array {
return Buffer.from(`"${this.toBech32()}"`, "utf8");
}
}
@DefineType()
export class ValAddress {
public static fromBech32(bech32Addr: string): ValAddress {
if (!bech32Config) {
throw new Error("bech32 config is null");
}
const { prefix: b32Prefix, words } = bech32.decode(bech32Addr);
if (b32Prefix !== bech32Config.bech32PrefixValAddr) {
throw new Error("Prefix doesn't match");
}
return new ValAddress(bech32.fromWords(words));
}
@Field.Array(0, { type: Type.Uint8 })
private address: Uint8Array;
constructor(address: Uint8Array | Address) {
this.address = address instanceof Address ? address.toBytes() : address;
}
public toBech32(): string {
if (!bech32Config) {
throw new Error("bech32 config is null");
}
const words = bech32.toWords(Buffer.from(this.address) as any);
return bech32.encode(bech32Config.bech32PrefixValAddr, words);
}
public toBytes(): Uint8Array {
return new Uint8Array(this.address);
}
public marshalJSON(): Uint8Array {
return Buffer.from(`"${this.toBech32()}"`, "utf8");
}
}
@DefineType()
export class ConsAddress {
public static fromBech32(bech32Addr: string): ConsAddress {
if (!bech32Config) {
throw new Error("bech32 config is null");
}
const { prefix: b32Prefix, words } = bech32.decode(bech32Addr);
if (b32Prefix !== bech32Config.bech32PrefixConsAddr) {
throw new Error("Prefix doesn't match");
}
return new ConsAddress(bech32.fromWords(words));
}
@Field.Array(0, { type: Type.Uint8 })
private address: Uint8Array;
constructor(address: Uint8Array | Address) {
this.address = address instanceof Address ? address.toBytes() : address;
}
public toBech32(): string {
if (!bech32Config) {
throw new Error("bech32 config is null");
}
const words = bech32.toWords(Buffer.from(this.address) as any);
return bech32.encode(bech32Config.bech32PrefixConsAddr, words);
}
public toBytes(): Uint8Array {
return new Uint8Array(this.address);
}
public marshalJSON(): Uint8Array {
return Buffer.from(`"${this.toBech32()}"`, "utf8");
}
}