Skip to content

Commit

Permalink
Add Bitcoin::Block#create_genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
azuchi committed Aug 9, 2024
1 parent 0b62492 commit 433c238
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/bitcoin/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ def initialize(header, transactions = [])
@transactions = transactions
end

# Create genesis block.
# @param [String] msg Message embedded in coinbase transaction.
# @param [Bitcoin::Script] script Coinbase transaction scriptPubkey.
# @param [Integer] time Block time.
# @param [Integer] nonce nonce.
# @param [Integer] bits nBits
# @param [Integer] version nVersion.
# @param [Integer] rewards Block rewards(satoshi).
def self.create_genesis(msg, script, time, nonce, bits, version, rewards = 50 * 100000000)
coinbase = Bitcoin::Tx.create_coinbase(msg, script, rewards)
header = BlockHeader.new(
version,
'00' * 32,
MerkleTree.build_from_leaf([coinbase.txid]).merkle_root.rhex,
time,
bits,
nonce
)
Block.new(header, [coinbase])
end

def self.parse_from_payload(payload)
Bitcoin::Message::Block.parse_from_payload(payload).to_block
end
Expand Down
13 changes: 13 additions & 0 deletions lib/bitcoin/tx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def initialize
alias_method :in, :inputs
alias_method :out, :outputs

# Create coinbase transaction.
# @param [String] msg Message embedded in coinbase transaction.
# @param [Bitcoin::Script] script Coinbase transaction scriptPubkey.
# @param [Integer] rewards Coinbase Transaction Rewards
# @return [Bitcoin::Tx] Coinbase transaction.
def self.create_coinbase(msg, script, rewards = 50 * 100000000)
coinbase = Tx.new
script_sig = Bitcoin::Script.new << 486604799 << "04" << msg.bth
coinbase.in << TxIn.new(out_point: OutPoint.create_coinbase_outpoint, script_sig: script_sig)
coinbase.out << TxOut.new(value: rewards, script_pubkey: script)
coinbase
end

def self.parse_from_payload(payload, non_witness: false, strict: false)
buf = payload.is_a?(String) ? StringIO.new(payload) : payload
tx = new
Expand Down
15 changes: 15 additions & 0 deletions spec/bitcoin/block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@
end
end

describe "create_genesis" do
it do
testnet4 = described_class.create_genesis(
"03/May/2024 000000000000000000001ebd58c244970b3aa9d783bb001011fbe8ea8e98e00e",
Bitcoin::Script.new << "000000000000000000000000000000000000000000000000000000000000000000" << Bitcoin::Opcodes::OP_CHECKSIG,
1714777860,
393743547,
0x1d00ffff,
1
)
expect(testnet4.header.merkle_root.rhex).to eq('7aa0a7ae1e223414cb807e40cd57e667b718e42aaf9306db9102fe28912b7b4e')
expect(testnet4.block_hash.rhex).to eq('00000000da84f2bafbbc53dee25a72ae507ff4914b867c565be350b0da8bf043')
end
end

end

0 comments on commit 433c238

Please sign in to comment.