狗狗币地址生成 - 地址无效

Dogecoin Address generation - Address not valid

我正在尝试生成狗狗币地址。生成的地址与 RPC-API getnewaddress 生成的有效狗狗币地址长度相同,长度相同,但它们不起作用.它们无效。

步骤如下:

  1. Public 来自 secp256k1 的密钥
  2. 应用 SHA256,然后对 SHA256 的结果应用 RIPEMD-160
  3. 在 RIPEMD-160 结果的开头添加 0x1E(狗狗币版本)
  4. 将 SHA256 应用到校验和哈希的加密公钥两次
  5. 将校验和哈希的前 4 个字节(8 个字符)添加到加密的 pub 密钥的末尾
  6. 应用 BASE56

生成一个D开头的34个字符的地址,看起来很真实,但其中none个是有效的。为什么?

    use strict;
    use warnings;
    use Digest::SHA qw(sha256);
    use Crypt::PK::ECC;
    use Crypt::RIPEMD160;
    use v5.10;
    
    use Bitcoin::Crypto::Base58 qw(
            encode_base58
            decode_base58
            encode_base58check
            decode_base58check
    );
    
    # Bitcoin Version 0x00
    # Dogecoin Version 0x1E
    
    sub append_checksum_and_version {
        my ($binstr) = @_;
    
        return (chr(0x1E) . $binstr . substr(sha256(sha256($binstr)), 0, 4));
    }
    
    
    my $pk = Crypt::PK::ECC->new();
    $pk->generate_key('secp256k1');
    my $public_raw = $pk->export_key_raw('public');
    my $encrypted_pubkey = Crypt::RIPEMD160->hash(sha256($public_raw));
    say "Private Key: " . unpack "H*", $pk->export_key_raw('private');
    say "Dogecoin Address: " . encode_base58(append_checksum_and_version($encrypted_pubkey));  

Output:

Dogecoin Address: DGRHFUwqvzh8VBFR1gAhRFVbM476bATZVK

Dogecoin Address: DEvMuu6PbQLDQE72aofFzxk5c8AmzRZos9

Dogecoin Address: DM1zqiW5ZPb1MD8hP1sxtBsPkQAofFTRTw

可能校验和计算不正确?即使我对比特币使用 0x00 而不是 0x1E。比特币地址也无效。

原来少了一个字节

return (chr(0x1E) . $binstr . substr(sha256(sha256($binstr)), 0, 4));

必须替换为

return (chr(0x1E) . $binstr . substr(sha256(sha256(chr(0x1E) . $binstr)), 0, 4));

0x1E(狗狗币)或0x00(比特币)的版本字节必须包含在校验和的双 sha256 计算中。

use strict;
use warnings;
use Digest::SHA qw(sha256);
use Crypt::PK::ECC;
use Crypt::RIPEMD160;
use Bitcoin::Crypto::Base58 qw(encode_base58 decode_base58 encode_base58check decode_base58check);
use v5.10;

# Version byte: Bitcoin 0x00, Dogecoin 0x1E
sub cv {
    my ($binstr) = @_;
    return (chr(0x1E) . $binstr . substr(sha256(sha256(chr(0x1E) . $binstr)), 0, 4));
}

my $pk = Crypt::PK::ECC->new()->generate_key('secp256k1');
my $dogecoin_address = encode_base58(cv(Crypt::RIPEMD160->hash(sha256($pk->export_key_raw('public')))));
say "Dogecoin Address: " . $dogecoin_address; 

给出有效地址:

Dogecoin Address: DR61RhFNCkswmDx6JjLyVPSoq51gBtxxjK
Dogecoin Address: DFFV4xa9Ph2LUXaKbyxMtViHFhu9e9h88J
Dogecoin Address: DAk4ZbSoWnhpt2exkEevG2j93CVEL7g5Fu

更新:

如果你想从公钥生成狗狗币的wifkey。主网的版本字节是0x9E(狗狗币)和0x80(比特币)

sub toWifKey {
    my ($binstr) = @_;
    return (chr(0x9E). $binstr . substr(sha256(sha256(chr(0x9E) . $binstr)), 0, 4));
}

my $wifkey = encode_base58(toWifKey($pk->export_key_raw('private')));