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

add first drivechain method #19

Merged
merged 9 commits into from
Sep 12, 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
20 changes: 20 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build project
on: pull_request

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: "1.23"

octobocto marked this conversation as resolved.
Show resolved Hide resolved
- uses: extractions/setup-just@v1

- uses: bufbuild/buf-action@v1
with:
setup_only: true

- run: just build
12 changes: 0 additions & 12 deletions .github/workflows/golangci-lint.yml

This file was deleted.

38 changes: 38 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Lint project
on: pull_request
jobs:
golangci-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.23

- uses: actions/checkout@v3
- uses: golangci/golangci-lint-action@v3

go-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: "1.23"

- uses: actions/cache@v4
name: Cache tools files
with:
path: .bin/go
key: ${{ runner.os }}-tools-${{ hashFiles('./scripts/check-go*.sh') }}
restore-keys: |
${{ runner.os }}-tools-

- name: format
env:
WRITE: "1"
run: |
bash scripts/check-goimports.sh
bash scripts/check-gogroup.sh

- run: git diff --exit-code
1 change: 1 addition & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ managed:
override:
- file_option: go_package_prefix
value: github.com/barebitcoin/btc-buf/gen

plugins:
- remote: buf.build/protocolbuffers/go
out: gen
Expand Down
2 changes: 1 addition & 1 deletion buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ modules:
name: buf.build/bitcoin/bitcoind
lint:
use:
- DEFAULT
- STANDARD
except:
- FIELD_NOT_REQUIRED
- PACKAGE_NO_IMPORT_CYCLE
Expand Down
3 changes: 2 additions & 1 deletion connectserver/connectserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (
"connectrpc.com/connect"
"connectrpc.com/grpchealth"
"connectrpc.com/grpcreflect"
"github.com/barebitcoin/btc-buf/connectserver/logging"
"github.com/gorilla/mux"
"github.com/oklog/ulid/v2"
"github.com/rs/zerolog"
"github.com/samber/lo"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"

"github.com/barebitcoin/btc-buf/connectserver/logging"
)

func addContextLogger() connect.Interceptor {
Expand Down
31 changes: 31 additions & 0 deletions drivechain/drivechain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package drivechain

import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"strings"
)

func ValidateDepositAddress(depositAddress string) error {
parts := strings.Split(depositAddress, "_")
if len(parts) != 3 {
return errors.New("invalid format, expected slot_address_checksum")
}

sidechainNumStr := parts[0]
address := parts[1]

addrWithoutChecksum := fmt.Sprintf("%s_%s_", sidechainNumStr, address)

hash := sha256.Sum256([]byte(addrWithoutChecksum))
calculatedChecksum := hex.EncodeToString(hash[:3])

checksum := parts[2]
if checksum != calculatedChecksum {
return errors.New("invalid checksum")
}

return nil
}
34 changes: 34 additions & 0 deletions drivechain/drivechain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package drivechain_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/barebitcoin/btc-buf/drivechain"
)

func TestCheckValidDepositAddress(t *testing.T) {
t.Run("can check various sidechain deposit addresses", func(t *testing.T) {
// a valid onchain address, but not a deposit address
err := drivechain.ValidateDepositAddress("3Ef6Dyk7UdbT8y8dge4Z73Ne2N18dPnU1h")
require.Error(t, err)

// valid
err = drivechain.ValidateDepositAddress("s5_tmEoMXN71n8cQ7VNjP3EpEEn6fbXMvASwXt_712f8a")
require.NoError(t, err)
err = drivechain.ValidateDepositAddress("s0_sYvUEgThKWXxEN9PeE3KvcD1vEXEJzq8tv_adfbb5")
require.NoError(t, err)
err = drivechain.ValidateDepositAddress("s6_0xc96aaa54e2d44c299564da76e1cd3184a2386b8d_0ad45c")
require.NoError(t, err)

err = drivechain.ValidateDepositAddress("s5_tmCtD9o83Y1R2C8E99wV7XCmxD75Ruk71zx_637f80")
require.NoError(t, err)

// invalid checksums
err = drivechain.ValidateDepositAddress("s6_0xc96aaa54e2d44c299564da76e1cd3184a2386b8d_adfbb5")
require.Error(t, err)
err = drivechain.ValidateDepositAddress("s5_tmEoMXN71n8cQ7VNjP3EpEEn6fbXMvASwXt_0ad45c")
require.Error(t, err)
})
}
Loading
Loading