/
Address.js
executable file
·50 lines (49 loc) · 1.07 KB
/
Address.js
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
import {is_string, error_with, try_coerce_bytes, phex_from_bytes} from './utils.js';
import {Coin, ETH} from './Coin.js';
export class Address {
static from(coin, value) {
if (value === undefined) { // eth shorthand
if (coin instanceof Address) {
return new this(coin.coin, coin.bytes.slice()); // copy
}
value = coin;
coin = ETH;
} else {
coin = Coin.from(coin);
}
try {
let v = try_coerce_bytes(value);
if (v === value) {
if (!is_string(value)) {
throw new Error('unknown address format');
}
v = coin.parse(value);
}
coin.assertValid(v);
return new this(coin, v);
} catch (err) {
throw new error_with('invalid address', {coin, value}, err);
}
}
constructor(coin, bytes) {
this.coin = coin;
this.bytes = bytes;
}
get value() {
let {coin, bytes} = this;
return coin.format(bytes);
}
toObject() {
let {coin, bytes, value} = this;
return {coin: coin.toObject(), value, bytes};
}
toPhex() {
return phex_from_bytes(this.bytes);
}
toString() {
return this.value;
}
toJSON() {
return this.value;
}
}