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

Library contract #32

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/BTCDepositAddressDeriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ error CannotParseBtcAddress(

// Types of Bitcoin Network

contract BTCDepositAddressDeriver {
contract BTCDepositAddressDeriver is Bech32m, Deriver {

event SeedChanged(string btcAddr1, string btcAddr2, string hrp);

Expand Down
47 changes: 2 additions & 45 deletions src/Bech32m.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pragma solidity ^0.8.24;

error EncodingIsUnknown();

library Bech32m {
contract Bech32m {

enum BechEncoding {
// Used is SegWit v.0
Expand Down Expand Up @@ -50,49 +50,6 @@ library Bech32m {
IncorrectEncodingForSegwitVn
}

function explainDecodeError(DecodeError err) public pure returns (string memory) {
if (err == DecodeError.NoError) {
return string("No error");
} else if (err == DecodeError.IncorrectPadding) {
return string("Incorrect Padding");
} else if (err == DecodeError.IncorrectLength) {
return string("Incorrect address length");
} else if (err == DecodeError.CharacterOutOfRange) {
return string("Address contain character out of range");
} else if (err == DecodeError.MixedCase) {
return string("Address consists of both capital and small letters");
} else if (err == DecodeError.IncorrectChecksum) {
return string("Address checksum does not match");
} else if (err == DecodeError.TooShortChecksum) {
return string("Address checksum is too short");
} else if (err == DecodeError.InputIsTooLong) {
return string("Address is too long");
} else if (err == DecodeError.NotBech32Character) {
return string("Address contains character which is not in bech32 encoding");
} else if (err == DecodeError.HRPIsEmpty) {
return string("Network prefix is empty");
} else if (err == DecodeError.NoDelimiter) {
return string("No prefix delimiter in the address");
} else if (err == DecodeError.HRPMismatch) {
return string("Network prefix is different from expected");
} else if (err == DecodeError.WitnessProgramTooSmall) {
return string("Witness program should be at least 2 bytes");
} else if (err == DecodeError.EmptyData) {
return string("Witness program is empty");
} else if (err == DecodeError.WitnessProgramTooLarge) {
return string("Witness program should be maximum 40 bytes");
} else if (err == DecodeError.SegwitVersionTooLarge) {
return string("Segwit version should be from 0 to 16 (including). Got some larger number.");
} else if (err == DecodeError.IncorrectSegwitV0Program) {
return string("Length of segwit v0 program should be either 20 or 32 bytes");
} else if (err == DecodeError.IncorrectEncodingForSegwitV0) {
return string("Segwit v0 should be encoded using Bech32");
} else if (err == DecodeError.IncorrectEncodingForSegwitVn) {
return string("Segwit with versions 1-16 should be encoded with Bech32m");
}
return "";
}

// Possible characters for Bitcoin address
bytes internal constant CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";

Expand Down Expand Up @@ -617,7 +574,7 @@ library Bech32m {

// Decode a segwit address
function decodeSegwitAddress(
bytes calldata expectedHrp,
bytes memory expectedHrp,
bytes calldata addr
) public pure returns (uint8, bytes memory, DecodeError) {
(
Expand Down
2 changes: 1 addition & 1 deletion src/Deriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {EllipticCurve} from "../lib/elliptic-curve-solidity/contracts/EllipticCu

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

library Deriver {
contract Deriver is Bech32m {

// BEGIN SECP256k1 CONSTANTS
uint256 public constant GX =
Expand Down
Loading