Skip to content

Commit

Permalink
feat(address): use utils for Must()
Browse files Browse the repository at this point in the history
  • Loading branch information
Planxnx committed Oct 23, 2023
1 parent dfc6733 commit 09e641f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
25 changes: 2 additions & 23 deletions address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package address
import (
"crypto/rand"
"encoding/hex"
"reflect"
"strings"

"github.com/Cleverse/go-utilities/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
Expand Down Expand Up @@ -128,32 +128,11 @@ func Random() (addr common.Address) {

// RandomFromPrivateKey returns a random address from a random private key
func RandomFromPrivateKey() common.Address {
return crypto.PubkeyToAddress(must(crypto.GenerateKey()).PublicKey)
return crypto.PubkeyToAddress(utils.Must(crypto.GenerateKey()).PublicKey)
}

// RandomFromBytes returns a random address from a random byte slice (via crypto/rand)
func RandomFromBytes() (addr common.Address) {
_, _ = rand.Read(addr[:])
return addr
}

// must panics if err is error or false.
func must[T any](val T, err any) T {
func() {
if err == nil {
return
}
switch e := err.(type) {
case bool:
if !e {
panic("not ok")
}
case error:
panic(e.Error())
default:
panic("must: invalid err type '" + reflect.TypeOf(err).Name() + "', should either be a bool or an error")
}
}()

return val
}
2 changes: 2 additions & 0 deletions address/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module github.com/Cleverse/go-utilities/address
go 1.21

require (
github.com/Cleverse/go-utilities/utils v0.0.0-20231023121154-63cf4ad4e8a2
github.com/ethereum/go-ethereum v1.13.4
github.com/stretchr/testify v1.8.4
)

require (
github.com/Cleverse/go-utilities/errors v0.0.0-20231019072721-442842e3dc09 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions address/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/Cleverse/go-utilities/errors v0.0.0-20231019072721-442842e3dc09 h1:kNAgub14cUBr7uoTxQSrf0qiMNJSzMhIDa8RFdv6hkk=
github.com/Cleverse/go-utilities/errors v0.0.0-20231019072721-442842e3dc09/go.mod h1:1QK+h746G1DwellQ6KK2rBCJusZqIDTZ9QFVGnUX9+Q=
github.com/Cleverse/go-utilities/utils v0.0.0-20231023121154-63cf4ad4e8a2 h1:hk2lVYgX5HZbhuES8TfnzjOj+p12pS4fWVY09zlkM/Q=
github.com/Cleverse/go-utilities/utils v0.0.0-20231023121154-63cf4ad4e8a2/go.mod h1:lSjYN1eK3zdCDktaJ+XJ/mF3T2mLG33DJj+ZSLpmm+U=
github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U=
github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
Expand Down

0 comments on commit 09e641f

Please sign in to comment.