Ejemplo de derivación de una clave secundaria P-256 XPrv (CKD SLIP-0010), firma hash y verificación de firma ECDSA con el módulo de blockchain.
package doxy.examples;
import com.dinamonetworks.BchainKeyInfo;
import com.dinamonetworks.Dinamo;
import br.com.trueaccess.TacException;
import br.com.trueaccess.TacNDJavaLib;
public class BchainCreateCkdP256 {
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_p256_parent";
String childId = "xprv_ckd_p256_child";
api.bchainCreateXPrv(TacNDJavaLib.DN_BCHAIN_BIP32_P256_XPRV,
TacNDJavaLib.DN_BCHAIN_VER_BIP32_MAINNET,
0,
xprvId);
System.out.println("Chave pai P-256 XPrv criada: " + xprvId);
BchainKeyInfo childInfo = api.bchainCreateCkdP256(
TacNDJavaLib.DN_BCHAIN_BIP32_P256_KDT_SLIP10,
TacNDJavaLib.DN_BCHAIN_VER_BIP32_MAINNET,
TacNDJavaLib.DN_BCHAIN_SECURE_BIP32_INDEX_BASE,
0,
xprvId,
childId);
System.out.printf("Chave filha P-256 derivada: %s (profundidade BIP32: %d)%n",
childId, childInfo.getBip32Depth());
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
};
byte[] signature = api.bchainSignHash(TacNDJavaLib.DN_BCHAIN_SIG_DER_ECDSA,
TacNDJavaLib.DN_BCHAIN_HASH_SHA256,
hash,
childId);
System.out.println("Assinatura ECDSA gerada com a chave filha (" + signature.length + " bytes).");
byte[] pubKey = api.bchainGetPubKey(TacNDJavaLib.DN_BCHAIN_PBK_SEC1_COMP, childId);
System.out.println("Chave pública P-256 recuperada (" + pubKey.length + " bytes).");
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.");
api.deleteKeyIfExists(childId);
api.deleteKeyIfExists(xprvId);
System.out.println("Chaves removidas.");
api.closeSession();
}
}