From c49adecba1eac193d17769b2931352c53eb626e1 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Wed, 29 Jan 2020 14:56:48 -0500 Subject: [PATCH] [cgo] refs #116 Tried to separate the coin_Uxarray the GoSLice and cli_PasswdByte --- include/cli.cli.go.h | 7 ++- include/coin.outputs.go.h | 8 +++- lib/cgo/cipher.bip32.bip32.go | 13 ++++++ lib/cgo/cipher.bip32.path.go | 33 +++++++++++++ lib/cgo/cipher.bip44.bip44.go | 31 +++++++++++++ lib/cgo/tests/check_cipher.address.c | 4 +- lib/cgo/tests/check_cipher.bitcoin.c | 4 +- lib/cgo/tests/check_coin.outputs.c | 2 +- lib/cgo/tests/check_coin.transactions.c | 61 +++++++++++++------------ 9 files changed, 127 insertions(+), 36 deletions(-) diff --git a/include/cli.cli.go.h b/include/cli.cli.go.h index 04e478290..24b0d713d 100644 --- a/include/cli.cli.go.h +++ b/include/cli.cli.go.h @@ -12,6 +12,11 @@ typedef struct { GoInt32_ _unnamed; } cli__WalletSaveError; typedef GoInterface_ cli__PasswordReader; -typedef GoSlice_ cli__PasswordFromBytes; typedef struct { } cli__PasswordFromTerm; + +typedef struct { + void* data; + GoInt_ len; + GoInt_ cap; +} cli__PasswordFromBytes; \ No newline at end of file diff --git a/include/coin.outputs.go.h b/include/coin.outputs.go.h index 6a6d5c5a4..8fe82f51d 100644 --- a/include/coin.outputs.go.h +++ b/include/coin.outputs.go.h @@ -9,9 +9,15 @@ typedef struct { GoUint64_ Hours; } coin__UxBody; typedef GoMap_ coin__UxHashSet; -typedef GoSlice_ coin__UxArray; typedef GoMap_ coin__AddressUxOuts; typedef struct { coin__UxHead Head; coin__UxBody Body; } coin__UxOut; + +typedef struct { + void* data; + GoInt_ len; + GoInt_ cap; + GoInt_ a; +} coin__UxArray; \ No newline at end of file diff --git a/lib/cgo/cipher.bip32.bip32.go b/lib/cgo/cipher.bip32.bip32.go index 0fcc6fe38..3e0def216 100644 --- a/lib/cgo/cipher.bip32.bip32.go +++ b/lib/cgo/cipher.bip32.bip32.go @@ -466,3 +466,16 @@ func SKY_bip32_PublicKey_GetParentFingerprint(_pk C.PublicKey__Handle, _arg0 *C. copyToGoSlice(reflect.ValueOf(__arg0), _arg0) return } + +//export SKY_bip32_PrivateKey_Publickey +func SKY_bip32_PrivateKey_Publickey(_pk C.PrivateKey__Handle, _pp *C.PublicKey__Handle) (___error_code uint32) { + pk, okpk := lookupPrivateKeyHandle(_pk) + if !okpk { + ___error_code = SKY_BAD_HANDLE + return + } + + pp := pk.PublicKey() + *_pp = registerPublicKeyHandle(pp) + return +} \ No newline at end of file diff --git a/lib/cgo/cipher.bip32.path.go b/lib/cgo/cipher.bip32.path.go index 7c44f474c..c0edeec29 100644 --- a/lib/cgo/cipher.bip32.path.go +++ b/lib/cgo/cipher.bip32.path.go @@ -35,3 +35,36 @@ func SKY_bip32_ParsePath(_p string, _arg1 *C.Path__Handle) (____error_code uint3 } return } + +// Helpers + +//export SKY_bip32_Path_Count +func SKY_bip32_Path_Count(handle C.Path__Handle, _arg0 *int) (____error_code uint32) { + p, okp := lookupPathHandle(handle) + + if !okp { + ____error_code = SKY_BAD_HANDLE + return + } + + *_arg0 = len(p.Elements) + return +} + +//export SKY_bip32_Path_GetElements +func SKY_bip32_Path_GetElements(handle C.Path__Handle, post int, _arg0 *C.bip32__PathNode) (____error_code uint32) { + p, okp := lookupPathHandle(handle) + + if !okp { + ____error_code = SKY_BAD_HANDLE + return + } + + if len(p.Elements) <= post { + ____error_code = SKY_BAD_HANDLE + return + } + + *_arg0 = *(*C.bip32__PathNode)(unsafe.Pointer(&p.Elements[post])) + return +} diff --git a/lib/cgo/cipher.bip44.bip44.go b/lib/cgo/cipher.bip44.bip44.go index 6735ecec1..0c8ad355d 100644 --- a/lib/cgo/cipher.bip44.bip44.go +++ b/lib/cgo/cipher.bip44.bip44.go @@ -73,3 +73,34 @@ func SKY_bip44_Account_Change(_a *C.Account__Handle, _arg0 *C.PrivateKey__Handle } return } + +// Helpers + +//export SKY_bip44_Account_String +func SKY_bip44_Account_String(_a C.Account__Handle, _arg0 *C.GoString_) (___err_code uint32) { + + a, oka := lookupAccountHandle(_a) + if !oka { + + ___err_code = SKY_BAD_HANDLE + return + } + + __arg0 := a.String() + copyString(__arg0, _arg0) + return +} + +//export SKY_bip44_Account_GetPrivateKey +func SKY_bip44_Account_GetPrivateKey(_a C.Account__Handle, _arg0 *C.PrivateKey__Handle) (___err_code uint32) { + + a, oka := lookupAccountHandle(_a) + if !oka { + + ___err_code = SKY_BAD_HANDLE + return + } + + *_arg0 = registerPrivateKeyHandle(a.PrivateKey) + return +} diff --git a/lib/cgo/tests/check_cipher.address.c b/lib/cgo/tests/check_cipher.address.c index 3929d8618..e1451e09f 100644 --- a/lib/cgo/tests/check_cipher.address.c +++ b/lib/cgo/tests/check_cipher.address.c @@ -161,7 +161,7 @@ START_TEST(TestDecodeBase58Address) char bufferHead[1024]; GoString_ h = {bufferHead, 0}; b.len = (GoInt)(len_b / 2); - errorcode = SKY_base58_Hex2Base58(b, &h); + errorcode = SKY_base58_Encode(b, &h); ck_assert(errorcode == SKY_OK); char bufferHeadTmp[1024]; GoString tmph = {bufferHeadTmp, 0}; @@ -170,7 +170,7 @@ START_TEST(TestDecodeBase58Address) errorcode = SKY_cipher_DecodeBase58Address(tmph, &addr); ck_assert_msg(errorcode == SKY_ErrAddressInvalidLength, "Fail %X", errorcode); b.len = len_b; - errorcode = SKY_base58_Hex2Base58(b, &h); + errorcode = SKY_base58_Encode(b, &h); ck_assert(errorcode == SKY_OK); tmph.n = h.n; tmph.p = h.p; diff --git a/lib/cgo/tests/check_cipher.bitcoin.c b/lib/cgo/tests/check_cipher.bitcoin.c index 206c9496c..81b59dbb2 100644 --- a/lib/cgo/tests/check_cipher.bitcoin.c +++ b/lib/cgo/tests/check_cipher.bitcoin.c @@ -88,7 +88,7 @@ START_TEST(TestDecodeBase58BitcoinAddress) b.len = (GoInt_)(len_b / 2); GoUint8_ buffer_h[1024]; GoString h = {buffer_h, 0}; - err = SKY_base58_Hex2Base58(b, &h_tmp); + err = SKY_base58_Encode(b, &h_tmp); ck_assert_int_eq(err, SKY_OK); h.n = h_tmp.n; h.p = h_tmp.p; @@ -99,7 +99,7 @@ START_TEST(TestDecodeBase58BitcoinAddress) SKY_cipher_BitcoinAddress_Bytes(&a, &b_temp); err = copyGoSlice_toGoSlice(&b, &b_temp, b_temp.len); ck_assert_int_eq(err, SKY_OK); - err = SKY_base58_Hex2Base58(b, &h_tmp); + err = SKY_base58_Encode(b, &h_tmp); ck_assert_int_eq(err, SKY_OK); h.n = h_tmp.n; h.p = h_tmp.p; diff --git a/lib/cgo/tests/check_coin.outputs.c b/lib/cgo/tests/check_coin.outputs.c index 606b14d6e..8666f378f 100644 --- a/lib/cgo/tests/check_coin.outputs.c +++ b/lib/cgo/tests/check_coin.outputs.c @@ -654,7 +654,7 @@ START_TEST(TestAddressUxOutsAdd) result = SKY_coin_AddressUxOuts_Set(h2, &(pData + 2)->Body.Address, &ux6); ck_assert_msg(result == SKY_OK, "SKY_coin_AddressUxOut_Set failed"); - result = SKY_coin_AddressUxOuts_Add(h1, h2, &h3); + result = SKY_coin_AddressUxOuts_Add(&h1, &h2, &h3); ck_assert_msg(result == SKY_OK, "SKY_coin_AddressUxOuts_Add failed"); registerHandleClose(h3); diff --git a/lib/cgo/tests/check_coin.transactions.c b/lib/cgo/tests/check_coin.transactions.c index c2ed90f98..416e412d2 100644 --- a/lib/cgo/tests/check_coin.transactions.c +++ b/lib/cgo/tests/check_coin.transactions.c @@ -465,7 +465,7 @@ START_TEST(TestTransactionSignInputs) keys.data = &seckey; keys.len = 1; keys.cap = 1; - result = SKY_coin_Transaction_SignInputs(handle, keys); + result = SKY_coin_Transaction_SignInputs(&handle, keys); ck_assert(result == SKY_ERROR); // Panics if not enough keys @@ -476,16 +476,16 @@ START_TEST(TestTransactionSignInputs) ck_assert(result == SKY_OK); result = SKY_coin_UxOut_Hash(&ux, &hash); ck_assert(result == SKY_OK); - result = SKY_coin_Transaction_PushInput(handle, &hash); + result = SKY_coin_Transaction_PushInput(&handle, &hash); ck_assert(result == SKY_OK); result = makeUxOutWithSecret(&ux2, &seckey2); ck_assert(result == SKY_OK); result = SKY_coin_UxOut_Hash(&ux2, &hash2); ck_assert(result == SKY_OK); - result = SKY_coin_Transaction_PushInput(handle, &hash2); + result = SKY_coin_Transaction_PushInput(&handle, &hash2); ck_assert(result == SKY_OK); makeAddress(&addr); - result = SKY_coin_Transaction_PushOutput(handle, &addr, 40, 80); + result = SKY_coin_Transaction_PushOutput(&handle, &addr, 40, 80); ck_assert(result == SKY_OK); GoInt SigLen; SKY_coin_Transaction_GetSignaturesCount(handle, &SigLen); @@ -493,14 +493,14 @@ START_TEST(TestTransactionSignInputs) keys.data = &seckey; keys.len = 1; keys.cap = 1; - result = SKY_coin_Transaction_SignInputs(handle, keys); + result = SKY_coin_Transaction_SignInputs(&handle, keys); ck_assert(result == SKY_ERROR); SKY_coin_Transaction_GetSignaturesCount(handle, &SigLen); ck_assert(SigLen == 0); ck_assert(SigLen == 0); // Valid signing - result = SKY_coin_Transaction_HashInner(handle, &hash); + result = SKY_coin_Transaction_HashInner(&handle, &hash); ck_assert(result == SKY_OK); keys.data = malloc(2 * sizeof(cipher__SecKey)); ck_assert(keys.data != NULL); @@ -508,11 +508,11 @@ START_TEST(TestTransactionSignInputs) keys.len = keys.cap = 2; memcpy(keys.data, &seckey, sizeof(cipher__SecKey)); memcpy(((cipher__SecKey*)keys.data) + 1, &seckey2, sizeof(cipher__SecKey)); - result = SKY_coin_Transaction_SignInputs(handle, keys); + result = SKY_coin_Transaction_SignInputs(&handle, keys); ck_assert(result == SKY_OK); SKY_coin_Transaction_GetSignaturesCount(handle, &SigLen); ck_assert(SigLen == 2); - result = SKY_coin_Transaction_HashInner(handle, &hash2); + result = SKY_coin_Transaction_HashInner(&handle, &hash2); ck_assert(result == SKY_OK); ck_assert(isU8Eq(hash2, hash, sizeof(cipher__SHA256)) == 0); @@ -569,9 +569,9 @@ START_TEST(TestTransactionHashInner) err = SKY_coin_Transaction_GetInputAt(txn2, 0, &sha_tmp2); ck_assert_int_eq(err, SKY_OK); ck_assert(isSHA256Eq(&sha_tmp2, &sha_tmp)); - err = SKY_coin_Transaction_HashInner(txn, &sha_tmp); + err = SKY_coin_Transaction_HashInner(&txn, &sha_tmp); ck_assert_int_eq(err, SKY_OK); - err = SKY_coin_Transaction_HashInner(txn2, &sha_tmp2); + err = SKY_coin_Transaction_HashInner(&txn2, &sha_tmp2); ck_assert_int_eq(err, SKY_OK); ck_assert(isSHA256Eq(&sha_tmp2, &sha_tmp) == 0); @@ -589,9 +589,9 @@ START_TEST(TestTransactionHashInner) err = SKY_coin_Transaction_GetOutputAt(txn2, 0, &out_tmp); ck_assert_int_eq(err, SKY_OK); ck_assert(isAddressEq(&out_tmp.Address, &a)); - err = SKY_coin_Transaction_HashInner(txn, &sha_tmp); + err = SKY_coin_Transaction_HashInner(&txn, &sha_tmp); ck_assert_int_eq(err, SKY_OK); - err = SKY_coin_Transaction_HashInner(txn2, &sha_tmp2); + err = SKY_coin_Transaction_HashInner(&txn2, &sha_tmp2); ck_assert_int_eq(err, SKY_OK); ck_assert(isSHA256Eq(&sha_tmp2, &sha_tmp) == 0); @@ -603,9 +603,9 @@ START_TEST(TestTransactionHashInner) cipher__Sig nullsig; err = SKY_coin_Transaction_PushSignature(txn, &nullsig); ck_assert_int_eq(err, SKY_OK); - err = SKY_coin_Transaction_HashInner(txn, &sha_tmp); + err = SKY_coin_Transaction_HashInner(&txn, &sha_tmp); ck_assert_int_eq(err, SKY_OK); - err = SKY_coin_Transaction_HashInner(txn2, &sha_tmp2); + err = SKY_coin_Transaction_HashInner(&txn2, &sha_tmp2); ck_assert_int_eq(err, SKY_OK); ck_assert(isSHA256Eq(&sha_tmp2, &sha_tmp)); } @@ -618,10 +618,13 @@ START_TEST(TestTransactionSerialization) Transaction__Handle handle = 0; makeTransaction(&handle); unsigned char buffer[1024]; + unsigned char buffer_tmp[1024]; GoSlice d = {buffer, 0, 1024}; - result = SKY_coin_Transaction_Serialize(&handle, &d); + coin__UxArray d_tmp = {buffer_tmp, 0, 1024}; + result = SKY_coin_Transaction_Serialize(&handle, &d_tmp); ck_assert(result == SKY_OK); Transaction__Handle handle2 = 0; + copycoin_UxArraytoGoSlice(&d, &d_tmp, sizeof(d_tmp)); result = SKY_coin_DeserializeTransaction(d, &handle2); ck_assert(result == SKY_OK); ck_assert(isTransactionHandleEq(&handle, &handle2)); @@ -636,25 +639,25 @@ START_TEST(TestTransactionOutputHours) cipher__Address addr; makeAddress(&addr); int result; - result = SKY_coin_Transaction_PushOutput(handle, &addr, 1000000, 100); + result = SKY_coin_Transaction_PushOutput(&handle, &addr, 1000000, 100); ck_assert(result == SKY_OK); makeAddress(&addr); - result = SKY_coin_Transaction_PushOutput(handle, &addr, 1000000, 200); + result = SKY_coin_Transaction_PushOutput(&handle, &addr, 1000000, 200); ck_assert(result == SKY_OK); makeAddress(&addr); - result = SKY_coin_Transaction_PushOutput(handle, &addr, 1000000, 500); + result = SKY_coin_Transaction_PushOutput(&handle, &addr, 1000000, 500); ck_assert(result == SKY_OK); makeAddress(&addr); - result = SKY_coin_Transaction_PushOutput(handle, &addr, 1000000, 0); + result = SKY_coin_Transaction_PushOutput(&handle, &addr, 1000000, 0); ck_assert(result == SKY_OK); GoUint64 hours; - result = SKY_coin_Transaction_OutputHours(handle, &hours); + result = SKY_coin_Transaction_OutputHours(&handle, &hours); ck_assert(result == SKY_OK); ck_assert(hours == 800); makeAddress(&addr); - result = SKY_coin_Transaction_PushOutput(handle, &addr, 1000000, + result = SKY_coin_Transaction_PushOutput(&handle, &addr, 1000000, 0xFFFFFFFFFFFFFFFF - 700); - result = SKY_coin_Transaction_OutputHours(handle, &hours); + result = SKY_coin_Transaction_OutputHours(&handle, &hours); ck_assert(result == SKY_ERROR); } END_TEST @@ -668,7 +671,7 @@ START_TEST(TestTransactionsHashes) result = makeTransactions(4, &hTxns); ck_assert(result == SKY_OK); - result = SKY_coin_Transactions_Hashes(hTxns, &hashes); + result = SKY_coin_Transactions_Hashes(&hTxns, &hashes); ck_assert_msg(result == SKY_OK, "SKY_coin_Transactions_Hashes failed"); registerMemCleanup(hashes.data); ck_assert(hashes.len == 4); @@ -678,7 +681,7 @@ START_TEST(TestTransactionsHashes) Transaction__Handle handle = 0; result = SKY_coin_Transactions_GetAt(hTxns, i, &handle); ck_assert(result == SKY_OK); - result = SKY_coin_Transaction_Hash(handle, &hash); + result = SKY_coin_Transaction_Hash(&handle, &hash); ck_assert_msg(result == SKY_OK, "SKY_coin_Transaction_Hash failed"); ck_assert(isU8Eq(*ph, hash, sizeof(cipher__SHA256))); ph++; @@ -703,11 +706,11 @@ START_TEST(TestTransactionsTruncateBytesTo) result = SKY_coin_Transactions_GetAt(h1, i, &handle); registerHandleClose(handle); ck_assert(result == SKY_OK); - result = SKY_coin_Transaction_Size(handle, &size); + result = SKY_coin_Transaction_Size(&handle, &size); trunc += size; ck_assert_msg(result == SKY_OK, "SKY_coin_Transaction_Size failed"); } - result = SKY_coin_Transactions_TruncateBytesTo(h1, trunc, &h2); + result = SKY_coin_Transactions_TruncateBytesTo(&h1, trunc, &h2); ck_assert_msg(result == SKY_OK, "SKY_coin_Transactions_TruncateBytesTo failed"); registerHandleClose(h2); @@ -716,12 +719,12 @@ START_TEST(TestTransactionsTruncateBytesTo) result = SKY_coin_Transactions_Length(h2, &length2); ck_assert(result == SKY_OK); ck_assert(length2 == length / 2); - result = SKY_coin_Transactions_Size(h2, &size); + result = SKY_coin_Transactions_Size(&h2, &size); ck_assert_msg(result == SKY_OK, "SKY_coin_Transactions_Size failed"); ck_assert(trunc == size); trunc++; - result = SKY_coin_Transactions_TruncateBytesTo(h1, trunc, &h2); + result = SKY_coin_Transactions_TruncateBytesTo(&h1, trunc, &h2); ck_assert_msg(result == SKY_OK, "SKY_coin_Transactions_TruncateBytesTo failed"); registerHandleClose(h2); @@ -730,7 +733,7 @@ START_TEST(TestTransactionsTruncateBytesTo) result = SKY_coin_Transactions_Length(h2, &length2); ck_assert(result == SKY_OK); ck_assert(length2 == length / 2); - result = SKY_coin_Transactions_Size(h2, &size); + result = SKY_coin_Transactions_Size(&h2, &size); ck_assert_msg(result == SKY_OK, "SKY_coin_Transactions_Size failed"); ck_assert(trunc - 1 == size); }