bool CBInitVersionChecksumBytesFromString(CBVersionChecksumBytes * self,CBByteArray * string,bool cacheString,void (*logError)(char *,...)) { // Cache string if needed if (cacheString) { self->cachedString = string; CBRetainObject(string); } else self->cachedString = NULL; self->cacheString = cacheString; // Get bytes from string conversion CBBigInt bytes; CBBigIntAlloc(&bytes, 25); // 25 is the number of bytes for bitcoin addresses. if (NOT CBDecodeBase58Checked(&bytes, (char *)CBByteArrayGetData(string), logError)) return false; // Take over the bytes with the CBByteArray if (NOT CBInitByteArrayWithData(CBGetByteArray(self), bytes.data, bytes.length, logError)) return false; CBByteArrayReverseBytes(CBGetByteArray(self)); // CBBigInt is in little-endian. Conversion needed to make bitcoin address the right way. return true; }
bool CBInitVersionChecksumBytesFromString(CBVersionChecksumBytes * self,CBByteArray * string,bool cacheString,CBEvents * events){ // Cache string if needed if (cacheString) { self->cachedString = string; CBRetainObject(string); }else self->cachedString = NULL; self->cacheString = cacheString; // Get bytes from string conversion CBBigInt bytes = CBDecodeBase58Checked((char *)CBByteArrayGetData(string), events); if (bytes.length == 1) { return false; } // Take over the bytes with the CBByteArray if (NOT CBInitByteArrayWithData(CBGetByteArray(self), bytes.data, bytes.length, events)) return false; CBByteArrayReverseBytes(CBGetByteArray(self)); // CBBigInt is in little-endian. Conversion needed to make bitcoin address the right way. return true; }
int main(){ unsigned int s = (unsigned int)time(NULL); printf("Session = %ui\n",s); srand(s); // Test checked decode CBBigInt bi; CBBigIntAlloc(&bi, 29); CBDecodeBase58Checked(&bi, "1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9", onErrorReceived); // Valid printf("END VALID\n"); CBDecodeBase58Checked(&bi, "1qBd3Y9D8HhzA4bYSKgkPw8LsX4wCcbqBX", onErrorReceived); // Invalid // ??? Test for: // c5f88541634fb7bade5f94ff671d1febdcbda116d2da779038ed767989 bi.data[0] = 0xc5; bi.data[1] = 0xf8; bi.data[2] = 0x85; bi.data[3] = 0x41; bi.data[4] = 0x63; bi.data[5] = 0x4f; bi.data[6] = 0xb7; bi.data[7] = 0xba; bi.data[8] = 0xde; bi.data[9] = 0x5f; bi.data[10] = 0x94; bi.data[11] = 0xff; bi.data[12] = 0x67; bi.data[13] = 0x1d; bi.data[14] = 0x1f; bi.data[15] = 0xeb; bi.data[16] = 0xdc; bi.data[17] = 0xbd; bi.data[18] = 0xa1; bi.data[19] = 0x16; bi.data[20] = 0xd2; bi.data[21] = 0xda; bi.data[22] = 0x77; bi.data[23] = 0x90; bi.data[24] = 0x38; bi.data[25] = 0xed; bi.data[26] = 0x76; bi.data[27] = 0x79; bi.data[28] = 0x89; bi.length = 29; char * str = CBEncodeBase58(&bi); printf("%s\n",str); if (strcmp(str, "7EyVQVmCjB3siBN8DdtuG3ws5jW9xsnT25vbt5eU")) { printf("7EyVQVmCjB3siBN8DdtuG3ws5jW9xsnT25vbt5eU FAIL\n"); return 1; } free(str); unsigned char * verify = malloc(29); for (int x = 0; x < 10000; x++) { for (int y = 0; y < 29; y++) { bi.data[y] = rand(); verify[y] = bi.data[y]; } bi.length = 29; printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",verify[y]); } str = CBEncodeBase58(&bi); printf(" -> %s -> \n",str); CBDecodeBase58(&bi,str); free(str); printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",bi.data[y]); if (bi.data[y] != verify[y]) { printf(" = FAIL\n"); return 1; } } printf(" = OK\n"); } free(bi.data); return 0; }
int main(){ unsigned int s = (unsigned int)time(NULL); printf("Session = %ui\n",s); srand(s); // Test checked decode CBEvents events; events.onErrorReceived = err; CBDependencies d; d.sha256 = sha256; CBDecodeBase58Checked("1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9", &events, &d); // Valid printf("END VALID\n"); CBDecodeBase58Checked("1qBd3Y9D8HhzA4bYSKgkPw8LsX4wCcbqBX", &events, &d); // Invalid unsigned char * test = malloc(29); // ??? Test for: // c5f88541634fb7bade5f94ff671d1febdcbda116d2da779038ed767989 // 7EyVQVmCjB3siBN8DdtuG3ws5jW9xsnT25vbt5eU = CORRECT // 7EyVQVmCjB3siBN8DdtuG3ws64y9xsnT25vbt5eU = FAILURE test[0] = 0xc5; test[1] = 0xf8; test[2] = 0x85; test[3] = 0x41; test[4] = 0x63; test[5] = 0x4f; test[6] = 0xb7; test[7] = 0xba; test[8] = 0xde; test[9] = 0x5f; test[10] = 0x94; test[11] = 0xff; test[12] = 0x67; test[13] = 0x1d; test[14] = 0x1f; test[15] = 0xeb; test[16] = 0xdc; test[17] = 0xbd; test[18] = 0xa1; test[19] = 0x16; test[20] = 0xd2; test[21] = 0xda; test[22] = 0x77; test[23] = 0x90; test[24] = 0x38; test[25] = 0xed; test[26] = 0x76; test[27] = 0x79; test[28] = 0x89; char * str = CBEncodeBase58(test,29); printf("%s\n",str); free(str); unsigned char * verify = malloc(29); for (int x = 0; x < 10000; x++) { for (int y = 0; y < 29; y++) { test[y] = rand(); verify[y] = test[y]; } printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",verify[y]); } str = CBEncodeBase58(test,29); printf(" -> %s -> \n",str); CBBigInt bi = CBDecodeBase58(str); free(str); printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",bi.data[y]); if (bi.data[y] != verify[y]) { printf(" = FAIL\n"); return 1; } } printf(" = OK\n"); free(bi.data); } return 0; }