Java API
HSM Dinamo
Loading...
Looking for...
No entries found
BchainCreateCkdBip32.java

Example of BIP32 XPrv (CKD) child key derivation, hash signing, and ECDSA signature verification using the blockchain module.

See Note on examples.
package doxy.examples;
import com.dinamonetworks.BchainKeyInfo;
import com.dinamonetworks.Dinamo;
import br.com.trueaccess.TacException;
import br.com.trueaccess.TacNDJavaLib;
public class BchainCreateCkdBip32 {
public static void main(String[] args) throws TacException {
String ip = "127.0.0.1";
String user = "master";
String password = "12345678";
Dinamo api = new Dinamo();
api.openSession(ip, user, password);
String xprvId = "xprv_ckd_bip32_parent";
String childId = "xprv_ckd_bip32_child";
// Cria a chave pai XPrv BIP32 (mainnet)
api.bchainCreateXPrv(TacNDJavaLib.DN_BCHAIN_BIP32_XPRV,
TacNDJavaLib.DN_BCHAIN_VER_BIP32_MAINNET,
0,
xprvId);
System.out.println("Chave pai XPrv BIP32 criada: " + xprvId);
// Deriva uma chave filha hardened (índice 0' = SECURE_INDEX_BASE + 0)
BchainKeyInfo childInfo = api.bchainCreateCkdBip32(
TacNDJavaLib.DN_BCHAIN_VER_BIP32_MAINNET,
TacNDJavaLib.DN_BCHAIN_SECURE_BIP32_INDEX_BASE,
0,
xprvId,
childId);
System.out.printf("Chave filha derivada: %s (profundidade BIP32: %d)%n",
childId, childInfo.getBip32Depth());
// Hash SHA256 de exemplo (32 bytes)
byte[] hash = new byte[] {
(byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x01,
(byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89,
(byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x01,
(byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89,
(byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x01,
(byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89,
(byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x01,
(byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89
};
// Assina o hash com a chave filha usando ECDSA DER e SHA256
byte[] signature = api.bchainSignHash(TacNDJavaLib.DN_BCHAIN_SIG_DER_ECDSA,
TacNDJavaLib.DN_BCHAIN_HASH_SHA256,
hash,
childId);
System.out.println("Assinatura gerada com a chave filha (" + signature.length + " bytes).");
// Recupera a chave pública da chave filha no formato SEC1 comprimido
byte[] pubKey = api.bchainGetPubKey(TacNDJavaLib.DN_BCHAIN_PBK_SEC1_COMP, childId);
System.out.println("Chave pública recuperada (" + pubKey.length + " bytes).");
// Verifica a assinatura
api.bchainVerify(TacNDJavaLib.DN_BCHAIN_SIG_DER_ECDSA,
TacNDJavaLib.DN_BCHAIN_HASH_SHA256,
hash,
signature,
TacNDJavaLib.DN_BCHAIN_PBK_SEC1_COMP,
pubKey);
System.out.println("Assinatura verificada com sucesso.");
// Remove as chaves criadas
api.deleteKeyIfExists(childId);
api.deleteKeyIfExists(xprvId);
System.out.println("Chaves removidas.");
api.closeSession();
}
}