Skip to content

Commit

Permalink
popm/wasm: rename KeyResult fields and add bitcoinScriptHash (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing authored Jul 11, 2024
1 parent 9c4cea5 commit 6a94b4c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
11 changes: 8 additions & 3 deletions web/packages/pop-miner/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ export type GenerateKeyArgs = {
*/
export type KeyResult = {
/**
* The Ethereum address for the key.
* The Hemi Ethereum address for the key.
*/
readonly ethereumAddress: string;
readonly hemiAddress: string;

/**
* The network the addresses were created for.
Expand All @@ -153,7 +153,12 @@ export type KeyResult = {
/**
* The Bitcoin pay-to-pubkey-hash address for the key.
*/
readonly publicKeyHash: string;
readonly bitcoinPubKeyHash: string;

/**
* The Bitcoin pay-to-pubkey-hash script hash for the key.
*/
readonly bitcoinScriptHash: string;
};

/**
Expand Down
12 changes: 8 additions & 4 deletions web/popminer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ type VersionResult struct {
//
// Returned by MethodGenerateKey and MethodParseKey.
type KeyResult struct {
// EthereumAddress is the Ethereum address for the key.
EthereumAddress string `json:"ethereumAddress"`
// HemiAddress is the Hemi Ethereum address for the key.
HemiAddress string `json:"hemiAddress"`

// Network is the network the addresses were created for.
Network string `json:"network"`
Expand All @@ -118,8 +118,12 @@ type KeyResult struct {
// encoded as a hexadecimal string.
PublicKey string `json:"publicKey"`

// PublicKeyHash is the Bitcoin pay-to-pubkey-hash address for the key.
PublicKeyHash string `json:"publicKeyHash"`
// BitcoinPubKeyHash is the Bitcoin pay-to-pubkey-hash address for the key.
BitcoinPubKeyHash string `json:"bitcoinPubKeyHash"`

// BitcoinScriptHash is the Bitcoin pay-to-pubkey-hash script hash for the
// key.
BitcoinScriptHash string `json:"bitcoinScriptHash"`
}

// BitcoinAddressToScriptHashResult contains the script hash requested for an
Expand Down
24 changes: 16 additions & 8 deletions web/popminer/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,30 @@ func parseKey(_ js.Value, args []js.Value) (any, error) {
return result, nil
}

func createKeyResult(privKey *dcrsecp256k1.PrivateKey, net string, btcChainParams *btcchaincfg.Params) (KeyResult, error) {
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 KeyResult{}, fmt.Errorf("create bitcoin address from public key: %w", err)
return nil, fmt.Errorf("create bitcoin address from public key: %w", err)
}
btcPubKeyHash := btcAddress.AddressPubKeyHash()

return KeyResult{
EthereumAddress: ethereum.PublicKeyToAddress(uncompressedPubKey).String(),
Network: net,
PrivateKey: hex.EncodeToString(privKey.Serialize()),
PublicKey: hex.EncodeToString(compressedPubKey),
PublicKeyHash: btcAddress.AddressPubKeyHash().String(),
btcScript, err := txscript.PayToAddrScript(btcPubKeyHash)
if err != nil {
return nil, fmt.Errorf("convert address to script: %w", err)
}
btcScriptHash := sha256.Sum256(btcScript)

return &KeyResult{
HemiAddress: ethereum.PublicKeyToAddress(uncompressedPubKey).String(),
Network: net,
PrivateKey: hex.EncodeToString(privKey.Serialize()),
PublicKey: hex.EncodeToString(compressedPubKey),
BitcoinPubKeyHash: btcPubKeyHash.String(),
BitcoinScriptHash: hex.EncodeToString(btcScriptHash[:]),
}, nil
}

Expand Down

0 comments on commit 6a94b4c

Please sign in to comment.