Add cashaddr address format for bitcoin cash

This commit is contained in:
Ian Coleman
2018-05-08 09:42:36 +10:00
parent 956e44efd7
commit e0f91e2029
5 changed files with 9394 additions and 37 deletions

View File

@@ -608,13 +608,25 @@
<h2>Derived Addresses</h2>
<p>Note these addresses are derived from the BIP32 Extended Key</p>
</div>
<div class="col-md-12 use-bitpay-addresses-container hidden">
<div class="checkbox">
<div class="col-md-12 bch-addr-type-container hidden">
<div class="radio">
<label>
<input type="checkbox" class="use-bitpay-addresses">
<input type="radio" value="cashaddr" name="bch-addr-type" class="use-bch-cashaddr-addresses" checked="checked">
<span>Use CashAddr addresses for Bitcoin Cash (ie starting with 'q' instead of '1')</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" value="bitpay" name="bch-addr-type" class="use-bch-bitpay-addresses">
<span>Use BitPay-style addresses for Bitcoin Cash (ie starting with 'C' instead of '1')</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" value="legacy" name="bch-addr-type" class="use-bch-legacy-addresses">
<span>Use legacy addresses for Bitcoin Cash (ie starting with '1')</span>
</label>
</div>
</div>
<div class="col-md-12">
<div class="checkbox">
@@ -903,6 +915,7 @@
<script src="js/segwit-parameters.js"></script>
<script src="js/ethereumjs-util.js"></script>
<script src="js/ripple-util.js"></script>
<script src="js/bchaddrjs-0.2.1.js"></script>
<script src="js/sjcl-bip39.js"></script>
<script src="js/wordlist_english.js"></script>
<script src="js/wordlist_japanese.js"></script>

