|
| 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