Skip to content

Commit

Permalink
popm/wasm: fix ethereum address when generating and parsing keys (#174)
Browse files Browse the repository at this point in the history
Use uncompressed serialised secp256k1 public key to create Ethereum
addresses.

Previously, the compressed serialised secp256k1 public key was used, which
resulted in a different value being provided as the address.
  • Loading branch information
joshuasing authored Jul 10, 2024
1 parent 9245995 commit 939813c
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions web/popminer/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,11 @@ func generateKey(_ js.Value, args []js.Value) (any, error) {
return nil, fmt.Errorf("generate secp256k1 private key: %w", err)
}

compressedPubKey := privKey.PubKey().SerializeCompressed()
btcAddress, err := btcutil.NewAddressPubKey(compressedPubKey, btcChainParams)
result, err := createKeyResult(privKey, net, btcChainParams)
if err != nil {
log.Errorf("failed to create bitcoin address: %v", err)
return nil, fmt.Errorf("create bitcoin address from public key: %w", err)
return nil, err
}

return KeyResult{
EthereumAddress: ethereum.PublicKeyToAddress(compressedPubKey).String(),
Network: net,
PrivateKey: hex.EncodeToString(privKey.Serialize()),
PublicKey: hex.EncodeToString(compressedPubKey),
PublicKeyHash: btcAddress.AddressPubKeyHash().String(),
}, nil
return result, nil
}

func parseKey(_ js.Value, args []js.Value) (any, error) {
Expand All @@ -247,15 +238,25 @@ func parseKey(_ js.Value, args []js.Value) (any, error) {
}

privKey := dcrsecp256k1.PrivKeyFromBytes(b)
result, err := createKeyResult(privKey, net, btcChainParams)
if err != nil {
return nil, err
}
return result, nil
}

func createKeyResult(privKey *dcrsecp256k1.PrivateKey, net string, btcChainParams *btcchaincfg.Params) (KeyResult, error) {
compressedPubKey := privKey.PubKey().SerializeCompressed()
uncompressedPubKey := privKey.PubKey().SerializeUncompressed()

btcAddress, err := btcutil.NewAddressPubKey(compressedPubKey, btcChainParams)
if err != nil {
log.Errorf("failed to create bitcoin address: %v", err)
return nil, fmt.Errorf("create bitcoin address from public key: %w", err)
return KeyResult{}, fmt.Errorf("create bitcoin address from public key: %w", err)
}

return KeyResult{
EthereumAddress: ethereum.PublicKeyToAddress(compressedPubKey).String(),
EthereumAddress: ethereum.PublicKeyToAddress(uncompressedPubKey).String(),
Network: net,
PrivateKey: hex.EncodeToString(privKey.Serialize()),
PublicKey: hex.EncodeToString(compressedPubKey),
Expand Down

0 comments on commit 939813c

Please sign in to comment.