Skip to content

Commit 0ec89ee

Browse files
committed
add Bitcash support
Reviewers: arik Subscribers: ben Differential Revision: https://phabricator.bitgo.com/D6102
1 parent 68f9083 commit 0ec89ee

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitgo",
3-
"version": "3.4.10",
3+
"version": "3.4.11",
44
"description": "BitGo Javascript SDK",
55
"main": "./src/index.js",
66
"keywords": [

src/v2/baseCoin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ BaseCoin.prototype.initializeCoin = function(coin) {
5757
coinInstances = {
5858
btc: require('./coins/btc'),
5959
tbtc: require('./coins/tbtc'),
60+
bch: require('./coins/bch'),
6061
ltc: require('./coins/ltc'),
6162
tltc: require('./coins/tltc'),
6263
eth: require('./coins/eth'),

src/v2/coins/bch.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var Btc = require('./btc');
2+
var bitcoin = require('bitcoinjs-lib');
3+
var _ = require('lodash');
4+
5+
var Bch = function() {
6+
// this function is called externally from BaseCoin
7+
// replace the BaseCoin prototype with the local override prototype, which inherits from BaseCoin
8+
// effectively, move the BaseCoin prototype one level away
9+
this.__proto__ = Bch.prototype;
10+
this.network = {
11+
messagePrefix: '\x19Bitcash Signed Message:\n',
12+
bip32: {
13+
public: 0x0488b21e,
14+
private: 0x0488ade4
15+
},
16+
pubKeyHash: 0x30,
17+
scriptHash: 0x32,
18+
wif: 0xb0,
19+
dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365
20+
dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53
21+
feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
22+
};
23+
this.altScriptHash = bitcoin.networks.litecoin.scriptHash;
24+
// do not support alt destinations in prod
25+
this.supportAltScriptDestination = false;
26+
};
27+
28+
Bch.prototype.__proto__ = Btc.prototype;
29+
30+
Bch.prototype.getChain = function() {
31+
return 'bch';
32+
};
33+
Bch.prototype.getFamily = function() {
34+
return 'bch';
35+
};
36+
37+
/**
38+
* Canonicalize a Litecoin address for a specific scriptHash version
39+
* @param address
40+
* @param scriptHashVersion 1 or 2, where 1 is the old version and 2 is the new version
41+
* @returns {*} address string
42+
*/
43+
Bch.prototype.canonicalAddress = function(address, scriptHashVersion = 2) {
44+
if (!this.isValidAddress(address, true)) {
45+
throw new Error('invalid address');
46+
}
47+
const addressDetails = bitcoin.address.fromBase58Check(address);
48+
if (addressDetails.version === this.network.pubKeyHash) {
49+
// the pub keys never changed
50+
return address;
51+
}
52+
53+
if ([1, 2].indexOf(scriptHashVersion) === -1) {
54+
throw new Error('scriptHashVersion needs to be either 1 or 2');
55+
}
56+
const scriptHashMap = {
57+
// altScriptHash is the old one
58+
1: this.altScriptHash,
59+
// by default we're using the new one
60+
2: this.network.scriptHash
61+
};
62+
const newScriptHash = scriptHashMap[scriptHashVersion];
63+
return bitcoin.address.toBase58Check(addressDetails.hash, newScriptHash);
64+
};
65+
66+
Bch.prototype.getRecoveryBlockchainApiBaseUrl = function() {
67+
return 'https://ltc.blockr.io/api/v1';
68+
};
69+
70+
Bch.prototype.calculateRecoveryAddress = function(scriptHashScript) {
71+
const bitgoAddress = bitcoin.address.fromOutputScript(scriptHashScript, this.network);
72+
const blockrAddress = this.canonicalAddress(bitgoAddress, 1);
73+
return blockrAddress;
74+
};
75+
76+
module.exports = Bch;

0 commit comments

Comments
 (0)