-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement BTCHeaderBytes and BTCHeaderHashBytes types (#29)
- Loading branch information
Showing
21 changed files
with
396 additions
and
404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
syntax = "proto3"; | ||
package babylon.btclightclient.v1; | ||
|
||
option go_package = "github.com/babylonchain/babylon/x/btclightclient/types"; | ||
import "gogoproto/gogo.proto"; | ||
|
||
// BTCHeader defines the structure of the incoming message bytes | ||
message BTCHeaderBytes { | ||
bytes header_bytes = 1; | ||
} | ||
option go_package = "github.com/babylonchain/babylon/x/btclightclient/types"; | ||
|
||
// BaseBTCHeader corresponds to the oldest BTC header maintained in storage | ||
// It is denoted by the header bytes and the height | ||
message BaseBTCHeader { | ||
BTCHeaderBytes header = 1; | ||
bytes header = 1 [ | ||
(gogoproto.customtype) = "github.com/babylonchain/babylon/types.BTCHeaderBytes" | ||
]; | ||
uint64 height = 2; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"encoding/json" | ||
"github.com/btcsuite/btcd/wire" | ||
) | ||
|
||
type BTCHeaderBytes []byte | ||
|
||
func NewBTCHeaderBytesFromHex(hex string) (BTCHeaderBytes, error) { | ||
var headerBytes BTCHeaderBytes | ||
err := headerBytes.UnmarshalHex(hex) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return headerBytes, nil | ||
} | ||
|
||
func (m BTCHeaderBytes) MarshalJSON() ([]byte, error) { | ||
hex, err := m.MarshalHex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return json.Marshal(hex) | ||
} | ||
|
||
func (m *BTCHeaderBytes) UnmarshalJSON(bz []byte) error { | ||
var headerHexStr string | ||
err := json.Unmarshal(bz, &headerHexStr) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return m.UnmarshalHex(headerHexStr) | ||
} | ||
|
||
func (m BTCHeaderBytes) Marshal() ([]byte, error) { | ||
return m, nil | ||
} | ||
|
||
func (m *BTCHeaderBytes) Unmarshal(data []byte) error { | ||
*m = data | ||
return nil | ||
} | ||
|
||
func (m BTCHeaderBytes) MarshalHex() (string, error) { | ||
btcdHeader, err := m.ToBlockHeader() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var buf bytes.Buffer | ||
btcdHeader.Serialize(&buf) | ||
return hex.EncodeToString(buf.Bytes()), nil | ||
} | ||
|
||
func (m *BTCHeaderBytes) UnmarshalHex(header string) error { | ||
// Decode the hash string from hex | ||
decoded, err := hex.DecodeString(header) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return m.Unmarshal(decoded) | ||
} | ||
|
||
func (m BTCHeaderBytes) MarshalTo(data []byte) (int, error) { | ||
copy(data, m) | ||
return len(data), nil | ||
} | ||
|
||
func (m *BTCHeaderBytes) Size() int { | ||
bz, _ := m.Marshal() | ||
return len(bz) | ||
} | ||
|
||
func (m BTCHeaderBytes) ToBlockHeader() (*wire.BlockHeader, error) { | ||
// Create an empty header | ||
header := &wire.BlockHeader{} | ||
|
||
// The Deserialize method expects an io.Reader instance | ||
reader := bytes.NewReader(m) | ||
// Decode the header bytes | ||
err := header.Deserialize(reader) | ||
// There was a parsing error | ||
if err != nil { | ||
return nil, err | ||
} | ||
return header, nil | ||
} | ||
|
||
func (m *BTCHeaderBytes) FromBlockHeader(header *wire.BlockHeader) { | ||
var buf bytes.Buffer | ||
header.Serialize(&buf) | ||
|
||
*m = buf.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/btcsuite/btcd/chaincfg/chainhash" | ||
) | ||
|
||
type BTCHeaderHashBytes []byte | ||
|
||
func NewBTCHeaderHashBytesFromHex(hex string) (BTCHeaderHashBytes, error) { | ||
var hashBytes BTCHeaderHashBytes | ||
err := hashBytes.UnmarshalHex(hex) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return hashBytes, nil | ||
} | ||
|
||
func (m BTCHeaderHashBytes) MarshalJSON() ([]byte, error) { | ||
hex, err := m.MarshalHex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Marshal the JSON from hex format | ||
return json.Marshal(hex) | ||
} | ||
|
||
func (m *BTCHeaderHashBytes) UnmarshalJSON(bz []byte) error { | ||
var headerHashStr string | ||
err := json.Unmarshal(bz, &headerHashStr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return m.UnmarshalHex(headerHashStr) | ||
} | ||
|
||
func (m BTCHeaderHashBytes) Marshal() ([]byte, error) { | ||
// Just return the bytes | ||
return m, nil | ||
} | ||
|
||
func (m *BTCHeaderHashBytes) Unmarshal(bz []byte) error { | ||
*m = bz | ||
return nil | ||
} | ||
|
||
func (m *BTCHeaderHashBytes) MarshalHex() (string, error) { | ||
chHash, err := m.ToChainhash() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return chHash.String(), nil | ||
} | ||
|
||
func (m *BTCHeaderHashBytes) UnmarshalHex(hash string) error { | ||
decoded, err := chainhash.NewHashFromStr(hash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Copy the bytes into the instance | ||
return m.Unmarshal(decoded[:]) | ||
} | ||
|
||
func (m BTCHeaderHashBytes) MarshalTo(data []byte) (int, error) { | ||
copy(data, m) | ||
return len(data), nil | ||
} | ||
|
||
func (m *BTCHeaderHashBytes) Size() int { | ||
bz, _ := m.Marshal() | ||
return len(bz) | ||
} | ||
|
||
func (m BTCHeaderHashBytes) ToChainhash() (*chainhash.Hash, error) { | ||
return chainhash.NewHash(m) | ||
} | ||
|
||
func (m *BTCHeaderHashBytes) FromChainhash(hash *chainhash.Hash) { | ||
var headerHashBytes BTCHeaderHashBytes | ||
headerHashBytes.Unmarshal(hash[:]) | ||
*m = headerHashBytes | ||
} | ||
|
||
func (m BTCHeaderHashBytes) reverse() { | ||
for i := 0; i < chainhash.HashSize/2; i++ { | ||
m[i], m[chainhash.HashSize-1-i] = m[chainhash.HashSize-1-i], m[i] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.