Skip to content

Commit

Permalink
Merge pull request #11 from astriaorg/quasystay1/support-bech32
Browse files Browse the repository at this point in the history
chore: update addresses to bech32m
  • Loading branch information
quasystaty1 authored Jul 26, 2024
2 parents b78b28e + bfdc3f2 commit 1d241ee
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 399 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM node:lts-alpine as frontend
LABEL org.opencontainers.image.source https://github.com/astriaorg/seq-faucet
LABEL org.opencontainers.image.source=https://github.com/astriaorg/seq-faucet

WORKDIR /frontend-build

Expand All @@ -9,7 +9,7 @@ RUN yarn install
COPY web ./
RUN yarn build

FROM golang:1.21-alpine as backend
FROM golang:1.22.5-alpine as backend

RUN apk add --no-cache gcc musl-dev linux-headers

Expand Down
9 changes: 4 additions & 5 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ var (
queueCapFlag = flag.Int("queuecap", 100, "Maximum transactions waiting to be sent")
versionFlag = flag.Bool("version", false, "Print version number")

assetFlag = flag.String("faucet.asset", "nria", "Asset and feeAsset used for transactions")
payoutFlag = flag.Int("faucet.amount", 1, "Number of Sequencer tokens to transfer per user request")
intervalFlag = flag.Int("faucet.minutes", 1440, "Number of minutes to wait between funding rounds")
netnameFlag = flag.String("faucet.name", "Astria Sequencer Network", "Network name to display on the frontend")
chainIdFlag = flag.String("sequencer.chainId", "astria-dusk-7", "Sequencer chain id to use for transactions")
chainIdFlag = flag.String("sequencer.chainId", "astria-dusk-9", "Sequencer chain id to use for transactions")
prefixFlag = flag.String("bech32.prefix", "astria", "Bech32 prefix for the address")

privKeyFlag = flag.String("wallet.privkey", os.Getenv("PRIVATE_KEY"), "Private key hex to fund user requests with")
providerFlag = flag.String("wallet.provider", os.Getenv("WEB3_PROVIDER"), "Endpoint for Ethereum JSON-RPC connection")
Expand All @@ -44,7 +46,7 @@ func Execute() {
panic(fmt.Errorf("failed to read private key: %w", err))
}

txBuilder, err := chain.NewTxBuilder(*providerFlag, privateKey, *chainIdFlag)
txBuilder, err := chain.NewTxBuilder(*providerFlag, privateKey, *chainIdFlag, *prefixFlag, *assetFlag)
if err != nil {
panic(fmt.Errorf("cannot connect to web3 provider: %w", err))
}
Expand All @@ -62,9 +64,6 @@ func getPrivateKeyFromFlag() (*ed25519.PrivateKey, error) {
}

hexkey := *privKeyFlag
if chain.Has0xPrefix(hexkey) {
hexkey = hexkey[2:]
}

privateKeyBytes, err := hex.DecodeString(hexkey)
if err != nil {
Expand Down
21 changes: 12 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
module github.com/astriaorg/seq-faucet

go 1.21
go 1.22.2

toolchain go1.22.5

require (
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.34.1-20240517150107-e54d335d6779.1
buf.build/gen/go/astria/protocol-apis/protocolbuffers/go v1.34.1-20240517150110-b4ffcca70a25.1
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.34.2-20240626163506-691883836b9e.2
buf.build/gen/go/astria/protocol-apis/protocolbuffers/go v1.34.2-20240709150132-719f5e6bd198.2
github.com/LK4D4/trylock v0.0.0-20191027065348-ff7e133a5c54
github.com/astriaorg/go-sequencer-client v0.4.0
github.com/cometbft/cometbft v0.38.7
github.com/ethereum/go-ethereum v1.13.15
github.com/astriaorg/astria-cli-go/modules/bech32m v0.0.0-20240719102244-9c858af4bf4c
github.com/astriaorg/astria-cli-go/modules/go-sequencer-client v0.0.0-20240711221144-628c2db1b0aa
github.com/cometbft/cometbft v0.38.9
github.com/jellydator/ttlcache/v2 v2.11.1
github.com/sirupsen/logrus v1.9.0
github.com/sirupsen/logrus v1.9.3
github.com/urfave/negroni v1.0.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
Expand All @@ -36,9 +39,9 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae // indirect
Expand All @@ -63,6 +66,6 @@ require (
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.60.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
76 changes: 54 additions & 22 deletions go.sum

Large diffs are not rendered by default.

38 changes: 20 additions & 18 deletions internal/chain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,49 @@ import (

primproto "buf.build/gen/go/astria/primitives/protocolbuffers/go/astria/primitive/v1"
txproto "buf.build/gen/go/astria/protocol-apis/protocolbuffers/go/astria/protocol/transactions/v1alpha1"
client "github.com/astriaorg/go-sequencer-client/client"
"github.com/astriaorg/astria-cli-go/modules/bech32m"
client "github.com/astriaorg/astria-cli-go/modules/go-sequencer-client/client"
"github.com/cometbft/cometbft/libs/bytes"

"github.com/ethereum/go-ethereum/common"
log "github.com/sirupsen/logrus"
)

type TxBuilder interface {
Sender() common.Address
Sender() string
Transfer(ctx context.Context, to string, value *big.Int) (bytes.HexBytes, error)
}

type TxBuild struct {
sequencerClient client.Client
privateKey *ed25519.PrivateKey
signer client.Signer
fromAddress common.Address
fromAddress string
sequencerChainId string
asset string
}

func NewTxBuilder(provider string, privateKey *ed25519.PrivateKey, chainId string) (TxBuilder, error) {
func NewTxBuilder(provider string, privateKey *ed25519.PrivateKey, chainId string, prefix string, asset string) (TxBuilder, error) {
sequencerClient, err := client.NewClient(provider)
if err != nil {
return nil, err
}

signer := client.NewSigner(*privateKey)
fromAddress, err := bech32m.EncodeFromBytes(prefix, signer.Address())
if err != nil {
return nil, err
}

return &TxBuild{
sequencerClient: *sequencerClient,
privateKey: privateKey,
signer: *signer,
fromAddress: signer.Address(),
fromAddress: fromAddress.String(),
sequencerChainId: chainId,
asset: asset,
}, nil
}

func (b *TxBuild) Sender() common.Address {
func (b *TxBuild) Sender() string {
return b.fromAddress
}

Expand All @@ -54,17 +60,13 @@ func (b *TxBuild) Transfer(ctx context.Context, to string, value *big.Int) (byte
panic(err)
}

buf := make([]byte, 16)
value.FillBytes(buf)

amount, err := convertToUint128(value)
if err != nil {
panic(err)
}

toAddress := common.HexToAddress(to)
log.Infof("Transfering %s to %s", amount, to)
toAddr := &primproto.Address{
Inner: toAddress.Bytes(),
Bech32M: to,
}

unsignedTx := &txproto.UnsignedTransaction{
Expand All @@ -76,10 +78,10 @@ func (b *TxBuild) Transfer(ctx context.Context, to string, value *big.Int) (byte
{
Value: &txproto.Action_TransferAction{
TransferAction: &txproto.TransferAction{
To: toAddr,
Amount: amount,
AssetId: client.DefaultAstriaAssetID[:],
FeeAssetId: client.DefaultAstriaAssetID[:],
To: toAddr,
Amount: amount,
Asset: b.asset,
FeeAsset: b.asset,
},
},
},
Expand Down
16 changes: 0 additions & 16 deletions internal/chain/util.go

This file was deleted.

52 changes: 0 additions & 52 deletions internal/chain/util_test.go

This file was deleted.

11 changes: 7 additions & 4 deletions internal/server/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"net/http"
"strings"

"github.com/astriaorg/seq-faucet/internal/chain"
"github.com/astriaorg/astria-cli-go/modules/bech32m"
log "github.com/sirupsen/logrus"
)

type claimRequest struct {
Expand Down Expand Up @@ -83,10 +84,12 @@ func readAddress(r *http.Request) (string, error) {
if err := decodeJSONBody(r, &claimReq); err != nil {
return "", err
}
if !chain.IsValidAddress(claimReq.Address, false) {
return "", &malformedRequest{status: http.StatusBadRequest, message: "invalid address"}
err := bech32m.Validate(claimReq.Address)
if err != nil {
return "", &malformedRequest{status: http.StatusBadRequest, message: "Invalid address"}
}

log.WithFields(log.Fields{
"address": claimReq.Address}).Info("Received claim request")
return claimReq.Address, nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *Server) consumeQueue() {
defer s.mutex.Unlock()
for len(s.queue) != 0 {
address := <-s.queue
txHash, err := s.Transfer(context.Background(), address, s.cfg.payoutNano)
txHash, err := s.Transfer(context.Background(), address, s.cfg.payout)
if err != nil {
log.WithError(err).Error("Failed to handle transaction in the queue")
} else {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *Server) handleClaim() http.HandlerFunc {

ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
txHash, err := s.Transfer(ctx, address, s.cfg.payoutNano)
txHash, err := s.Transfer(ctx, address, s.cfg.payout)
s.mutex.Unlock()
if err != nil {
log.WithError(err).Error("Failed to send transaction")
Expand All @@ -126,7 +126,7 @@ func (s *Server) handleInfo() http.HandlerFunc {
return
}
renderJSON(w, infoResponse{
Account: s.Sender().String(),
Account: s.Sender(),
Network: s.cfg.network,
Payout: s.cfg.payout.String(),
}, http.StatusOK)
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"scripts": {
"build-web": "cd web && yarn install && yarn vite build"
},
"dependencies": {
"bech32": "^2.0.0"
}
}
1 change: 0 additions & 1 deletion web/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* a value or a type, so tell TypeScript to enforce using
* `import type` instead of `import` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
Expand Down
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@ethersproject/address": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"animate.css": "^4.1.1",
"bech32": "^2.0.0",
"bulma": "^0.9.4",
"bulma-toast": "^2.4.2"
}
Expand Down
11 changes: 1 addition & 10 deletions web/src/Faucet.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { onMount } from 'svelte';
import { getAddress } from '@ethersproject/address';
import { bech32 } from 'bech32';
import { CloudflareProvider } from '@ethersproject/providers';
import { setDefaults as setToast, toast } from 'bulma-toast';
Expand Down Expand Up @@ -41,14 +41,6 @@
return;
}
}
try {
address = getAddress(address);
} catch (error) {
toast({ message: error.reason, type: 'is-warning' });
return;
}
const res = await fetch('/api/claim', {
method: 'POST',
headers: {
Expand All @@ -58,7 +50,6 @@
address,
}),
});
let { msg } = await res.json();
let type = res.ok ? 'is-success' : 'is-warning';
toast({ message: msg, type });
Expand Down
Loading

0 comments on commit 1d241ee

Please sign in to comment.