Skip to content

Commit

Permalink
fix bug in simnet address encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
Viacheslav Zhygulin authored and Viacheslav Zhygulin committed Jul 24, 2024
1 parent 940c032 commit 98b49f3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/BitcoinUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ contract BitcoinUtils {
bytes constant BTC_P2SH_TESTNET = hex"6d"; // prefix = m
bytes constant BTC_P2PKH_REGTEST = hex"32"; // prefix = 2
bytes constant BTC_P2SH_REGTEST = hex"6d"; // prefix = m
bytes constant BTC_P2PKH_SIMNET = hex"3f"; // prefix = S
bytes constant BTC_P2SH_SIMNET = hex"7b"; // prefix = s
bytes constant BTC_P2PKH_SIMNET = hex"53"; // prefix = S
bytes constant BTC_P2SH_SIMNET = hex"73"; // prefix = s

function getBtcBase58_P2PKH(BitcoinNetworkEncoder.Network network) public pure returns (bytes memory) {
function getBtcBase58_P2PKH(BitcoinNetworkEncoder.Network network) internal pure returns (bytes memory) {
if (network == BitcoinNetworkEncoder.Network.Mainnet) {
return BTC_P2PKH_MAINNET;
} else if (network == BitcoinNetworkEncoder.Network.Regtest) {
Expand All @@ -101,7 +101,7 @@ contract BitcoinUtils {
}
}

function getBtcBase58_P2SH(BitcoinNetworkEncoder.Network network) public pure returns (bytes memory) {
function getBtcBase58_P2SH(BitcoinNetworkEncoder.Network network) internal pure returns (bytes memory) {
if (network == BitcoinNetworkEncoder.Network.Mainnet) {
return BTC_P2SH_MAINNET;
} else if (network == BitcoinNetworkEncoder.Network.Regtest) {
Expand Down Expand Up @@ -129,8 +129,10 @@ contract BitcoinUtils {

bytes memory BTC_P2PKH = getBtcBase58_P2PKH(network);
bytes memory BTC_P2SH = getBtcBase58_P2SH(network);
bytes memory prefix = BitcoinNetworkEncoder.getBtcBech32Prefix(network);

if (equalBytes(bytes(BTCAddress)[: 1], BTC_P2PKH) || equalBytes(bytes(BTCAddress)[: 1], BTC_P2SH)) {
if (equalBytes(bytes(BTCAddress)[: 1], BTC_P2PKH) || equalBytes(bytes(BTCAddress)[: 1], BTC_P2SH) &&
!equalBytes(bytes(BTCAddress)[: 3], prefix)) {
if (bytes(BTCAddress).length < 26 || bytes(BTCAddress).length > 35 || !alphabetCheck(bytes(BTCAddress))) {
return false;
}
Expand All @@ -139,7 +141,6 @@ contract BitcoinUtils {
return validateBase58Checksum(BTCAddress);
}

bytes memory prefix = BitcoinNetworkEncoder.getBtcBech32Prefix(network);
if (equalBytes(bytes(BTCAddress)[: prefix.length], prefix)) {
if (network == BitcoinNetworkEncoder.Network.Regtest) {
if (bytes(BTCAddress).length < 43 || bytes(BTCAddress).length > 63) return false;
Expand Down
38 changes: 38 additions & 0 deletions test/BitcoinUtils_Simnet.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "forge-std/Test.sol";

import "../src/BitcoinUtils.sol";
import "../src/BitcoinNetworkEncoder.sol";

// See also https://en.bitcoin.it/wiki/List_of_address_prefixes

contract BitcoinUtils_Testnet_Test is Test {
BitcoinNetworkEncoder.Network private network = BitcoinNetworkEncoder.Network.Simnet;
BitcoinUtils private utils = new BitcoinUtils();


function testValidAddress() public {
assertTrue(utils.validateBitcoinAddress(network, "ScuV2eqXfQCPcpxqqVSFtMVwkfqcwnQKB1"));
//assertTrue(utils.validateBitcoinAddress(network, "mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r"));
//assertTrue(utils.validateBitcoinAddress(network, "2NFPLS6TQVVvic6Nh85PGfcYesbGdm1fjpo"));
}

function testInvalidAddress() public {
assertFalse(utils.validateBitcoinAddress(network, ""));
assertFalse(utils.validateBitcoinAddress(network, "7SeEnXWPaCCALbVrTnszCVGfRU8cGfx"));
assertFalse(utils.validateBitcoinAddress(network, "j9ywUkWg2fTQrouxxh5rSZhRvrjMkEUfuiKe"));
}

function testBech32ValidAddress() public {
assertTrue(utils.validateBitcoinAddress(network, "sb1p5z8wl5tu7m0d79vzqqsl9gu0x4fkjug857fusx4fl4kfgwh5j25sxv5dv3"));
assertTrue(utils.validateBitcoinAddress(network, "sb1pfusykjdt46ktwq03d20uqqf94uh9487344wr3q5v9szzsxnjdfkszvtlt8"));
}

function testBech32ValidMainnetAddressIsNotValidForTestnet() public {
assertFalse(utils.validateBitcoinAddress(network, "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0"));
assertFalse(utils.validateBitcoinAddress(network, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"));
}
}

0 comments on commit 98b49f3

Please sign in to comment.