Files
thehub/libs/utils/cashaddr.h
T
tomFlowee efa6c05e1a A API review of CashAddr.h
This removes from the header all private methods, adds API docs and does
some renames that make code using this API much more readable.
2026-02-09 15:28:04 +01:00

88 lines
2.9 KiB
C++

/*
* This file is part of the Flowee project
* Copyright (C) 2017 Pieter Wuille
* Copyright (C) 2017 The Bitcoin developers
* Copyright (C) 2020-2026 Tom Zander <tom@flowee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FLOWEE_CASHADDR_H
#define FLOWEE_CASHADDR_H
#include <streaming/ConstBuffer.h>
#include <cstdint>
#include <string>
#include <vector>
namespace CashAddress {
// Cashaddr is an address format based on bech32.
enum AddressType : uint8_t {
PubkeyType = 0,
ScriptType = 1,
TokenPubkeyType = 2, //< Token-specific P2PKH
TokenScriptType = 3 //< Token-specific P2SH
};
struct Content {
AddressType type;
std::vector<uint8_t> hash;
bool isNull() const { return hash.empty(); }
};
/**
* Take a blockchain level data hash and turn it into a cash-address.
*
* @param prefix the chain name, without the colon. Probaby 'bitcoincash' or 'bchtest'
* @param content this struct holds the ripe160 hash of the address, plus the
* type of data the address encodes.
* @return the string will copy prefix, with a colon and then the address with the checksum at the end.
*/
std::string encode(const std::string &prefix, const Content &content);
/**
* Converts an address to the raw content object.
* Notice that the type is predifined to just be a couple of values, but we don't
* fail if the type is valid but unknown.
* As such a valid Content is not null AND has a known type.
*
* @param addr is the cash address, this can be with or without the prefix.
* @param prefix this will be used to validate the address.
* @return the content object will have actual data in the hash (and isNull()
* would return true) and in that case the type property will be set.
*/
Content decode(const std::string &addr, const std::string &prefix);
/**
* Returns if this character is valid in a cash address.
* Notice we only talk about the address, not the prefix.
*/
bool validCharacter(uint8_t x);
/**
* create a default script for the type and hash the output-script
* to get a unique ID for the entire out-script.
*
* \param content the previously parsed content.
* \return the sha256 hash
*/
Streaming::ConstBuffer createHashedOutputScript(const Content &content);
Content extractAddressFromScript(const Streaming::ConstBuffer &output);
} // namespace CashAddress
#endif