Skip to content

Commit bf92e38

Browse files
committed
Turn address to lowercase before checksum and update comments and remove unused constants.
1 parent e6ee94c commit bf92e38

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

main.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ type CoinType = uint32
4747

4848
const (
4949
CoinTypeBTC CoinType = 0x80000000
50-
CoinTypeLTC CoinType = 0x80000002
5150
CoinTypeETH CoinType = 0x8000003c
52-
CoinTypeEOS CoinType = 0x800000c2
5351
)
5452

5553
const (
@@ -307,7 +305,7 @@ func GenerateFromBytes(prvKey *btcec.PrivateKey, compress bool) (wif, address, s
307305
}
308306
segwitNested = addressScriptHash.EncodeAddress()
309307

310-
//taproot
308+
// generate a taproot address
311309
tapKey := txscript.ComputeTaprootKeyNoScript(prvKey.PubKey())
312310
addressTaproot, err := btcutil.NewAddressTaproot(schnorr.SerializePubKey(tapKey), &chaincfg.MainNetParams)
313311
if err != nil {
@@ -455,22 +453,32 @@ func main() {
455453
func encodeEthereum(privateKeyBytes []byte) (privateKey, address string) {
456454
_, pubKey := btcec.PrivKeyFromBytes(privateKeyBytes)
457455

456+
// Public ECDSA Key
458457
publicKey := pubKey.ToECDSA()
458+
459+
// ethereum public key must be 64byte (32byte x 32byte y coordinates
460+
// this is uncompressed ECDSA public key without 04 prefix
459461
publicKeyBytes := append(publicKey.X.FillBytes(make([]byte, 32)), publicKey.Y.FillBytes(make([]byte, 32))...)
460462

461-
// Ethereum uses the last 20 bytes of the keccak256 hash of the public key
463+
// Keccak-256 hash of the public key
462464
hash := sha3.NewLegacyKeccak256()
463465
hash.Write(publicKeyBytes)
464466
addr := hash.Sum(nil)
467+
468+
// Ethereum uses the last 20 bytes of the Keccak-256 hash of the public key
469+
// this is ethereum address(without 0x prefix) but currently have not checksum
465470
addr = addr[len(addr)-20:]
466471

467472
return hex.EncodeToString(privateKeyBytes), eip55checksum(fmt.Sprintf("0x%x", addr))
468473
}
469474

470475
// eip55checksum implements the EIP55 checksum address encoding.
476+
// https://github.com/ethereum/ercs/blob/master/ERCS/erc-55.md
477+
// In English, convert the address to hex, but if the i th digit is a letter (ie. it's one of abcdef)
478+
// print it in uppercase if the 4*i th bit of the hash of the lowercase hexadecimal address is 1 otherwise print it in lowercase.
471479
// this function is copied from the go-ethereum library: go-ethereum/common/types.go checksumHex method
472480
func eip55checksum(address string) string {
473-
buf := []byte(address)
481+
buf := []byte(strings.ToLower(address))
474482
sha := sha3.NewLegacyKeccak256()
475483
sha.Write(buf[2:])
476484
hash := sha.Sum(nil)

0 commit comments

Comments
 (0)