Skip to content

Commit

Permalink
[cgo] refs fibercrypto#116 Tried to separate the coin_Uxarray the GoS…
Browse files Browse the repository at this point in the history
…Lice and cli_PasswdByte
  • Loading branch information
Maykel Arias Torres committed Jan 29, 2020
1 parent 60bd7c1 commit c49adec
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 36 deletions.
7 changes: 6 additions & 1 deletion include/cli.cli.go.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 7 additions & 1 deletion include/coin.outputs.go.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 13 additions & 0 deletions lib/cgo/cipher.bip32.bip32.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 33 additions & 0 deletions lib/cgo/cipher.bip32.path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions lib/cgo/cipher.bip44.bip44.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions lib/cgo/tests/check_cipher.address.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/cgo/tests/check_cipher.bitcoin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/cgo/tests/check_coin.outputs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
61 changes: 32 additions & 29 deletions lib/cgo/tests/check_coin.transactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -476,43 +476,43 @@ 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);
ck_assert(SigLen == 0);
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);
registerMemCleanup(keys.data);
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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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));
}
Expand All @@ -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));
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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++;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit c49adec

Please sign in to comment.