Here is an article on Ethereum HD wallet addresses from a public master key:
Deriving HD wallet addresses from a public master key
As a developer working with Ethereum or other blockchain platforms, you may need to create transactions that require deriving HD (Hyper-Local Decentralized) wallet addresses. However, if you don’t have access to the private keys associated with these addresses, creating a transaction history report can be challenging.
In this article, we’ll explore how to derive HD wallet addresses from a public master key and provide a service API that meets your needs.
What are Ethereum HD wallet addresses?
Before we dive into the solution, let’s quickly review what Ethereum HD wallet addresses are. An HD wallet address consists of three keys:
m
(public master key)
r
(public root key)
s
(public signature key)
The public master key (m
) serves as input to create 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 master 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 curve secp256k1
. 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 theecdh_secp256k1
function from theelliptic-curves
library.
- Use the private key to derive the corresponding HD wallet address using the
ecdh_hodalv2
function.
Here is a sample code in JavaScript:
const elliptic = require('elliptic');
const ECDSA = elliptic.ec;
// Load master public key (m)
const masterPubKey = ...; // load from file or input
// convert m to 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: the derived HD wallet address
Service API for deriving HD wallet addresses
To create a service API that fits your needs, you can build on top of this solution. Here is an example of how to design the API:
// Import required dependencies
const elliptic = require('elliptic');
const ECDSA = elliptic.ec;
const HDWalletAddressDeriver = require('./hda-deriver');
// Define a new class that extends the ecdh_hodalv2
function
class HDWalletAddressDeriver {
async derive HdWalletAddress(masterPubKey) {
// Convert the master public key to the 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();
// Define API endpoints for deriving HD wallet addresses
// Endpoint for deriving HD wallet address from master public key
POST /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
To use this service API, you can send a POST request to the /derive-hd-wallet-address
endpoint with the master public key as input:
“`bash
curl -X POST \
-H ‘Content-Type: application/json’ \
-d ‘{“masterPubKey”: “…