Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix simnet encoding bug #33

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/BitcoinNetworkEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ library BitcoinNetworkEncoder {
string constant BTC_BECH32_REGTEST = 'brct';
string constant BTC_BECH32_SIMNET = 'sb';

bytes constant BTC_P2PKH_MAINNET = hex"31"; // prefix = 1
bytes constant BTC_P2SH_MAINNET = hex"33"; // prefix = 3
bytes constant BTC_P2PKH_TESTNET = hex"32"; // prefix = 2
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"53"; // prefix = S
bytes constant BTC_P2SH_SIMNET = hex"73"; // prefix = s

//NB: don't forget to update `lnbtc_ext.go` when changing this enum!
enum Network {
Mainnet,
Expand All @@ -22,7 +31,7 @@ library BitcoinNetworkEncoder {
Simnet
}

function getBtcBech32Prefix(Network _network) public pure returns (bytes memory) {
function getBtcBech32Prefix(Network _network) internal pure returns (bytes memory) {
if (_network == Network.Mainnet) {
return BTC_BECH32_MAINNET_BYTES;
} else if (_network == Network.Regtest) {
Expand All @@ -36,7 +45,7 @@ library BitcoinNetworkEncoder {
}
}

function getNetworkPrefix(Network _network) public pure returns (string memory) {
function getNetworkPrefix(Network _network) internal pure returns (string memory) {
if (_network == Network.Mainnet) {
return BTC_BECH32_MAINNET;
} else if (_network == Network.Testnet) {
Expand All @@ -50,5 +59,31 @@ library BitcoinNetworkEncoder {
}
}

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) {
return BTC_P2PKH_REGTEST;
} else if (network == BitcoinNetworkEncoder.Network.Testnet) {
return BTC_P2PKH_TESTNET;
} else if (network == BitcoinNetworkEncoder.Network.Simnet) {
return BTC_P2PKH_SIMNET;
} else {
revert("Unknown network type");
}
}

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) {
return BTC_P2SH_REGTEST;
} else if (network == BitcoinNetworkEncoder.Network.Testnet) {
return BTC_P2SH_TESTNET;
} else if (network == BitcoinNetworkEncoder.Network.Simnet) {
return BTC_P2SH_SIMNET;
} else {
revert("Unknown network type");
}
}
}
44 changes: 5 additions & 39 deletions src/BitcoinUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,42 +78,7 @@ contract BitcoinUtils {
// ALPHABET_MAP[x] = z;
// }

bytes constant BTC_P2PKH_MAINNET = hex"31"; // prefix = 1
bytes constant BTC_P2SH_MAINNET = hex"33"; // prefix = 3
bytes constant BTC_P2PKH_TESTNET = hex"32"; // prefix = 2
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

function getBtcBase58_P2PKH(BitcoinNetworkEncoder.Network network) public pure returns (bytes memory) {
if (network == BitcoinNetworkEncoder.Network.Mainnet) {
return BTC_P2PKH_MAINNET;
} else if (network == BitcoinNetworkEncoder.Network.Regtest) {
return BTC_P2PKH_REGTEST;
} else if (network == BitcoinNetworkEncoder.Network.Testnet) {
return BTC_P2PKH_TESTNET;
} else if (network == BitcoinNetworkEncoder.Network.Simnet) {
return BTC_P2PKH_SIMNET;
} else {
revert("Unknown network type");
}
}

function getBtcBase58_P2SH(BitcoinNetworkEncoder.Network network) public pure returns (bytes memory) {
if (network == BitcoinNetworkEncoder.Network.Mainnet) {
return BTC_P2SH_MAINNET;
} else if (network == BitcoinNetworkEncoder.Network.Regtest) {
return BTC_P2SH_REGTEST;
} else if (network == BitcoinNetworkEncoder.Network.Testnet) {
return BTC_P2SH_TESTNET;
} else if (network == BitcoinNetworkEncoder.Network.Simnet) {
return BTC_P2SH_SIMNET;
} else {
revert("Unknown network type");
}
}

function validateBitcoinAddress(
BitcoinNetworkEncoder.Network network,
Expand All @@ -127,10 +92,12 @@ contract BitcoinUtils {
console.log("\nraw address data");
console.logBytes(bytes(BTCAddress));

bytes memory BTC_P2PKH = getBtcBase58_P2PKH(network);
bytes memory BTC_P2SH = getBtcBase58_P2SH(network);
bytes memory BTC_P2PKH = BitcoinNetworkEncoder.getBtcBase58_P2PKH(network);
bytes memory BTC_P2SH = BitcoinNetworkEncoder.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)[: prefix.length], prefix)) {
if (bytes(BTCAddress).length < 26 || bytes(BTCAddress).length > 35 || !alphabetCheck(bytes(BTCAddress))) {
return false;
}
Expand All @@ -139,7 +106,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
37 changes: 37 additions & 0 deletions test/BitcoinUtils_Simnet.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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, "SYi7rot5GKoyuRNUnjrfKYRBL7F4e9L8bN"));
}

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"));
}
}