-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathbch.ts
More file actions
48 lines (40 loc) · 1.84 KB
/
bch.ts
File metadata and controls
48 lines (40 loc) · 1.84 KB
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
import { BitGoBase } from '@bitgo/sdk-core';
import { address as wasmAddress } from '@bitgo/wasm-utxo';
import { AbstractUtxoCoin } from '../../abstractUtxoCoin.js';
import { UtxoCoinName } from '../../names.js';
import { isScriptRecipient } from '../../transaction/index.js';
export class Bch extends AbstractUtxoCoin {
readonly name: UtxoCoinName = 'bch';
protected constructor(bitgo: BitGoBase) {
super(bitgo);
}
static createInstance(bitgo: BitGoBase): Bch {
return new Bch(bitgo);
}
/**
* Canonicalize a Bitcoin Cash address for a specific version
*
* Starting on January 14th, 2018 Bitcoin Cash's bitcoin-abc node switched over to using cashaddr
* encoding for all of their addresses in order to distinguish them from Bitcoin Core's.
* https://www.bitcoinabc.org/cashaddr. We're sticking with the old base58 format because
* migrating over to the new format will be laborious, and we want to see how the space evolves
*
* @param address may or may not be prefixed with the network, example bitcoincash:pppkt7q2axpsm2cajyjtu6x8fsh6ywauzgxmsru962 or pppkt7q2axpsm2cajyjtu6x8fsh6ywauzgxmsru962
* @param version the version of the desired address, 'base58' or 'cashaddr', defaulting to 'base58'
* @returns {*} address string
*/
canonicalAddress(address: string, version: unknown = 'base58'): string {
if (isScriptRecipient(address)) {
return address;
}
if (version === 'base58') {
const script = wasmAddress.toOutputScriptWithCoin(address, this.name);
return wasmAddress.fromOutputScriptWithCoin(script, this.name, 'default');
}
if (version === 'cashaddr') {
const script = wasmAddress.toOutputScriptWithCoin(address, this.name);
return wasmAddress.fromOutputScriptWithCoin(script, this.name, 'cashaddr');
}
throw new Error(`invalid version ${version}`);
}
}