From c1dc507189d0ae0e5e80ba53610e143a6268dffe Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 15 May 2014 17:36:23 +0200 Subject: [PATCH] add hdnode serialization function --- bip32.c | 32 ++++++++++++++++++++++++++++++++ bip32.h | 2 ++ tests.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/bip32.c b/bip32.c index f0de3d18c0..993e404e84 100644 --- a/bip32.c +++ b/bip32.c @@ -118,3 +118,35 @@ void hdnode_fill_public_key(HDNode *node) { ecdsa_get_public_key33(node->private_key, node->public_key); } + +void hdnode_serialize(const HDNode *node, uint32_t version, char use_public, uint8_t *buffer) +{ + uint8_t node_data[82], a[32]; + int i,j; + uint32_t rem, tmp; + const char code[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + write_be(node_data, version); + node_data[4] = node->depth; + write_be(node_data + 5, node->fingerprint); + write_be(node_data + 9, node->child_num); + memcpy(node_data + 13, node->chain_code, 32); + if (use_public) { + memcpy(node_data + 45, node->public_key, 33); + } else { + node_data[45] = 0; + memcpy(node_data + 46, node->private_key, 32); + } + sha256_Raw(node_data, 78, a); + sha256_Raw(a, 32, a); + memcpy(node_data + 78, a, 4); // checksum + for (j = 110; j >= 0; j--) { + rem = node_data[0] % 58; + node_data[0] /= 58; + for (i = 1; i < 82; i++) { + tmp = rem * 24 + node_data[i]; // 2^8 == 4*58 + 24 + node_data[i] = rem * 4 + (tmp / 58); + rem = tmp % 58; + } + buffer[j] = code[rem]; + } +} diff --git a/bip32.h b/bip32.h index 051596d052..7acfb24089 100644 --- a/bip32.h +++ b/bip32.h @@ -28,4 +28,6 @@ void hdnode_fill_public_key(HDNode *node); void hdnode_fill_address(HDNode *node); +void hdnode_serialize(const HDNode *node, uint32_t version, char use_public, uint8_t *buffer); + #endif diff --git a/tests.c b/tests.c index 574a99f13b..deef707040 100644 --- a/tests.c +++ b/tests.c @@ -78,6 +78,7 @@ inline char *tohex(const uint8_t *bin, size_t l) START_TEST(test_bip32_vector_1) { HDNode node; + uint8_t buffer[113]; // init m hdnode_from_seed(fromhex("000102030405060708090a0b0c0d0e0f"), 16, &node); @@ -87,6 +88,10 @@ START_TEST(test_bip32_vector_1) ck_assert_mem_eq(node.chain_code, fromhex("873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508"), 32); ck_assert_mem_eq(node.private_key, fromhex("e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35"), 32); ck_assert_mem_eq(node.public_key, fromhex("0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"); // [Chain m/0'] hdnode_private_ckd_prime(&node, 0); @@ -94,6 +99,10 @@ START_TEST(test_bip32_vector_1) ck_assert_mem_eq(node.chain_code, fromhex("47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141"), 32); ck_assert_mem_eq(node.private_key, fromhex("edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea"), 32); ck_assert_mem_eq(node.public_key, fromhex("035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw"); // [Chain m/0'/1] hdnode_private_ckd(&node, 1); @@ -101,6 +110,10 @@ START_TEST(test_bip32_vector_1) ck_assert_mem_eq(node.chain_code, fromhex("2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19"), 32); ck_assert_mem_eq(node.private_key, fromhex("3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368"), 32); ck_assert_mem_eq(node.public_key, fromhex("03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ"); // [Chain m/0'/1/2'] hdnode_private_ckd_prime(&node, 2); @@ -108,6 +121,10 @@ START_TEST(test_bip32_vector_1) ck_assert_mem_eq(node.chain_code, fromhex("04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f"), 32); ck_assert_mem_eq(node.private_key, fromhex("cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca"), 32); ck_assert_mem_eq(node.public_key, fromhex("0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5"); // [Chain m/0'/1/2'/2] hdnode_private_ckd(&node, 2); @@ -115,6 +132,10 @@ START_TEST(test_bip32_vector_1) ck_assert_mem_eq(node.chain_code, fromhex("cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd"), 32); ck_assert_mem_eq(node.private_key, fromhex("0f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4"), 32); ck_assert_mem_eq(node.public_key, fromhex("02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV"); // [Chain m/0'/1/2'/2/1000000000] hdnode_private_ckd(&node, 1000000000); @@ -122,6 +143,10 @@ START_TEST(test_bip32_vector_1) ck_assert_mem_eq(node.chain_code, fromhex("c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e"), 32); ck_assert_mem_eq(node.private_key, fromhex("471b76e389e528d6de6d816857e012c5455051cad6660850e58372a6c3e6e7c8"), 32); ck_assert_mem_eq(node.public_key, fromhex("022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy"); } END_TEST @@ -129,6 +154,7 @@ END_TEST START_TEST(test_bip32_vector_2) { HDNode node; + uint8_t buffer[113]; int r; // init m @@ -139,6 +165,10 @@ START_TEST(test_bip32_vector_2) ck_assert_mem_eq(node.chain_code, fromhex("60499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689"), 32); ck_assert_mem_eq(node.private_key, fromhex("4b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e"), 32); ck_assert_mem_eq(node.public_key, fromhex("03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB"); // [Chain m/0] r = hdnode_private_ckd(&node, 0); @@ -147,6 +177,10 @@ START_TEST(test_bip32_vector_2) ck_assert_mem_eq(node.chain_code, fromhex("f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c"), 32); ck_assert_mem_eq(node.private_key, fromhex("abe74a98f6c7eabee0428f53798f0ab8aa1bd37873999041703c742f15ac7e1e"), 32); ck_assert_mem_eq(node.public_key, fromhex("02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH"); // [Chain m/0/2147483647'] r = hdnode_private_ckd_prime(&node, 2147483647); @@ -155,6 +189,10 @@ START_TEST(test_bip32_vector_2) ck_assert_mem_eq(node.chain_code, fromhex("be17a268474a6bb9c61e1d720cf6215e2a88c5406c4aee7b38547f585c9a37d9"), 32); ck_assert_mem_eq(node.private_key, fromhex("877c779ad9687164e9c2f4f0f4ff0340814392330693ce95a58fe18fd52e6e93"), 32); ck_assert_mem_eq(node.public_key, fromhex("03c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a"); // [Chain m/0/2147483647'/1] r = hdnode_private_ckd(&node, 1); @@ -163,6 +201,10 @@ START_TEST(test_bip32_vector_2) ck_assert_mem_eq(node.chain_code, fromhex("f366f48f1ea9f2d1d3fe958c95ca84ea18e4c4ddb9366c336c927eb246fb38cb"), 32); ck_assert_mem_eq(node.private_key, fromhex("704addf544a06e5ee4bea37098463c23613da32020d604506da8c0518e1da4b7"), 32); ck_assert_mem_eq(node.public_key, fromhex("03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon"); // [Chain m/0/2147483647'/1/2147483646'] r = hdnode_private_ckd_prime(&node, 2147483646); @@ -171,6 +213,10 @@ START_TEST(test_bip32_vector_2) ck_assert_mem_eq(node.chain_code, fromhex("637807030d55d01f9a0cb3a7839515d796bd07706386a6eddf06cc29a65a0e29"), 32); ck_assert_mem_eq(node.private_key, fromhex("f1c7c871a54a804afe328b4c83a1c33b8e5ff48f5087273f04efa83b247d6a2d"), 32); ck_assert_mem_eq(node.public_key, fromhex("02d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL"); // [Chain m/0/2147483647'/1/2147483646'/2] r = hdnode_private_ckd(&node, 2); @@ -179,6 +225,10 @@ START_TEST(test_bip32_vector_2) ck_assert_mem_eq(node.chain_code, fromhex("9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271"), 32); ck_assert_mem_eq(node.private_key, fromhex("bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23"), 32); ck_assert_mem_eq(node.public_key, fromhex("024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c"), 33); + hdnode_serialize(&node, 0x0488ADE4, 0, buffer); + ck_assert_str_eq(buffer, "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j"); + hdnode_serialize(&node, 0x0488B21E, 1, buffer); + ck_assert_str_eq(buffer, "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt"); // init m hdnode_from_seed(fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node);