block_io 1.0.6 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.appveyor.yml-disabled +26 -0
- data/.gitignore +5 -1
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/LICENSE +1 -1
- data/README.md +22 -13
- data/block_io.gemspec +9 -8
- data/examples/basic.rb +38 -10
- data/examples/dtrust.rb +60 -42
- data/examples/proxy.rb +36 -0
- data/examples/sweeper.rb +24 -14
- data/lib/block_io.rb +16 -399
- data/lib/block_io/api_exception.rb +11 -0
- data/lib/block_io/chainparams/BTC.yml +8 -0
- data/lib/block_io/chainparams/BTCTEST.yml +8 -0
- data/lib/block_io/chainparams/DOGE.yml +8 -0
- data/lib/block_io/chainparams/DOGETEST.yml +8 -0
- data/lib/block_io/chainparams/LTC.yml +8 -0
- data/lib/block_io/chainparams/LTCTEST.yml +8 -0
- data/lib/block_io/client.rb +243 -0
- data/lib/block_io/extended_bitcoinrb.rb +127 -0
- data/lib/block_io/helper.rb +262 -0
- data/lib/block_io/key.rb +38 -0
- data/lib/block_io/version.rb +1 -1
- data/spec/client_misc_spec.rb +76 -0
- data/spec/client_spec.rb +68 -0
- data/spec/dtrust_spec.rb +167 -0
- data/spec/helper_spec.rb +44 -0
- data/spec/key_spec.rb +92 -0
- data/spec/larger_transaction_spec.rb +351 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/sweep_spec.rb +115 -0
- data/spec/test-cases/.gitignore +2 -0
- data/spec/test-cases/LICENSE +21 -0
- data/spec/test-cases/README.md +2 -0
- data/spec/test-cases/json/create_and_sign_transaction_response.json +61 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_251inputs.json +1261 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_252inputs.json +1266 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_253inputs.json +1271 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_P2WSH-over-P2SH_1of2_762inputs.json +3816 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2SH_3of5_195inputs.json +2931 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2SH_4of5_195inputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_3of5_251inputs.json +3771 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_3of5_252inputs.json +3786 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_3of5_253inputs.json +3801 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_4of5_251inputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_4of5_252inputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_P2WSH-over-P2SH_4of5_253inputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_3of5_251inputs.json +3771 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_3of5_252inputs.json +3786 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_3of5_253inputs.json +3801 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_4of5_251inputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_4of5_252inputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_WITNESS_V0_4of5_253inputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2sh_3_of_5_keys.json +21 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2sh_4_of_5_keys.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_3_of_5_keys.json +21 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_4_of_5_keys.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3_of_5_keys.json +36 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3of5_251outputs.json +591 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3of5_252outputs.json +576 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3of5_253outputs.json +531 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4_of_5_keys.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4of5_251outputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4of5_252outputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4of5_253outputs.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_sweep_p2pkh.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_sweep_p2wpkh.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_sweep_p2wpkh_over_p2sh.json +5 -0
- data/spec/test-cases/json/create_and_sign_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json +21 -0
- data/spec/test-cases/json/get_balance_response.json +8 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2SH_3of5_195inputs.json +1397 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2SH_4of5_195inputs.json +1397 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_3of5_251inputs.json +1795 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_3of5_252inputs.json +1802 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_3of5_253inputs.json +1809 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_4of5_251inputs.json +1789 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_4of5_252inputs.json +1802 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_P2WSH-over-P2SH_4of5_253inputs.json +1809 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_3of5_251inputs.json +1795 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_3of5_252inputs.json +1802 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_3of5_253inputs.json +1809 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_4of5_251inputs.json +1795 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_4of5_252inputs.json +1802 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_WITNESS_V0_4of5_253inputs.json +1809 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_p2sh.json +45 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_p2wsh_over_p2sh.json +45 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0.json +52 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_3of5_251outputs.json +1805 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_3of5_252outputs.json +1804 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_3of5_253outputs.json +1789 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_4of5_251outputs.json +1805 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_4of5_252outputs.json +1804 -0
- data/spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0_4of5_253outputs.json +1789 -0
- data/spec/test-cases/json/prepare_sweep_transaction_response_p2pkh.json +35 -0
- data/spec/test-cases/json/prepare_sweep_transaction_response_p2wpkh.json +35 -0
- data/spec/test-cases/json/prepare_sweep_transaction_response_p2wpkh_over_p2sh.json +35 -0
- data/spec/test-cases/json/prepare_transaction_response.json +164 -0
- data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_251inputs.json +1796 -0
- data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_252inputs.json +1803 -0
- data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_253inputs.json +1810 -0
- data/spec/test-cases/json/prepare_transaction_response_P2WSH-over-P2SH_1of2_762inputs.json +5367 -0
- data/spec/test-cases/json/prepare_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json +76 -0
- data/spec/test-cases/json/summarize_prepared_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json +6 -0
- metadata +249 -43
data/lib/block_io/key.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module BlockIo
|
2
|
+
|
3
|
+
class Key
|
4
|
+
|
5
|
+
def self.generate
|
6
|
+
# returns a new key
|
7
|
+
Bitcoin::Key.generate(Bitcoin::Key::TYPES[:compressed]) # compressed
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from_private_key_hex(priv_key_hex)
|
11
|
+
# returns Bitcoin::Key (compressed)
|
12
|
+
# quirky behavior from bitcoinrb 0.7.0: use IntegerOctetString.encode on private key (integer) first
|
13
|
+
Bitcoin::Key.new(:priv_key => ECDSA::Format::IntegerOctetString.encode(priv_key_hex.to_i(16), 32).bth, :key_type => Bitcoin::Key::TYPES[:compressed])
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_passphrase(passphrase)
|
17
|
+
# ATTENTION: use BlockIo::Key.new to generate new private keys. Using passphrases is not recommended due to lack of / low entropy.
|
18
|
+
# create a private/public key pair from a given passphrase
|
19
|
+
# use a long, random passphrase. your security depends on the passphrase's entropy.
|
20
|
+
|
21
|
+
raise Exception.new("Must provide passphrase at least 8 characters long.") if passphrase.nil? or passphrase.length < 8
|
22
|
+
|
23
|
+
hashed_key = Helper.sha256([passphrase].pack("H*")) # must pass bytes to sha256
|
24
|
+
|
25
|
+
# modding is for backward compatibility with legacy bitcoinjs
|
26
|
+
BlockIo::Key.from_private_key_hex((hashed_key.to_i(16) % ECDSA::Group::Secp256k1.order).to_s(16))
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.from_wif(wif)
|
30
|
+
# returns a new key extracted from the Wallet Import Format provided
|
31
|
+
|
32
|
+
Bitcoin::Key.from_wif(wif)
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/lib/block_io/version.rb
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
describe "Client" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@api_key = "0000-0000-0000-0000"
|
7
|
+
@req_params = {:to_address => "QTLcyTFrH7T6kqUsi1VV2mJVXmX3AmwUNH", :amounts => "0.248"}
|
8
|
+
@headers = {
|
9
|
+
'Accept' => 'application/json',
|
10
|
+
'Connection' => 'Keep-Alive',
|
11
|
+
'Content-Type' => 'application/json; charset=UTF-8',
|
12
|
+
'Host' => 'block.io',
|
13
|
+
'User-Agent' => "gem:block_io:#{BlockIo::VERSION}"
|
14
|
+
}
|
15
|
+
|
16
|
+
@prepare_transaction_response = File.new("spec/test-cases/json/prepare_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json").read
|
17
|
+
@stub1 = stub_request(:post, "https://block.io/api/v2/prepare_transaction").
|
18
|
+
with(
|
19
|
+
body: @req_params.merge({:api_key => @api_key}).to_json,
|
20
|
+
headers: @headers).
|
21
|
+
to_return(status: 200, body: @prepare_transaction_response, headers: {})
|
22
|
+
|
23
|
+
@create_and_sign_transaction_response = File.new("spec/test-cases/json/create_and_sign_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json").read
|
24
|
+
@summarize_prepared_transaction_response = File.new("spec/test-cases/json/summarize_prepared_transaction_response_with_blockio_fee_and_expected_unsigned_txid.json").read
|
25
|
+
|
26
|
+
@insecure_pin_valid = "d1650160bd8d2bb32bebd139d0063eb6063ffa2f9e4501ad" # still insecure, don't use this!
|
27
|
+
@insecure_pin_invalid = "blockiotestpininsecure"
|
28
|
+
end
|
29
|
+
|
30
|
+
context "summarize_prepare_transaction" do
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
|
34
|
+
@blockio = BlockIo::Client.new(:api_key => @api_key, :pin => @insecure_pin_valid)
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
it "success" do
|
39
|
+
|
40
|
+
@blockio.prepare_transaction(@req_params)
|
41
|
+
|
42
|
+
expect(@stub1).to have_been_requested.times(1)
|
43
|
+
|
44
|
+
expect(@blockio.summarize_prepared_transaction(Oj.safe_load(@prepare_transaction_response))).to eq(Oj.safe_load(@summarize_prepared_transaction_response))
|
45
|
+
|
46
|
+
expect(@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_transaction_response))).to eq(Oj.safe_load(@create_and_sign_transaction_response))
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
context "create_and_sign_transaction_with_invalid_expected_unsigned_txid" do
|
53
|
+
|
54
|
+
before(:each) do
|
55
|
+
|
56
|
+
@blockio = BlockIo::Client.new(:api_key => @api_key, :pin => @insecure_pin_valid)
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
it "fails" do
|
61
|
+
|
62
|
+
@blockio.prepare_transaction(@req_params)
|
63
|
+
|
64
|
+
expect(@stub1).to have_been_requested.times(1)
|
65
|
+
|
66
|
+
@bad_response = Oj.safe_load(@prepare_transaction_response)
|
67
|
+
@bad_response['data']['expected_unsigned_txid'] = SecureRandom.hex(32)
|
68
|
+
|
69
|
+
expect{@blockio.create_and_sign_transaction(@bad_response)}.to raise_error(Exception, "Expected unsigned transaction ID mismatch. Please report this error to support@block.io.")
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
describe "Client.prepare_transaction" do
|
2
|
+
|
3
|
+
before(:each) do
|
4
|
+
@api_key = "0000-0000-0000-0000"
|
5
|
+
@req_params = {:to_address => "QTLcyTFrH7T6kqUsi1VV2mJVXmX3AmwUNH", :amounts => "0.248"}
|
6
|
+
@headers = {
|
7
|
+
'Accept' => 'application/json',
|
8
|
+
'Connection' => 'Keep-Alive',
|
9
|
+
'Content-Type' => 'application/json; charset=UTF-8',
|
10
|
+
'Host' => 'block.io',
|
11
|
+
'User-Agent' => "gem:block_io:#{BlockIo::VERSION}"
|
12
|
+
}
|
13
|
+
|
14
|
+
@prepare_transaction_response = File.new("spec/test-cases/json/prepare_transaction_response.json").read
|
15
|
+
@stub1 = stub_request(:post, "https://block.io/api/v2/prepare_transaction").
|
16
|
+
with(
|
17
|
+
body: @req_params.merge({:api_key => @api_key}).to_json,
|
18
|
+
headers: @headers).
|
19
|
+
to_return(status: 200, body: @prepare_transaction_response, headers: {})
|
20
|
+
|
21
|
+
@create_and_sign_transaction_response = File.new("spec/test-cases/json/create_and_sign_transaction_response.json").read
|
22
|
+
|
23
|
+
@insecure_pin_valid = "d1650160bd8d2bb32bebd139d0063eb6063ffa2f9e4501ad" # still insecure, don't use this!
|
24
|
+
@insecure_pin_invalid = "blockiotestpininsecure"
|
25
|
+
end
|
26
|
+
|
27
|
+
context "pin_valid" do
|
28
|
+
|
29
|
+
before(:each) do
|
30
|
+
|
31
|
+
@blockio = BlockIo::Client.new(:api_key => @api_key, :pin => @insecure_pin_valid)
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
it "success" do
|
36
|
+
|
37
|
+
@blockio.prepare_transaction(@req_params)
|
38
|
+
|
39
|
+
expect(@stub1).to have_been_requested.times(1)
|
40
|
+
|
41
|
+
expect(@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_transaction_response))).to eq(Oj.safe_load(@create_and_sign_transaction_response))
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
context "pin_invalid" do
|
48
|
+
|
49
|
+
before(:each) do
|
50
|
+
|
51
|
+
@blockio = BlockIo::Client.new(:api_key => @api_key, :pin => @insecure_pin_invalid)
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
it "fails" do
|
56
|
+
|
57
|
+
@blockio.prepare_transaction(@req_params)
|
58
|
+
|
59
|
+
expect(@stub1).to have_been_requested.times(1)
|
60
|
+
|
61
|
+
expect { @blockio.create_and_sign_transaction(Oj.safe_load(@prepare_transaction_response)) }.to raise_error(Exception, "Invalid Secret PIN provided.")
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
data/spec/dtrust_spec.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
describe "Client.prepare_dtrust_transaction" do
|
2
|
+
|
3
|
+
before(:each) do
|
4
|
+
@api_key = "0000-0000-0000-0000"
|
5
|
+
@private_keys = ["b515fd806a662e061b488e78e5d0c2ff46df80083a79818e166300666385c0a2",
|
6
|
+
"1584b821c62ecdc554e185222591720d6fe651ed1b820d83f92cdc45c5e21f",
|
7
|
+
"2f9090b8aa4ddb32c3b0b8371db1b50e19084c720c30db1d6bb9fcd3a0f78e61",
|
8
|
+
"6c1cefdfd9187b36b36c3698c1362642083dcc1941dc76d751481d3aa29ca65"]
|
9
|
+
@to_address = "QcnYiN3t3foHxHv7CnqXrmRoiMkADhapZw"
|
10
|
+
@amount = "0.00020000"
|
11
|
+
@headers = {
|
12
|
+
'Accept' => 'application/json',
|
13
|
+
'Connection' => 'Keep-Alive',
|
14
|
+
'Content-Type' => 'application/json; charset=UTF-8',
|
15
|
+
'Host' => 'block.io',
|
16
|
+
'User-Agent' => "gem:block_io:#{BlockIo::VERSION}"
|
17
|
+
}
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
context "p2sh" do
|
22
|
+
|
23
|
+
before(:each) do
|
24
|
+
|
25
|
+
@blockio = BlockIo::Client.new(:api_key => @api_key)
|
26
|
+
@from_address = "QZVSzPeaEJxB9bYuDEL7iWrHSdGbAP3pXV"
|
27
|
+
@prepare_dtrust_transaction_response_p2sh = File.new("spec/test-cases/json/prepare_dtrust_transaction_response_p2sh.json").read
|
28
|
+
@req_params = {:from_address => @from_address, :amount => @amount, :to_address => @to_address}
|
29
|
+
@stub1 = stub_request(:post, "https://block.io/api/v2/prepare_dtrust_transaction").
|
30
|
+
with(
|
31
|
+
body: @req_params.merge({:api_key => @api_key}).to_json,
|
32
|
+
headers: @headers).
|
33
|
+
to_return(status: 200, body: @prepare_dtrust_transaction_response_p2sh, headers: {})
|
34
|
+
|
35
|
+
@create_and_sign_transaction_response_dtrust_p2sh_3_of_5_keys = File.new("spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2sh_3_of_5_keys.json").read
|
36
|
+
@create_and_sign_transaction_response_dtrust_p2sh_4_of_5_keys = File.new("spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2sh_4_of_5_keys.json").read
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
context "3_of_5_keys" do
|
41
|
+
it "success" do
|
42
|
+
|
43
|
+
@blockio.prepare_dtrust_transaction(@req_params)
|
44
|
+
|
45
|
+
expect(@stub1).to have_been_requested.times(1)
|
46
|
+
|
47
|
+
expect(
|
48
|
+
@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_dtrust_transaction_response_p2sh), @private_keys.first(3))
|
49
|
+
).to eq(Oj.safe_load(@create_and_sign_transaction_response_dtrust_p2sh_3_of_5_keys))
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "4_of_5_keys" do
|
55
|
+
it "success" do
|
56
|
+
|
57
|
+
@blockio.prepare_dtrust_transaction(@req_params)
|
58
|
+
|
59
|
+
expect(@stub1).to have_been_requested.times(1)
|
60
|
+
|
61
|
+
expect(@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_dtrust_transaction_response_p2sh), @private_keys)).to eq(Oj.safe_load(@create_and_sign_transaction_response_dtrust_p2sh_4_of_5_keys))
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
context "p2wsh_over_p2sh" do
|
69
|
+
|
70
|
+
before(:each) do
|
71
|
+
|
72
|
+
@blockio = BlockIo::Client.new(:api_key => @api_key)
|
73
|
+
@from_address = "Qg1QzgjUDkwaHeT7Yznuyu2V1keJieDVAF"
|
74
|
+
@prepare_dtrust_transaction_response_p2wsh_over_p2sh = File.new("spec/test-cases/json/prepare_dtrust_transaction_response_p2wsh_over_p2sh.json").read
|
75
|
+
@req_params = {:from_address => @from_address, :amount => @amount, :to_address => @to_address}
|
76
|
+
@stub1 = stub_request(:post, "https://block.io/api/v2/prepare_dtrust_transaction").
|
77
|
+
with(
|
78
|
+
body: @req_params.merge({:api_key => @api_key}).to_json,
|
79
|
+
headers: @headers).
|
80
|
+
to_return(status: 200, body: @prepare_dtrust_transaction_response_p2wsh_over_p2sh, headers: {})
|
81
|
+
|
82
|
+
@create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_3_of_5_keys = File.new("spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_3_of_5_keys.json").read
|
83
|
+
@create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_4_of_5_keys = File.new("spec/test-cases/json/create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_4_of_5_keys.json").read
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
context "3_of_5_keys" do
|
88
|
+
it "success" do
|
89
|
+
|
90
|
+
@blockio.prepare_dtrust_transaction(@req_params)
|
91
|
+
|
92
|
+
expect(@stub1).to have_been_requested.times(1)
|
93
|
+
|
94
|
+
expect(
|
95
|
+
@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_dtrust_transaction_response_p2wsh_over_p2sh), @private_keys.first(3))
|
96
|
+
).to eq(Oj.safe_load(@create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_3_of_5_keys))
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "4_of_5_keys" do
|
102
|
+
it "success" do
|
103
|
+
|
104
|
+
@blockio.prepare_dtrust_transaction(@req_params)
|
105
|
+
|
106
|
+
expect(@stub1).to have_been_requested.times(1)
|
107
|
+
|
108
|
+
expect(
|
109
|
+
@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_dtrust_transaction_response_p2wsh_over_p2sh), @private_keys)
|
110
|
+
).to eq(Oj.safe_load(@create_and_sign_transaction_response_dtrust_p2wsh_over_p2sh_4_of_5_keys))
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
context "witness_v0" do
|
118
|
+
|
119
|
+
before(:each) do
|
120
|
+
|
121
|
+
@blockio = BlockIo::Client.new(:api_key => @api_key)
|
122
|
+
@from_address = "tltc1qtvscupcwnlsykujp98y0jhf8s4x48mzrr3v8v822ytg6vmprvzaqj8jd0h"
|
123
|
+
@prepare_dtrust_transaction_response_witness_v0 = File.new("spec/test-cases/json/prepare_dtrust_transaction_response_witness_v0.json").read
|
124
|
+
@req_params = {:from_address => @from_address, :amount => @amount, :to_address => @to_address}
|
125
|
+
@stub1 = stub_request(:post, "https://block.io/api/v2/prepare_dtrust_transaction").
|
126
|
+
with(
|
127
|
+
body: @req_params.merge({:api_key => @api_key}).to_json,
|
128
|
+
headers: @headers).
|
129
|
+
to_return(status: 200, body: @prepare_dtrust_transaction_response_witness_v0, headers: {})
|
130
|
+
|
131
|
+
@create_and_sign_transaction_response_dtrust_witness_v0_3_of_5_keys = File.new("spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_3_of_5_keys.json").read
|
132
|
+
@create_and_sign_transaction_response_dtrust_witness_v0_4_of_5_keys = File.new("spec/test-cases/json/create_and_sign_transaction_response_dtrust_witness_v0_4_of_5_keys.json").read
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
context "3_of_5_keys" do
|
137
|
+
it "success" do
|
138
|
+
|
139
|
+
@blockio.prepare_dtrust_transaction(@req_params)
|
140
|
+
|
141
|
+
expect(@stub1).to have_been_requested.times(1)
|
142
|
+
|
143
|
+
expect(
|
144
|
+
@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_dtrust_transaction_response_witness_v0), @private_keys.first(3))
|
145
|
+
).to eq(Oj.safe_load(@create_and_sign_transaction_response_dtrust_witness_v0_3_of_5_keys))
|
146
|
+
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "4_of_5_keys" do
|
151
|
+
it "success" do
|
152
|
+
|
153
|
+
@blockio.prepare_dtrust_transaction(@req_params)
|
154
|
+
|
155
|
+
expect(@stub1).to have_been_requested.times(1)
|
156
|
+
|
157
|
+
expect(
|
158
|
+
@blockio.create_and_sign_transaction(Oj.safe_load(@prepare_dtrust_transaction_response_witness_v0), @private_keys)
|
159
|
+
).to eq(Oj.safe_load(@create_and_sign_transaction_response_dtrust_witness_v0_4_of_5_keys))
|
160
|
+
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
|
data/spec/helper_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
describe "Helper.sha256" do
|
3
|
+
it "deadbeef" do
|
4
|
+
expect(BlockIo::Helper.sha256("deadbeef")).to eq("2baf1f40105d9501fe319a8ec463fdf4325a2a5df445adf3f572f626253678c9")
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "Helper.pinToAesKey" do
|
9
|
+
it "deadbeef" do
|
10
|
+
expect(BlockIo::Helper.pinToAesKey("deadbeef", false)).to eq([["b87ddac3d84865782a0edbc21b5786d56795dd52bab0fe49270b3726372a83fe"].pack("H*")].pack("m0"))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "Helper.encrypt" do
|
15
|
+
before(:each) do
|
16
|
+
@encryption_key = BlockIo::Helper.pinToAesKey("deadbeef")
|
17
|
+
@encrypted_data = BlockIo::Helper.encrypt("beadbeef", @encryption_key)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "beadbeef" do
|
21
|
+
expect(@encrypted_data).to eq("3wIJtPoC8KO6S7x6LtrN0g==")
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "Helper.decrypt" do
|
27
|
+
before(:each) do
|
28
|
+
@encryption_key = BlockIo::Helper.pinToAesKey("deadbeef")
|
29
|
+
@bad_encryption_key = BlockIo::Helper.pinToAesKey(SecureRandom.hex(8))
|
30
|
+
@encrypted_data = BlockIo::Helper.encrypt("beadbeef", @encryption_key)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "encryption_key" do
|
34
|
+
@decrypted_data = BlockIo::Helper.decrypt(@encrypted_data, @encryption_key)
|
35
|
+
expect(@decrypted_data).to eq("beadbeef")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "bad_encryption_key" do
|
39
|
+
expect{
|
40
|
+
BlockIo::Helper.decrypt(@encrypted_data, @bad_encryption_key)
|
41
|
+
}.to raise_error(Exception, "Invalid Secret PIN provided.")
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/spec/key_spec.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
describe "Key.from_private_key_hex" do
|
2
|
+
# generates faulty signature with Native Ruby bitcoinrb 0.7.0 unless the private key is padded to be 64 char hex (below is 63 chars)
|
3
|
+
|
4
|
+
context "6c1cefdfd9187b36b36c3698c1362642083dcc1941dc76d751481d3aa29ca65" do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@key = BlockIo::Key.from_private_key_hex("6c1cefdfd9187b36b36c3698c1362642083dcc1941dc76d751481d3aa29ca65")
|
8
|
+
@data_to_sign = "c290b6e7e43ae83124499a06d1c0f8d385cc86f28aad77260599654061847547"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "succeeds" do
|
12
|
+
@signature = @key.sign([@data_to_sign].pack("H*"))
|
13
|
+
expect(@signature.unpack("H*")[0]).to eq("304402204ad00b8fd0918e5a0e9ec353a32139265ab3e633748dc85494561f1cee748551022073b229aad08f7bf62020300a34df587336a30784b29439abb405435413c961f4")
|
14
|
+
expect(@key.verify(@signature, [@data_to_sign].pack("H*"))).to eq(true)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "Key.from_wif" do
|
22
|
+
|
23
|
+
context "L1cq4uDmSKMiViT4DuR8jqJv8AiiSZ9VeJr82yau5nfVQYaAgDdr" do
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
Bitcoin.chain_params = "BTC"
|
27
|
+
@key_low_r = Bitcoin::Key.from_wif("L1cq4uDmSKMiViT4DuR8jqJv8AiiSZ9VeJr82yau5nfVQYaAgDdr")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "match(public_key)" do
|
31
|
+
expect(@key_low_r.public_key_hex).to eq("024988bae7e0ade83cb1b6eb0fd81e6161f6657ad5dd91d216fbeab22aea3b61a0")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "match(private_key)" do
|
35
|
+
expect(@key_low_r.private_key_hex).to eq("833e2256c42b4a41ee0a6ee284c39cf8e1978bc8e878eb7ae87803e22d48caa9")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "sign_with_low_r" do
|
39
|
+
expect(@key_low_r.sign(["e76f0f78b7e7474f04cc14ad1343e4cc28f450399a79457d1240511a054afd63"].pack("H*")).unpack("H*")[0]).to eq("3044022061753424b6936ca4cfcc81b883dab55f16d84d3eaf9d5da77c1e25f54fda963802200d3db78e8f5aac62909c2a89ab1b2b413c00c0860926e824f37a19fa140c79f4")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "Key.from_passphrase" do
|
46
|
+
|
47
|
+
context "deadbeef" do
|
48
|
+
|
49
|
+
before(:each) do
|
50
|
+
@key_low_r = BlockIo::Key.from_passphrase("deadbeef")
|
51
|
+
@data = "e76f0f78b7e7474f04cc14ad1343e4cc28f450399a79457d1240511a054afd63"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "match(public_key)" do
|
55
|
+
expect(@key_low_r.public_key_hex).to eq("02953b9dfcec241eec348c12b1db813d3cd5ec9d93923c04d2fa3832208b8c0f84")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "match(private_key)" do
|
59
|
+
expect(@key_low_r.private_key_hex).to eq("5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "sign_with_low_r" do
|
63
|
+
expect(@key_low_r.sign([@data].pack("H*")).unpack("H*")[0]).to eq("304402204ac97a4cdad5f842e745e27c3ffbe08b3704900baafab602277a5a196c3a4a3202202bacdf06afaf58032383447a9f3e9a42bfaeabf6dbcf9ab275d8f24171d272cf")
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "Key.generate" do
|
71
|
+
|
72
|
+
context "compressed" do
|
73
|
+
|
74
|
+
before(:each) do
|
75
|
+
Bitcoin.chain_params = "BTC"
|
76
|
+
@key = Bitcoin::Key.generate
|
77
|
+
end
|
78
|
+
|
79
|
+
it "compressed?" do
|
80
|
+
expect(@key.compressed?).to eq(true)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
context "uncompressed" do
|
86
|
+
Bitcoin.chain_params = "BTC"
|
87
|
+
end
|
88
|
+
|
89
|
+
it "raises_exception" do
|
90
|
+
expect {Bitcoin::Key.generate(0x00)}.to raise_error(RuntimeError, "key_type must always be Bitcoin::KEY::TYPES[:compressed]")
|
91
|
+
end
|
92
|
+
end
|