Skip to content

Commit

Permalink
adds parseBTCTaprootAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
dpiatkivskyi committed Jun 25, 2024
1 parent e1e5e20 commit c96311e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/BTCDepositAddressDeriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

pragma solidity ^0.8.24;

import {Deriver} from "./Deriver.sol";
import {Bech32m} from "./Bech32m.sol";

error UnsupportedBtcAddress(string btcAddress);
error CannotParseBtcAddress(
string btcAddress,
string hrp,
Bech32m.DecodeError err
);

contract BTCDepositAddressDeriver {

event SeedChanged(string btcAddr1, string btcAddr2, string hrp);
Expand Down Expand Up @@ -35,6 +45,9 @@ contract BTCDepositAddressDeriver {

networkHrp = _hrp;

(p1x, p1y) = parseBTCTaprootAddress(_hrp, _btcAddr1);
(p2x, p2y) = parseBTCTaprootAddress(_hrp, _btcAddr2);

btcAddr1 = _btcAddr1;
btcAddr2 = _btcAddr2;

Expand All @@ -61,4 +74,31 @@ contract BTCDepositAddressDeriver {

return _hrp;
}

// Derive pubkey's (x,y) coordinates from taproot address
function parseBTCTaprootAddress(
string memory _hrp,
string calldata _bitcoinAddress
) public pure returns (uint256, uint256) {

(uint8 witVer, bytes memory witProg, Bech32m.DecodeError err) = Bech32m
.decodeSegwitAddress(bytes(_hrp), bytes(_bitcoinAddress));

if (err != Bech32m.DecodeError.NoError) {
revert CannotParseBtcAddress(_bitcoinAddress, _hrp, err);
}
if (witVer != 1 || witProg.length != 32) {
revert UnsupportedBtcAddress(_bitcoinAddress);
}

uint256 x = uint256(bytes32(witProg));

if (x == 0 || x >= Deriver.PP) {
revert UnsupportedBtcAddress(_bitcoinAddress);
}

uint256 y = Deriver.liftX(x);

return (x, y);
}
}

0 comments on commit c96311e

Please sign in to comment.