比特币隔离见证钱包地址计算

问题描述 投票:0回答:1

抱歉,这有点令人误解,但是我实际上是用Litecoin而不是比特币来做到这一点的,但是算法完全相同,我很确定答案也将是。实际上,几乎可以肯定的是,比特币也将面临同样的问题。我很难为给定的公共密钥生成正确的隔离见证地址,如下所示:

Litecoin地址生成(与比特币相同)

  1. 我使用压缩的公共密钥:03861b83752e0c47cac36fc5980ae8956f41f6d9792a51f68a6bd5f66cc7364b48
  2. 公共密钥上的SHA256。
  3. RipeMD160 2的结果。
  4. 在我的情况下,添加一个前缀字节,即LTC_TESTNET的0x3a
  5. 将SHA256的结果加倍4,并将该结果的前四个字节添加到RawMD160的末尾。
  6. 最后,是Base58编码。

所有人看起来都很花哨,对吗? Out弹出SegWit地址:QhQxSZvvDWr3JvoKsYVC6BBW3DqkGhesrF

但是,我很确定这个地址不正确。当我将私钥作为(p2wpkh-p2sh :)导入Electrum-LTC时,它为此私钥生成的地址是:QYyWqgyWSm1AJWph32GnyY7eamG1wUDruk

现在,我相信Electrum-LTC是正确的,生成SegWit地址时我缺少一些东西,而不仅仅是更改网络前缀,还需要更多地址生成。我的公钥哈希为:

e444ac77800cdf904b928fc4642ab6fb6d4d696c

和Electrum-LTC的公钥哈希为:

87b3e5bf5b2a1381e6549020d245e45b9ac76c82

由于值有很大不同,它表明初始的SHA256并不仅是仅对公钥进行哈希处理,而且我丢失了一些东西。我的想法不完整,希望有人能给出答案,并且我可以在chainparams.cpp的源代码中找到唯一的东西:

base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};

任何人都知道我可能在做什么错吗?

更新-有人要求输入代码-就在这里

#include <openssl/sha.h>
#include <openssl/ripemd.h>
#include <cstdint>
#include <iostream>
#include <vector>

std::string base58Encode(const std::vector<uint8_t>& data)
{
    const uint8_t mapping[] = {
      '1', '2', '3', '4', '5', '6', '7', '8',
      '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
      'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q',
      'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
      'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
      'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p',
      'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
      'y', 'z' };

    std::vector<uint8_t> digits((data.size() * 137) / 100);
    size_t digitslen = 1;
    for (size_t i = 0; i < data.size(); i++)
    {
        uint32_t carry = static_cast<uint32_t>(data[i]);
        for (size_t j = 0; j < digitslen; j++)
        {
            carry = carry + static_cast<uint32_t>(digits[j] << 8);
            digits[j] = static_cast<uint8_t>(carry % 58);
            carry /= 58;
        }
        for (; carry; carry /= 58)
            digits[digitslen++] = static_cast<uint8_t>(carry % 58);
    }
    std::string result;
    for (size_t i = 0; i < data.size() && !data[i]; i++)
        result.push_back(mapping[0]);
    for (size_t i = 0; i < digitslen; i++)
        result.push_back(mapping[digits[digitslen - 1 - i]]);
    return result;
}

std::vector<uint8_t> SHA256_Hash(const std::vector<uint8_t>& data)
{
    std::vector<uint8_t> SHA256_Digest(SHA256_DIGEST_LENGTH);
    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, data.data(), data.size());
    SHA256_Final(SHA256_Digest.data(), &ctx);
    return SHA256_Digest;
}

std::vector<uint8_t> RipeMD160_Hash(const std::vector<uint8_t>& data)
{
    std::vector<uint8_t> RipeMD160_Digest(RIPEMD160_DIGEST_LENGTH);
    RIPEMD160_CTX ctx;
    RIPEMD160_Init(&ctx);
    RIPEMD160_Update(&ctx, data.data(), data.size());
    RIPEMD160_Final(RipeMD160_Digest.data(), &ctx);
    return RipeMD160_Digest;
}

std::string getWalletAddress(const std::vector<uint8_t>& public_key, uint8_t id)
{
    std::vector<uint8_t> addr_hash = SHA256_Hash(key);
    std::vector<uint8_t> addr_ripe = RipeMD160_Hash(addr_hash);
    addr_ripe.insert(addr_ripe.begin(), id);
    addr_hash = SHA256_Hash(SHA256_Hash(addr_ripe));
    addr_ripe.insert(addr_ripe.end(), addr_hash.begin(), addr_hash.begin() + sizeof(uint32_t));
    return base58Encode(addr_ripe);
}

int main(int argc, char** argv)
{
    const uint8_t LTC_TESTNET = 0x3a;

    std::vector<uint8_t> public_key{
        0x03, 0x86, 0x1b, 0x83, 0x75, 0x2e, 0x0c, 0x47, 0xca, 0xc3, 0x6f, 0xc5, 0x98, 0x0a, 0xe8, 0x95,
        0x6f, 0x41, 0xf6, 0xd9, 0x79, 0x2a, 0x51, 0xf6, 0x8a, 0x6b, 0xd5, 0xf6, 0x6c, 0xc7, 0x36, 0x4b, 0x48};

    std::cout << "Wallet Address: " << getWalletAddress(public_key, LTC_TESTNET) << std::endl;
    // The above ouputs:  QhQxSZvVDWr3JvoKsYVC6BBW3DqkGhesrF
    // But it should be:  QYyWqgyWSm1AJWph32GnyY7eamG1wUDruk
    return 0;
}
c++ bitcoin litecoin
1个回答
0
投票

终于弄清楚了这一点...计算p2wpkh-p2sh地址时,不只是地址前缀不同。实际上,我在这里找到了上述问题的答案:https://bitcoin.stackexchange.com/questions/72775/is-it-possible-to-convert-an-address-from-p2pkh-to-p2sh(尽管它的解释方式很神秘)。

我的问题中的上述代码对于生成比特币/莱特币等的p2pkh地址非常满意,但是当生成Q(对于Litecoin),2(对于比特币)时,它将不起作用,因为它不仅是公钥那是散列的。它实际上是一个脚本,大小为0x00(DUP_0),然后是公共密钥哈希的长度(0x14)。

因此,如果地址生成代码更改为:,请修复以上代码:

std::string getWalletAddress(const std::vector<uint8_t>& public_key, uint8_t id, bool is_p2sh)
{
    std::vector<uint8_t> addr_hash = SHA256_Hash(key);
    std::vector<uint8_t> addr_ripe = RipeMD160_Hash(addr_hash);
    if (is_p2sh)
    {
        addr_ripe.insert(addr_ripe.begin(), { 0x00, static_cast<uint8_t>(addr_ripe.size()) });
        addr_hash = SHA256_Hash(addr_ripe);
        addr_ripe = RipeMD160_Hash(addr_ripe);
    }
    addr_ripe.insert(addr_ripe.begin(), id);
    addr_hash = SHA256_Hash(SHA256_Hash(addr_ripe));
    addr_ripe.insert(addr_ripe.end(), addr_hash.begin(), addr_hash.begin() + sizeof(uint32_t));
    return base58Encode(addr_ripe);
}

// This will now work for both p2pkh addresses and p2wpkh-p2sh addresses

getWalletAddress(public_key, LTC_TESTNET, true);  // Produces: QYyWqgyWSm1AJWph32GnyY7eamG1wUDruk
© www.soinside.com 2019 - 2024. All rights reserved.