From 09e641fc94c6284a305f695e4c8c3dd7f6d73fa0 Mon Sep 17 00:00:00 2001 From: Planxnx Date: Mon, 23 Oct 2023 19:22:18 +0700 Subject: [PATCH] feat(address): use utils for Must() --- address/address.go | 25 ++----------------------- address/go.mod | 2 ++ address/go.sum | 4 ++++ 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/address/address.go b/address/address.go index ae409b7..1f0884e 100644 --- a/address/address.go +++ b/address/address.go @@ -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" ) @@ -128,7 +128,7 @@ 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) @@ -136,24 +136,3 @@ 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 -} diff --git a/address/go.mod b/address/go.mod index a8670f1..dbfcfd9 100644 --- a/address/go.mod +++ b/address/go.mod @@ -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 diff --git a/address/go.sum b/address/go.sum index 0472d40..9a6ceff 100644 --- a/address/go.sum +++ b/address/go.sum @@ -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=