Files
thehub/libs/utils/cashaddr.h
T

88 lines
2.9 KiB
C++
Raw Permalink Normal View History

2019-04-11 11:33:27 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2017 Pieter Wuille
* Copyright (C) 2017 The Bitcoin developers
2026-02-09 15:23:48 +01:00
* Copyright (C) 2020-2026 Tom Zander <tom@flowee.org>
2019-04-11 11:33:27 +02:00
*
* 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/>.
*/
2024-01-22 19:32:15 +01:00
#ifndef FLOWEE_CASHADDR_H
#define FLOWEE_CASHADDR_H
2019-04-11 11:33:27 +02:00
2019-10-16 22:43:16 +02:00
#include <streaming/ConstBuffer.h>
2019-04-11 11:33:27 +02:00
#include <cstdint>
#include <string>
#include <vector>
namespace CashAddress {
2026-02-09 15:23:48 +01:00
// Cashaddr is an address format based on bech32.
2019-04-11 11:33:27 +02:00
2026-02-09 15:23:48 +01:00
enum AddressType : uint8_t {
PubkeyType = 0,
ScriptType = 1,
TokenPubkeyType = 2, //< Token-specific P2PKH
TokenScriptType = 3 //< Token-specific P2SH
};
2019-04-11 11:33:27 +02:00
struct Content {
AddressType type;
std::vector<uint8_t> hash;
2026-02-09 15:23:48 +01:00
bool isNull() const { return hash.empty(); }
2019-04-11 11:33:27 +02:00
};
2026-02-09 15:23:48 +01:00
/**
* 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);
2019-04-11 11:33:27 +02:00
2019-10-16 22:43:16 +02:00
/**
2025-01-27 20:40:02 +01:00
* create a default script for the type and hash the output-script
2019-10-16 22:43:16 +02:00
* 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);
2023-09-02 19:40:46 +02:00
Content extractAddressFromScript(const Streaming::ConstBuffer &output);
2019-10-16 22:43:16 +02:00
} // namespace CashAddress
2021-07-29 20:26:03 +02:00
#endif