【问题标题】:Retrive Public key from a Bitcoin Private Key从比特币私钥中检索公钥
【发布时间】:2014-01-17 18:08:13
【问题描述】:

我如何从比特币私钥中获取比特币地址。

我了解整个方法,除了第一个方法,公钥及其 x 和 y 线来自哈希/私钥。

如果我能在 php 中获得代码示例,那对我会更有帮助。

【问题讨论】:

  • @Michael J. Gray,同意。此外,这可能属于 bitcoin.stackexchange.com
  • @MichaelJ.Gray 我了解整个方法,但我在特定部分“公钥如何来和 x,y 线”有问题。我不是在写代码,而是想先理解它。
  • @ArmandoWaky 您可能需要查看*.com/help/on-topic 以了解有关此处主题问题的信息。您的问题与软件算法或特定编程问题无关,而是与比特币相关的加密算法有关。也许正如 r3wt 所提到的,它应该转到bitcoin.stackexchange.com。即使crypto.stackexchange.com 也会更容易接受。

标签: php cryptography bitcoin ecdsa


【解决方案1】:

这是一个 NodeJS 脚本,从 WIF -> 私钥 -> 公钥 -> 二进制地址 -> 人类可读地址

// EXAMPLE: node wif_details.js 5JuHN27YsJktraQYGgLeihxZ2bwQbFANFdpc8gtDgqyyJsZJQB6

const ecc = require('eosjs-ecc');
const base58 = require('bs58');
const ripemd160 = require('ripemd160')

const wif = process.argv[2];
console.log("WIF: ", wif);

const privkey = ecc.PrivateKey.fromString(wif);
console.log("Private Key: ", privkey.toBuffer().toString('hex'));

const compressed_pubkey = privkey.toPublic();
const uncompressed_pubkey = compressed_pubkey.toUncompressed();
console.log("Public key (compressed): ", compressed_pubkey.toHex());
console.log("Public key: ", uncompressed_pubkey.toHex());

const hash1 = ecc.sha256(compressed_pubkey.toBuffer());
const hash2 = new ripemd160().update(Buffer.from(hash1, 'hex')).digest('hex');
const hash3 = ecc.sha256(uncompressed_pubkey.toBuffer());
const hash4 = new ripemd160().update(Buffer.from(hash3, 'hex')).digest('hex');
const with_prefix_compressed = '00' + hash2;
const with_prefix_uncompressed = '00' + hash4;

const hash5 = ecc.sha256(Buffer.from(with_prefix_compressed, 'hex'));
const hash6 = ecc.sha256(Buffer.from(hash5, 'hex'));
const hash7 = ecc.sha256(Buffer.from(with_prefix_uncompressed, 'hex'));
const hash8 = ecc.sha256(Buffer.from(hash7, 'hex'));
const binary_address_compressed = with_prefix_compressed + hash6.slice(0,8);
const binary_address_uncompressed = with_prefix_uncompressed + hash8.slice(0,8);
console.log("Binary address (compressed): ", binary_address_compressed);
console.log("Binary address: ", binary_address_uncompressed);

const bitcoin_address_compressed = base58.encode(Buffer.from(binary_address_compressed, 'hex'));
const bitcoin_address_uncompressed = base58.encode(Buffer.from(binary_address_uncompressed, 'hex'));
console.log("Bitcoin address (compressed): ", bitcoin_address_compressed);
console.log("Bitcoin address: ", bitcoin_address_uncompressed);

【讨论】:

    【解决方案2】:

    对于初学者来说,请使用以下库:

    https://github.com/RobKohr/PHP-Bitcoin-Address-Creator

    您可以选择使用 vanitygen 的后端并通过exec()shell_exec() 甚至更好的escapeshellarg() 生成地址。除了这两种方法之外,您的选择是使用 bitcoind 服务器设置 rpc。

    当然还有更复杂的解决方案,比如位于here(coinbit.tk, a splitkey vanity address generator that generates the private key in javascript)的地方

    https://github.com/RobKohr/PHP-Bitcoin-Address-Creator

    如果您仍有问题,请参阅以下主题。

    https://bitcoin.stackexchange.com/questions/2289/php-script-to-create-private-key-public-address

    https://bitcointalk.org/index.php?topic=81626.0

    【讨论】:

      【解决方案3】:

      这并不是您真正想要的,因为您从特定的私钥开始。 但是,如果您只想生成私钥和公钥,则可能需要查看 vanitygen,它会同时生成两者。

      【讨论】:

        最近更新 更多