bip32.c
int hdnode_deserialize(const char *str, uint32_t version_public, uint32_t version_private, const char *curve, HDNode *node, uint32_t *fingerprint)
{
	uint8_t node_data[78];
	memset(node, 0, sizeof(HDNode));
	node->curve = get_curve_by_name(curve);
	if (base58_decode_check(str, node->curve->hasher_base58, node_data, sizeof(node_data)) != sizeof(node_data)) {
		return -1;
	}
	uint32_t version = read_be(node_data);
	if (version == version_public) {
		memzero(node->private_key, sizeof(node->private_key));
		memcpy(node->public_key, node_data + 45, 33);
	} else if (version == version_private) { // private node
		if (node_data[45]) { // invalid data
			return -2;
		}
		memcpy(node->private_key, node_data + 46, 32);
		memzero(node->public_key, sizeof(node->public_key));
	} else {
		return -3; // invalid version
	}
	node->depth = node_data[4];
	if (fingerprint) {
		*fingerprint = read_be(node_data + 5);
	}
	node->child_num = read_be(node_data + 9);
	memcpy(node->chain_code, node_data + 13, 32);
	return 0;
}
// check for validity of curve point in case of public data not performed
check for validity of curve point in case of public data not performed
check for validity of curve point in case of public data not performed
int hdnode_deserialize(const char *str, uint32_t version_public, uint32_t version_private, const char *curve, HDNode *node, uint32_t *fingerprint)
{
	uint8_t node_data[78];
	memset(node, 0, sizeof(HDNode));
	node->curve = get_curve_by_name(curve);
	if (base58_decode_check(str, node->curve->hasher_base58, node_data, sizeof(node_data)) != sizeof(node_data)) {
		return -1;
	}
	uint32_t version = read_be(node_data);
	if (version == version_public) {
		memzero(node->private_key, sizeof(node->private_key));
		memcpy(node->public_key, node_data + 45, 33);
	} else if (version == version_private) { 
		if (node_data[45]) { 
			return -2;
		}
		memcpy(node->private_key, node_data + 46, 32);
		memzero(node->public_key, sizeof(node->public_key));
	} else {
		return -3; 
	}
	node->depth = node_data[4];
	if (fingerprint) {
		*fingerprint = read_be(node_data + 5);
	}
	node->child_num = read_be(node_data + 9);
	memcpy(node->chain_code, node_data + 13, 32);
	return 0;
}
int hdnode_deserialize ( const char * str , uint32_t version_public , uint32_t version_private , const char * curve , HDNode * node , uint32_t * fingerprint ) { uint8_t node_data [ 78 ] ; memset ( node , 0 , sizeof ( HDNode ) ) ; node -> curve = get_curve_by_name ( curve ) ; if ( base58_decode_check ( str , node -> curve -> hasher_base58 , node_data , sizeof ( node_data ) ) != sizeof ( node_data ) ) { return -1 ; } uint32_t version = read_be ( node_data ) ; if ( version == version_public ) { memzero ( node -> private_key , sizeof ( node -> private_key ) ) ; memcpy ( node -> public_key , node_data + 45 , 33 ) ; } else if ( version == version_private ) { if ( node_data [ 45 ] ) { return -2 ; } memcpy ( node -> private_key , node_data + 46 , 32 ) ; memzero ( node -> public_key , sizeof ( node -> public_key ) ) ; } else { return -3 ; } node -> depth = node_data [ 4 ] ; if ( fingerprint ) { * fingerprint = read_be ( node_data + 5 ) ; } node -> child_num = read_be ( node_data + 9 ) ; memcpy ( node -> chain_code , node_data + 13 , 32 ) ; return 0 ; }
check for validity of curve point in case of public data not performed
check for validity of curve point in case of public data not performed
// private node // invalid data // invalid version