uint32_t CBTransactionOutputSerialise(CBTransactionOutput * self){ CBByteArray * bytes = CBGetMessage(self)->bytes; if (NOT bytes) { CBLogError("Attempting to serialise a CBTransactionInput with no bytes."); return 0; } if (NOT self->scriptObject){ CBLogError("Attempting to serialise a CBTransactionOutput without scriptObject."); return 0; } CBVarInt scriptLen = CBVarIntFromUInt64(CBGetByteArray(self->scriptObject)->length); uint32_t reqLen = 8 + scriptLen.size + CBGetByteArray(self->scriptObject)->length; if (bytes->length < reqLen) { CBLogError("Attempting to serialise a CBTransactionOutput with less bytes than required. %i < %i", bytes->length, reqLen); return 0; } // Serialise data into the CBByteArray and rereference objects to this CBByteArray to save memory. CBByteArraySetInt64(bytes, 0, self->value); CBVarIntEncode(bytes, 8, scriptLen); CBByteArrayCopyByteArray(bytes, 8 + scriptLen.size, CBGetByteArray(self->scriptObject)); CBByteArrayChangeReference(CBGetByteArray(self->scriptObject), bytes, 8 + scriptLen.size); // Ensure length is correct bytes->length = reqLen; // Is serialised. CBGetMessage(self)->serialised = true; return reqLen; }
uint8_t CBNetworkAddressSerialise(CBNetworkAddress * self, bool timestamp){ CBByteArray * bytes = CBGetMessage(self)->bytes; if (! bytes) { CBLogError("Attempting to serialise a CBNetworkAddress with no bytes."); return 0; } if (bytes->length < 26 + timestamp * 4) { CBLogError("Attempting to serialise a CBNetworkAddress with less bytes than required."); return 0; } uint8_t cursor; if (timestamp) { CBByteArraySetInt32(bytes, 0, (uint32_t)(self->lastSeen - self->penalty)); cursor = 4; }else cursor = 0; CBByteArraySetInt64(bytes, cursor, self->services); cursor += 8; CBByteArrayCopyByteArray(bytes, cursor, self->ip); CBByteArrayChangeReference(self->ip, bytes, cursor); cursor += 16; CBByteArraySetPort(bytes, cursor, self->port); bytes->length = cursor + 2; CBGetMessage(self)->serialised = true; return cursor + 2; }
int CBPingPongSerialise(CBPingPong * self) { CBByteArray * bytes = CBGetMessage(self)->bytes; if (! bytes) { CBLogError("Attempting to serialise a CBPingPong with no bytes."); return 0; } if (bytes->length < 8) { CBLogError("Attempting to serialise a CBPingPong with less than 8 bytes."); return 0; } CBByteArraySetInt64(bytes, 0, self->ID); bytes->length = 8; CBGetMessage(self)->serialised = true; return 8; }
CBGetHashReturn CBTransactionGetInputHashForSignature(void * vself, CBByteArray * prevOutSubScript, uint32_t input, CBSignType signType, uint8_t * hash){ CBTransaction * self= vself; if (self->inputNum < input + 1) { CBLogError("Receiving transaction hash to sign cannot be done for because the input index goes past the number of inputs."); return CB_TX_HASH_BAD; } uint8_t last5Bits = (signType & 0x1f); // For some reason this is what the C++ client does. CBVarInt prevOutputSubScriptVarInt = CBVarIntFromUInt64(prevOutSubScript->length); uint32_t sizeOfData = 12 + prevOutSubScript->length + prevOutputSubScriptVarInt.size; // Version, lock time and the sign type make up 12 bytes. if (signType & CB_SIGHASH_ANYONECANPAY) { sizeOfData += 41; // Just this one input. 32 bytes for outPointerHash, 4 for outPointerIndex, 4 for sequence and one for the *inputNum* var int }else{ sizeOfData += CBVarIntSizeOf(self->inputNum) + self->inputNum * 41 - 1; // All inputs with 1 byte var int except one. } if (last5Bits == CB_SIGHASH_NONE){ sizeOfData++; // Just for the CBVarInt and no outputs. }else if ((signType & 0x1f) == CB_SIGHASH_SINGLE){ if (self->outputNum < input + 1) { CBLogError("Receiving transaction hash to sign cannot be done for CB_SIGHASH_SINGLE because there are not enough outputs."); return CB_TX_HASH_BAD; } sizeOfData += CBVarIntSizeOf(input + 1) + input * 9; // For outputs up to the input index // The size for the output at the input index. uint32_t len = CBGetByteArray(self->outputs[input]->scriptObject)->length; sizeOfData += 8 + CBVarIntSizeOf(len) + len; }else{ // All outputs. Default to SIGHASH_ALL sizeOfData += CBVarIntSizeOf(self->outputNum); for (uint32_t x = 0; x < self->outputNum; x++) { uint32_t len = CBGetByteArray(self->outputs[x]->scriptObject)->length; sizeOfData += 8 + CBVarIntSizeOf(len) + len; } } CBByteArray * data = CBNewByteArrayOfSize(sizeOfData); if (NOT data) return CB_TX_HASH_ERR; CBByteArraySetInt32(data, 0, self->version); // Copy input data. Scripts are not copied for the inputs. uint32_t cursor; if (signType & CB_SIGHASH_ANYONECANPAY) { CBVarIntEncode(data, 4, CBVarIntFromUInt64(1)); // Only the input the signature is for. CBByteArrayCopyByteArray(data, 5, self->inputs[input]->prevOut.hash); CBByteArraySetInt32(data, 37, self->inputs[input]->prevOut.index); // Add prevOutSubScript CBVarIntEncode(data, 41, prevOutputSubScriptVarInt); cursor = 41 + prevOutputSubScriptVarInt.size; CBByteArrayCopyByteArray(data, cursor, prevOutSubScript); cursor += prevOutSubScript->length; CBByteArraySetInt32(data, cursor, self->inputs[input]->sequence); cursor += 4; }else{ CBVarInt inputNum = CBVarIntFromUInt64(self->inputNum); CBVarIntEncode(data, 4, inputNum); cursor = 4 + inputNum.size; for (uint32_t x = 0; x < self->inputNum; x++) { CBByteArrayCopyByteArray(data, cursor, self->inputs[x]->prevOut.hash); cursor += 32; CBByteArraySetInt32(data, cursor, self->inputs[x]->prevOut.index); cursor += 4; // Add prevOutSubScript if the input is for the signature. if (x == input) { CBVarIntEncode(data, cursor, prevOutputSubScriptVarInt); cursor += prevOutputSubScriptVarInt.size; CBByteArrayCopyByteArray(data, cursor, prevOutSubScript); cursor += prevOutSubScript->length; }else{ CBVarIntEncode(data, cursor, CBVarIntFromUInt64(0)); cursor++; } if ((signType == CB_SIGHASH_NONE || signType == CB_SIGHASH_SINGLE) && x != input) { CBByteArraySetInt32(data, cursor, 0); } else // SIGHASH_ALL or input index for signing sequence CBByteArraySetInt32(data, cursor, self->inputs[x]->sequence); cursor += 4; } } // Copy output data if (last5Bits == CB_SIGHASH_NONE){ CBVarInt varInt = CBVarIntFromUInt64(0); CBVarIntEncode(data, cursor, varInt); cursor++; }else if (last5Bits == CB_SIGHASH_SINGLE){ CBVarInt varInt = CBVarIntFromUInt64(input + 1); CBVarIntEncode(data, cursor, varInt); cursor += varInt.size; for (uint32_t x = 0; x < input; x++) { CBByteArraySetInt64(data, cursor, CB_OUTPUT_VALUE_MINUS_ONE); cursor += 8; CBVarIntEncode(data, cursor, CBVarIntFromUInt64(0)); cursor++; } CBByteArraySetInt64(data, cursor, self->outputs[input]->value); cursor += 8; varInt = CBVarIntFromUInt64(CBGetByteArray(self->outputs[input]->scriptObject)->length); CBVarIntEncode(data, cursor, varInt); cursor += varInt.size; CBByteArrayCopyByteArray(data, cursor, CBGetByteArray(self->outputs[input]->scriptObject)); cursor += varInt.val; }else{ // SIGHASH_ALL CBVarInt varInt = CBVarIntFromUInt64(self->outputNum); CBVarIntEncode(data, cursor, varInt); cursor += varInt.size; for (uint32_t x = 0; x < self->outputNum; x++) { CBByteArraySetInt64(data, cursor, self->outputs[x]->value); cursor += 8; varInt = CBVarIntFromUInt64(CBGetByteArray(self->outputs[x]->scriptObject)->length); CBVarIntEncode(data, cursor, varInt); cursor += varInt.size; CBByteArrayCopyByteArray(data, cursor, CBGetByteArray(self->outputs[x]->scriptObject)); cursor += varInt.val; } } // Set lockTime CBByteArraySetInt32(data, cursor, self->lockTime); CBByteArraySetInt32(data, cursor + 4, signType); assert(sizeOfData == cursor + 8); // Must always be like this uint8_t firstHash[32]; CBSha256(CBByteArrayGetData(data), sizeOfData, firstHash); CBSha256(firstHash, 32, hash); return CB_TX_HASH_OK; }
bool CBAlertSerialisePayload(CBAlert * self) { CBByteArray * bytes = CBGetMessage(self)->bytes; if (! bytes) { CBLogError("Attempting to serialise a CBAlert with no bytes."); return false; } // Create varints and calculate length whilst at it. CBVarInt setCancelLen = CBVarIntFromUInt64(self->setCancelNum); CBVarInt userAgentLen = CBVarIntFromUInt64(self->userAgentNum); CBVarInt hiddenCommentLen = CBVarIntFromUInt64(self->hiddenComment ? self->hiddenComment->length : 0); CBVarInt displayedCommentLen = CBVarIntFromUInt64(self->displayedComment ? self->displayedComment->length : 0); CBVarInt reservedLen = CBVarIntFromUInt64(self->reserved ? self->reserved->length : 0); int length = 40 + setCancelLen.size + setCancelLen.val * 4 + userAgentLen.size + hiddenCommentLen.size + hiddenCommentLen.val + displayedCommentLen.size + displayedCommentLen.val + reservedLen.size + reservedLen.val; // Add user agents to length for (int x = 0; x < self->userAgentNum; x++) length += self->userAgents[x]->length + CBVarIntSizeOf(self->userAgents[x]->length); // So far length is for payload so make payload var int. CBVarInt payloadLen = CBVarIntFromUInt64(length); // Add the length of this var int. length += payloadLen.size; // Check length, with at least one byte for signature (could be 0 varint) if (bytes->length < length + 1) { CBLogError("Attempting to serialise a CBAlert with less bytes than required for the payload and a signature var int."); return false; } // Serialise the payload section. CBByteArraySetVarInt(bytes, 0, payloadLen); int cursor = payloadLen.size; CBByteArraySetInt32(bytes, cursor, self->version); cursor += 4; CBByteArraySetInt64(bytes, cursor, self->relayUntil); cursor += 8; CBByteArraySetInt64(bytes, cursor, self->expiration); cursor += 8; CBByteArraySetInt32(bytes, cursor, self->ID); cursor += 4; CBByteArraySetInt32(bytes, cursor, self->cancel); cursor += 4; // Cancel set CBByteArraySetVarInt(bytes, cursor, setCancelLen); cursor += setCancelLen.size; for (int x = 0; x < self->setCancelNum; x++) { CBByteArraySetInt32(bytes, cursor, self->setCancel[x]); cursor += 4; } CBByteArraySetInt32(bytes, cursor, self->minVer); cursor += 4; CBByteArraySetInt32(bytes, cursor, self->maxVer); cursor += 4; // User agent set CBByteArraySetVarInt(bytes, cursor, userAgentLen); cursor += userAgentLen.size; for (int x = 0; x < self->userAgentNum; x++) { CBVarInt aUserAgentLen = CBVarIntFromUInt64(self->userAgents[x]->length); CBByteArraySetVarInt(bytes, cursor, aUserAgentLen); cursor += aUserAgentLen.size; CBByteArrayCopyByteArray(bytes, cursor, self->userAgents[x]); CBByteArrayChangeReference(self->userAgents[x], bytes, cursor); cursor += aUserAgentLen.val; } CBByteArraySetInt32(bytes, cursor, self->priority); cursor += 4; // Hidden comment CBByteArraySetVarInt(bytes, cursor, hiddenCommentLen); cursor += hiddenCommentLen.size; if (self->hiddenComment) { CBByteArrayCopyByteArray(bytes, cursor, self->hiddenComment); CBByteArrayChangeReference(self->hiddenComment, bytes, cursor); cursor += hiddenCommentLen.val; } // Displayed comment CBByteArraySetVarInt(bytes, cursor, displayedCommentLen); cursor += displayedCommentLen.size; if (self->displayedComment) { CBByteArrayCopyByteArray(bytes, cursor, self->displayedComment); CBByteArrayChangeReference(self->displayedComment, bytes, cursor); cursor += displayedCommentLen.val; } // Reserved CBByteArraySetVarInt(bytes, cursor, reservedLen); cursor += reservedLen.size; if (self->reserved) { CBByteArrayCopyByteArray(bytes, cursor, self->reserved); CBByteArrayChangeReference(self->reserved, bytes, cursor); cursor += reservedLen.val; } self->payload = CBNewByteArraySubReference(bytes, payloadLen.size, cursor - payloadLen.size); return true; }
uint32_t CBVersionSerialise(CBVersion * self, bool force){ CBByteArray * bytes = CBGetMessage(self)->bytes; if (NOT bytes) { CBGetMessage(self)->logError("Attempting to serialise a CBVersion with no bytes."); return 0; } if (bytes->length < 46) { CBGetMessage(self)->logError("Attempting to serialise a CBVersion with less than 46 bytes."); return 0; } CBByteArraySetInt32(bytes, 0, self->version); CBByteArraySetInt64(bytes, 4, self->services); CBByteArraySetInt64(bytes, 12, self->time); if (force && CBGetMessage(self->addRecv)->serialised) CBReleaseObject(CBGetMessage(self->addRecv)->bytes); if (NOT CBGetMessage(self->addRecv)->serialised || force) { CBGetMessage(self->addRecv)->bytes = CBByteArraySubReference(bytes, 20, bytes->length-20); if (NOT CBGetMessage(self->addRecv)->bytes) { CBGetMessage(self)->logError("Cannot create a new CBByteArray sub reference in CBVersionSerialise for receiving address."); return 0; } uint32_t len = CBNetworkAddressSerialise(self->addRecv,false); if (NOT len) { CBGetMessage(self)->logError("CBVersion cannot be serialised because of an error with the receiving CBNetworkAddress"); // Release bytes to avoid problems overwritting pointer without release, if serialisation is tried again. CBReleaseObject(CBGetMessage(self->addRecv)->bytes); return 0; } CBGetMessage(self->addRecv)->bytes->length = len; }else if (CBGetMessage(self->addRecv)->bytes->sharedData != bytes->sharedData){ // Move serialsed data to one location CBByteArrayCopyByteArray(bytes, 20, CBGetMessage(self->addRecv)->bytes); CBByteArrayChangeReference(CBGetMessage(self->addRecv)->bytes, bytes, 20); } if (self->version >= 106) { if (bytes->length < 85) { CBGetMessage(self)->logError("Attempting to serialise a CBVersion with less than 85 bytes required."); return 0; } if (self->userAgent->length > 400) { CBGetMessage(self)->logError("Attempting to serialise a CBVersion with a userAgent over 400 bytes."); return 0; } if (force && CBGetMessage(self->addSource)->serialised) CBReleaseObject(CBGetMessage(self->addSource)->bytes); if (NOT CBGetMessage(self->addSource)->serialised || force) { CBGetMessage(self->addSource)->bytes = CBByteArraySubReference(bytes, 46, bytes->length-46); if (NOT CBGetMessage(self->addSource)->bytes) { CBGetMessage(self)->logError("Cannot create a new CBByteArray sub reference in CBVersionSerialise for source address."); return 0; } uint32_t len = CBNetworkAddressSerialise(self->addSource,false); if (NOT len) { CBGetMessage(self)->logError("CBVersion cannot be serialised because of an error with the source CBNetworkAddress"); // Release bytes to avoid problems overwritting pointer without release, if serialisation is tried again. CBReleaseObject(CBGetMessage(self->addSource)->bytes); return 0; } CBGetMessage(self->addSource)->bytes->length = len; }else if (CBGetMessage(self->addSource)->bytes->sharedData != bytes->sharedData){ // Move serialsed data to one location CBByteArrayCopyByteArray(bytes, 46, CBGetMessage(self->addSource)->bytes); CBByteArrayChangeReference(CBGetMessage(self->addSource)->bytes, bytes, 46); } CBByteArraySetInt64(bytes, 72, self->nonce); CBVarInt varInt = CBVarIntFromUInt64(self->userAgent->length); CBVarIntEncode(bytes, 80, varInt); if (bytes->length < 84 + varInt.size + varInt.val) { CBGetMessage(self)->logError("Attempting to deserialise a CBVersion without enough space to cover the userAgent and block height."); return 0; } CBByteArrayCopyByteArray(bytes, 80 + varInt.size,self->userAgent); CBByteArrayChangeReference(self->userAgent,bytes,80 + varInt.size); CBByteArraySetInt32(bytes, 80 + varInt.size + (uint32_t)varInt.val, self->blockHeight); CBGetMessage(self)->serialised = true; return 84 + varInt.size + (uint32_t)varInt.val; }else{ // Not the further message. CBGetMessage(self)->serialised = true; return 46; } }