/* buf already contains version and ripemd160. Append checksum and encode */ char *base58_with_check(char dest[BASE58_ADDR_MAX_LEN], u8 buf[1 + sizeof(struct ripemd160) + 4]) { /* Append checksum */ base58_get_checksum(buf + 1 + sizeof(struct ripemd160), buf, 1 + sizeof(struct ripemd160)); /* Now encode. */ return encode_base58(dest, BASE58_ADDR_MAX_LEN, buf, 1 + sizeof(struct ripemd160) + 4); }
std::string payment_address::encoded() const { data_chunk unencoded_address; unencoded_address.reserve(25); // Type, Hash, Checksum doth make thy address unencoded_address.push_back(version_); extend_data(unencoded_address, hash_); append_checksum(unencoded_address); BITCOIN_ASSERT(unencoded_address.size() == 25); return encode_base58(unencoded_address); }
std::string secret_to_wif(const ec_secret& secret, bool compressed) { auto version = to_array(payment_address::wif_version); data_chunk data; if (compressed) data = build_chunk({ version, secret, to_array(0x01) }, checksum_size); else data = build_chunk({version, secret}, checksum_size); append_checksum(data); return encode_base58(data); }
std::string payment_address::encoded(uint8_t version_byte) const { data_chunk unencoded_address; BITCOIN_ASSERT( type_ == payment_type::pubkey_hash || type_ == payment_type::script_hash); // Type, Hash, Checksum doth make thy address unencoded_address.push_back(version_byte); extend_data(unencoded_address, hash_); uint32_t checksum = generate_sha256_checksum(unencoded_address); extend_data(unencoded_address, uncast_type(checksum)); BITCOIN_ASSERT(unencoded_address.size() == 25); return encode_base58(unencoded_address); }
static char *to_base58(const tal_t *ctx, u8 version, const struct ripemd160 *rmd) { u8 buf[1 + sizeof(*rmd) + 4]; char out[BASE58_ADDR_MAX_LEN + 2], *p; buf[0] = version; memcpy(buf+1, rmd, sizeof(*rmd)); /* Append checksum */ base58_get_checksum(buf + 1 + sizeof(*rmd), buf, 1 + sizeof(*rmd)); p = encode_base58(out, BASE58_ADDR_MAX_LEN, buf, sizeof(buf)); return tal_strdup(ctx, p); }
char *key_to_base58(const tal_t *ctx, bool test_net, const struct privkey *key) { u8 buf[1 + 32 + 1 + 4]; char out[BASE58_KEY_MAX_LEN + 2], *p; buf[0] = test_net ? 239 : 128; memcpy(buf + 1, key->secret, sizeof(key->secret)); /* Mark this as a compressed key. */ buf[1 + 32] = 1; /* Append checksum */ base58_get_checksum(buf + 1 + 32 + 1, buf, 1 + 32 + 1); p = encode_base58(out, BASE58_KEY_MAX_LEN, buf, sizeof(buf)); return tal_strdup(ctx, p); }
std::string hd_public_key::encoded() const { auto prefix = mainnet_public_prefix; if (lineage_.testnet) prefix = testnet_public_prefix; auto data = build_data({ to_big_endian(prefix), to_byte(lineage_.depth), to_little_endian(lineage_.parent_fingerprint), to_big_endian(lineage_.child_number), c_, K_ }, checksum_size); append_checksum(data); return encode_base58(data); }
char *bitcoin_to_base58(const tal_t *ctx, bool test_net, const struct bitcoin_address *addr) { u8 buf[1 + sizeof(addr->addr) + 4]; char out[BASE58_ADDR_MAX_LEN + 2], *p; buf[0] = test_net ? 111 : 0; BUILD_ASSERT(sizeof(addr->addr) == sizeof(struct ripemd160)); memcpy(buf+1, addr, sizeof(addr->addr)); /* Append checksum */ base58_get_checksum(buf + 1 + sizeof(addr->addr), buf, 1 + sizeof(addr->addr)); p = encode_base58(out, BASE58_ADDR_MAX_LEN, buf, sizeof(buf)); return tal_strdup(ctx, p); }
BCW_API std::string hd_public_key::serialize() const { data_chunk data; data.reserve(serialized_length); auto prefix = mainnet_public_prefix; if (lineage_.testnet) prefix = testnet_public_prefix; extend_data(data, to_big_endian(prefix)); data.push_back(lineage_.depth); extend_data(data, to_little_endian(lineage_.parent_fingerprint)); extend_data(data, to_big_endian(lineage_.child_number)); extend_data(data, c_); extend_data(data, K_); append_checksum(data); return encode_base58(data); }
std::string stealth_address::encoded() const { if (!valid_) return std::string(); data_chunk raw_address; raw_address.push_back(get_version()); raw_address.push_back(get_options()); extend_data(raw_address, scan_pubkey_); // Spend_pubkeys must be guarded against a size greater than 255. auto number_spend_pubkeys = static_cast<uint8_t>(spend_pubkeys_.size()); // Adjust for key reuse. if (get_reuse_key()) --number_spend_pubkeys; raw_address.push_back(number_spend_pubkeys); // Serialize the spend keys, excluding any that match the scan key. for (const ec_point& pubkey: spend_pubkeys_) if (pubkey != scan_pubkey_) extend_data(raw_address, pubkey); raw_address.push_back(signatures_); // The prefix must be guarded against a size greater than 32 // so that the bitfield can convert into uint32_t and sized by uint8_t. auto prefix_number_bits = static_cast<uint8_t>(prefix_.size()); // Serialize the prefix bytes/blocks. raw_address.push_back(prefix_number_bits); extend_data(raw_address, prefix_.blocks()); append_checksum(raw_address); return encode_base58(raw_address); }
std::string payment_address::encoded() const { const auto data = wrap(version_, hash_); return encode_base58(data); }