9328
src/js/bchaddrjs-0.2.1.js Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -307,17 +307,6 @@ bitcoinjs.bitcoin.networks.bgold = {
wif: 128
};
bitcoinjs.bitcoin.networks.bitcoinCashBitbpay = {
messagePrefix: '\x18Bitcoin Signed Message:\n',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
},
pubKeyHash: 0x1c,
scriptHash: 0x28,
wif: 0x80
};
bitcoinjs.bitcoin.networks.monacoin = {
messagePrefix: '\x18Monacoin Signed Message:\n',
bip32: {

View File

@@ -91,8 +91,8 @@
DOM.generatedStrength = $(".generate-container .strength");
DOM.generatedStrengthWarning = $(".generate-container .warning");
DOM.hardenedAddresses = $(".hardened-addresses");
DOM.useBitpayAddressesContainer = $(".use-bitpay-addresses-container");
DOM.useBitpayAddresses = $(".use-bitpay-addresses");
DOM.bitcoinCashAddressTypeContainer = $(".bch-addr-type-container");
DOM.bitcoinCashAddressType = $("[name=bch-addr-type]")
DOM.useBip38 = $(".use-bip38");
DOM.bip38Password = $(".bip38-password");
DOM.addresses = $(".addresses");
@@ -147,7 +147,7 @@
DOM.privateKeyToggle.on("click", togglePrivateKeys);
DOM.csvTab.on("click", updateCsv);
DOM.languages.on("click", languageChanged);
DOM.useBitpayAddresses.on("change", useBitpayAddressesChange);
DOM.bitcoinCashAddressType.on("change", bitcoinCashAddressTypeChange);
setQrEvents(DOM.showQrEls);
disableForms();
hidePending();
@@ -172,7 +172,7 @@
clearDerivedKeys();
clearAddressesList();
DOM.litecoinLtubContainer.addClass("hidden");
DOM.useBitpayAddressesContainer.addClass("hidden");
DOM.bitcoinCashAddressTypeContainer.addClass("hidden");
var networkIndex = e.target.value;
var network = networks[networkIndex];
network.onSelect();
@@ -424,8 +424,7 @@
}, 50);
}
function useBitpayAddressesChange() {
setBitcoinCashNetworkValues();
function bitcoinCashAddressTypeChange() {
phraseChanged();
}
@@ -852,6 +851,16 @@
privkey = convertRipplePriv(privkey);
address = convertRippleAdrr(address);
}
// Bitcoin Cash address format may vary
if (networks[DOM.network.val()].name == "BCH - Bitcoin Cash") {
var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
if (bchAddrType == "cashaddr") {
address = bchaddr.toCashAddress(address);
}
else if (bchAddrType == "bitpay") {
address = bchaddr.toBitpayAddress(address);
}
}
// Segwit addresses are different
if (isSegwit) {
if (!segwitAvailable) {
@@ -1417,19 +1426,6 @@
DOM.bip141unavailable.removeClass("hidden");
}
function useBitpayAddresses() {
return !(DOM.useBitpayAddresses.prop("checked"));
}
function setBitcoinCashNetworkValues() {
if (useBitpayAddresses()) {
network = bitcoinjs.bitcoin.networks.bitcoin;
}
else {
network = bitcoinjs.bitcoin.networks.bitcoinCashBitbpay;
}
}
function adjustNetworkForSegwit() {
// If segwit is selected the xpub/xprv prefixes need to be adjusted
// to avoid accidentally importing BIP49 xpub to BIP44 watch only
@@ -1582,8 +1578,7 @@
name: "BCH - Bitcoin Cash",
segwitAvailable: false,
onSelect: function() {
DOM.useBitpayAddressesContainer.removeClass("hidden");
setBitcoinCashNetworkValues();
DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
setHdCoin(145);
},
},

View File

@@ -553,7 +553,7 @@ it('Allows selection of slimcoin testnet', function(done) {
it('Allows selection of bitcoin cash', function(done) {
var params = {
selectText: "BCH - Bitcoin Cash",
firstAddress: "1JKvb6wKtsjNoCRxpZ4DGrbniML7z5U16A",
firstAddress: "bitcoincash:qzlquk7w4hkudxypl4fgv8x279r754dkvur7jpcsps",
};
testNetwork(done, params);
});
@@ -3120,10 +3120,26 @@ it('Warns when entropy is filtered and discarded', function(done) {
});
});
// Bitcoin Cash address can be set to use cashaddr format
it('Can use cashaddr format for bitcoin cash addresses', function(done) {
driver.executeScript(function() {
$(".use-bch-cashaddr-addresses").prop("checked", true);
});
driver.findElement(By.css('.phrase'))
.sendKeys("abandon abandon ability");
selectNetwork("BCH - Bitcoin Cash");
driver.sleep(generateDelay).then(function() {
getFirstAddress(function(address) {
expect(address).toBe("bitcoincash:qzlquk7w4hkudxypl4fgv8x279r754dkvur7jpcsps");
done();
});
});
});
// Bitcoin Cash address can be set to use bitpay format
it('Can use bitpay format for bitcoin cash addresses', function(done) {
driver.executeScript(function() {
$(".use-bitpay-addresses").prop("checked", true);
$(".use-bch-bitpay-addresses").prop("checked", true);
});
driver.findElement(By.css('.phrase'))
.sendKeys("abandon abandon ability");
@@ -3136,6 +3152,22 @@ it('Can use bitpay format for bitcoin cash addresses', function(done) {
});
});
// Bitcoin Cash address can be set to use legacy format
it('Can use legacy format for bitcoin cash addresses', function(done) {
driver.executeScript(function() {
$(".use-bch-legacy-addresses").prop("checked", true);
});
driver.findElement(By.css('.phrase'))
.sendKeys("abandon abandon ability");
selectNetwork("BCH - Bitcoin Cash");
driver.sleep(generateDelay).then(function() {
getFirstAddress(function(address) {
expect(address).toBe("1JKvb6wKtsjNoCRxpZ4DGrbniML7z5U16A");
done();
});
});
});
// End of tests ported from old suit, so no more comments above each test now
it('Can generate more addresses from a custom index', function(done) {