Here is an article about Ethereum HD wallet addresses from a public master key:
Deriving HD wallet addresses from a public public key
As a developer working with Ethereum or other blockchain platforms, you may need to create transactions that contain HD (Hyper-Local Decentralized) wallet addresses. However, if you don’t have access to the private keys associated with these addresses, generating a transaction history report can be a challenge.
In this article, we will explore how to derive HD wallet addresses from a public master key and provide an API that meets your needs.
What are Ethereum HD wallet addresses?
Before diving into the solution, let’s quickly take a look at what Ethereum HD wallet addresses are. An HD wallet address consists of three keys:
- “m” (public master key)
- “r” (public master key)
s
(signing public key)
The public master key (m
) serves as the input for generating HD wallet addresses, and the resulting addresses can be used to send or receive Ether on the Ethereum network.
Deriving HD wallet addresses from a public public key
To derive an HD wallet address from a public master key, you need to use the Elliptic Curve Digital Signature Algorithm (ECDSA) with the secp256k1
curve. Here is a step-by-step guide:
- Load the
m
(public master key) into a variable.
- Convert the m value to an ECDSA private key using the ecdh_secp256k1 function in the elliptic-curves library.
- Use the private key to derive the corresponding HD wallet address using the ecdh_hodalv2 function.
Here is some sample code in JavaScript:
const elliptical = require('elliptic');
const ECDSA = elliptical.ec;
// Load the master public key (m)
const masterPubKey = ...; // load from file or feed
// Convert m to an ECDSA private key
const privateKey = await ECDSA.fromPublic(masterPubKey, {
curve: 'secp256k1',
});
// Derive HD Wallet Address
const hdAddress = await elliptic.hodalv2(privateKey.r, privateKey.s);
console.log(hdAddress); // output: derived HD Wallet Address
Service API for Deriving HD Wallet Addresses
You can build this solution to create a service API that meets your requirements. Here is an example of how to design the API:
// Import the required dependencies
const elliptic = require('elliptic');
const ECDSA = elliptic.ec;
const HDWalletAddressDeriver = require('./hda-deriver');
// Define a new class that extends ecdh_hodalv2
class HDWalletAddressDeriver {
async derive HdWalletAddress(masterPubKey) {
// Convert the master public key to an ECDSA private key
const privateKey = await ECDSA.fromPublic(masterPubKey, {
curve: 'secp256k1',
});
// Derive the HD wallet address using the private key
const hdAddress = await elliptic.hodalv2(privateKey.r, privateKey.s);
return hdAddress;
}
}
// Create a new instance of the HDWalletAddressDeriver class
const hdWalletAddressDeriver = new HDWalletAddressDeriver();
// Configure API endpoints for deriving HD wallet addresses
// Endpoint for deriving HD wallet address from public master key
LOCATION /derive-hd-wallet-address/:masterPubKey HTTP/1.1
{
Content-Type: application/json
}
// API endpoint for getting derived HD wallet address
GET /derive-hd-wallet-address HTTP/1.1
{
Accept: application/json
}
Example use cases
You can use this service API by sending a POST request to the /derive-hd-wallet-address endpoint, passing in the public master key:
“`bash
curl -X POST \
\
-H ‘Content-Type: application/json’ \
-d ‘{“masterPubKey”: “…
Leave a Reply