-
Notifications
You must be signed in to change notification settings - Fork 36
/
conversions.go
46 lines (40 loc) · 1.58 KB
/
conversions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package conversions
import (
"crypto/ecdsa"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)
// PrivateKeyToAddress is a handy converter for an ecdsa private key to a usable eth address
func PrivateKeyToAddress(privateKey *ecdsa.PrivateKey) (common.Address, error) {
publicKeyECDSA, ok := privateKey.Public().(*ecdsa.PublicKey)
if !ok {
return common.Address{}, fmt.Errorf(
"Error converting public key to ECDSA format. Private Key: %s Public Key: %s", privateKey, privateKey.Public())
}
return crypto.PubkeyToAddress(*publicKeyECDSA), nil
}
// Credit to kimxilxyong: https://github.com/ethereum/go-ethereum/issues/21221#issuecomment-802092592
// EtherToWei converts an ETH float amount to wei
func EtherToWei(eth *big.Float) *big.Int {
truncInt, _ := eth.Int(nil)
truncInt = new(big.Int).Mul(truncInt, big.NewInt(params.Ether))
fracStr := strings.Split(fmt.Sprintf("%.18f", eth), ".")[1]
fracStr += strings.Repeat("0", 18-len(fracStr))
fracInt, _ := new(big.Int).SetString(fracStr, 10)
wei := new(big.Int).Add(truncInt, fracInt)
return wei
}
// WeiToEther converts a wei amount to eth float
func WeiToEther(wei *big.Int) *big.Float {
f := new(big.Float)
f.SetPrec(236) // IEEE 754 octuple-precision binary floating-point format: binary256
f.SetMode(big.ToNearestEven)
fWei := new(big.Float)
fWei.SetPrec(236) // IEEE 754 octuple-precision binary floating-point format: binary256
fWei.SetMode(big.ToNearestEven)
return f.Quo(fWei.SetInt(wei), big.NewFloat(params.Ether))
}