CBByteArray * CBVersionChecksumBytesGetString(CBVersionChecksumBytes * self) { if (self->cachedString) { // Return cached string CBRetainObject(self->cachedString); return self->cachedString; } else { // Make string CBByteArrayReverseBytes(CBGetByteArray(self)); // Make this into little-endian CBBigInt bytes; CBBigIntAlloc(&bytes, CBGetByteArray(self)->length); bytes.length = CBGetByteArray(self)->length; memcpy(bytes.data, CBByteArrayGetData(CBGetByteArray(self)), bytes.length); char * string = CBEncodeBase58(&bytes); if (NOT string) return NULL; CBByteArray * str = CBNewByteArrayFromString(string, true, CBGetByteArray(self)->logError); if (NOT str) { free(string); return NULL; } CBByteArrayReverseBytes(CBGetByteArray(self)); // Now the string is got, back to big-endian. if (self->cacheString) { self->cachedString = str; CBRetainObject(str); // Retain for this object. } return str; // No additional retain. Retained from constructor. } }
CBByteArray * CBVersionChecksumBytesGetString(CBVersionChecksumBytes * self){ if (self->cachedString) { // Return cached string CBRetainObject(self->cachedString); return self->cachedString; }else{ // Make string CBByteArrayReverseBytes(CBGetByteArray(self)); // Make this into little-endian char * string = CBEncodeBase58(CBByteArrayGetData(CBGetByteArray(self)),CBGetByteArray(self)->length); CBByteArray * str = CBNewByteArrayWithData((uint8_t *)string, strlen(string), CBGetByteArray(self)->events); CBByteArrayReverseBytes(CBGetByteArray(self)); // Now the string is got, back to big-endian. if (self->cacheString) { self->cachedString = str; CBRetainObject(str); // Retain for this object. } return str; // No additional retain. Retained from constructor. } }
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; }
int main(int argc, char * argv[]){ bool encode = strcmp(argv[1],"-d"); // Read comma sperated inputs from the second argument char * inputs[100], * comma; inputs[0] = argv[2]; int num = 1; for (; (comma = strchr(argv[2], ',')); num++) { inputs[num] = comma + 1; *comma = '\0'; argv[2] = comma + 1; } for (int x = 0; x < num; x++) { if (encode) { // Convert hex string into bytes and then encode base58 string CBByteArray * bytes = CBNewByteArrayFromHex(inputs[x]); CBByteArrayReverseBytes(bytes); CBBigInt bi = {CBByteArrayGetData(bytes), bytes->length, bytes->length}; char * output = CBEncodeBase58(&bi); puts(output); free(output); CBReleaseObject(bytes); }else{ // Decode base58 string and then produce data as a hex string. CBBigInt bi; CBBigIntAlloc(&bi, strlen(inputs[x]) * 100 / 136); CBDecodeBase58(&bi, inputs[x]); printf("0x"); for (uint8_t x = bi.length; x--;) printf("%02x", bi.data[x]); puts(""); free(bi.data); } } }
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; }