-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_out.rb
More file actions
86 lines (76 loc) · 2.14 KB
/
tx_out.rb
File metadata and controls
86 lines (76 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
module Tinycoin::Core
class TxOut
attr_reader :script_pubkey # 振出人の公開鍵と署名が含まれる
attr_reader :address # 受取人アドレス (base58の文字列)
attr_reader :amount
attr_reader :locktime
def initialize
@type = :unknown
end
def set_coinbase! wallet
@type = :coinbase
@amount = Tinycoin::Core::MINER_REWARD_AMOUNT
@script_pubkey = Script.generate_coinbase_out
@address = wallet.address
@locktime = Time.now.to_i
end
def parse_from_hash hash
begin
@type = :coinbase if hash.fetch("type") == "coinbase"
@amount = hash.fetch("value")
@locktime = hash.fetch("locktime")
@script_pubkey = hash.fetch("scriptPubKey").fetch("asm")
@address = hash.fetch("scriptPubKey").fetch("address")
self
rescue KeyError
raise Tinycoin::Errors::InvalidFieldFormat
end
end
def to_binary_s
generate_blk.to_binary_s
end
def to_json
to_hash.to_json
end
def to_binary_s
generate_blk.to_binary_s
end
def to_sha256hash_s
to_sha256hash.to_s(16).rjust(64, '0')
end
def to_sha256hash
blk_tx = generate_blk
@tx_id = Digest::SHA256.hexdigest(Digest::SHA256.digest(blk_tx.to_binary_s)).to_i(16)
@tx_id
end
def to_hash
if @type == :coinbase
{
type: "coinbase",
hash: to_sha256hash_s,
value: @amount.to_s.to_i(10),
locktime: @locktime,
scriptPubKey: {
asm: @script_pubkey.to_s,
address: @address.to_s
}
}
else
raise Tinycoin::Errors::NotImplemented
{
type: "unknown"
}
end
end
def generate_blk
Tinycoin::Types::BulkTxOut.new(
amount: @amount,
script_len: @script_pubkey.to_s.size,
script_pubkey: @script_pubkey.to_s,
locktime: @locktime,
address: Wallet.decode_base58(@address).to_i(16)
)
end
end
end