stellar-base 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1f8db8c0eef99ea37838371a5718d8ab6df3e0d
4
- data.tar.gz: 3170f3c49015a46b0d8337769ea1672194d4f584
3
+ metadata.gz: dd816d7bbac1d2b4938046e55db9545824e94d00
4
+ data.tar.gz: af0d72500eed2bd454006f77148714ca228bfccf
5
5
  SHA512:
6
- metadata.gz: c4ce937a9b9ac9c8b14c1cd8b8557182e1d24c3e6c3d432801c8109fb5df8f3b0b7bb909352368d01cbd67d4d75b46a67495a5e93be274d95aafbbd3a5c76cff
7
- data.tar.gz: 632997ffa01f3bf3d380aabb2b56553cbfba65cd74b92af20bd896f054c54f59ec070238d3aca36739a4937106b9fa1c6cc13e31b36e74f0fe5b386d40fc723d
6
+ metadata.gz: f571ed6a5bb220ec0eb34f056e0642a6441a02e889f113217ba5c08e66848ad281f15d99fc9812fe7b89094287e3df04c132367af08acfabcae9d644eb58ef84
7
+ data.tar.gz: da9dd38df4f482aca38610469e73ab5d5f3f60d2992b9454abc86ce0dcbdf33fc3b3eb87ca9195aab1865612937a65db61dbf9ae94442204bbbdf64560dc0531
data/lib/stellar-base.rb CHANGED
@@ -29,3 +29,4 @@ require_relative './stellar/transaction_envelope'
29
29
  require_relative './stellar/util/strkey'
30
30
  require_relative './stellar/util/continued_fraction'
31
31
  require_relative './stellar/convert'
32
+ require_relative './stellar/networks'
@@ -1,5 +1,5 @@
1
1
  module Stellar
2
2
  module Base
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -27,8 +27,13 @@ module Stellar
27
27
  new(public_key, secret_key)
28
28
  end
29
29
 
30
+ def self.from_network_passphrase(passphrase)
31
+ network_id = Digest::SHA256.digest(passphrase)
32
+ from_raw_seed network_id
33
+ end
34
+
30
35
  def self.master
31
- from_raw_seed("allmylifemyhearthasbeensearching")
36
+ from_raw_seed(Stellar.current_network_id)
32
37
  end
33
38
 
34
39
  def initialize(public_key, secret_key=nil)
@@ -0,0 +1,45 @@
1
+ module Stellar
2
+ # Provides a container for well-known network passphrases, such as the main network and SDF test network
3
+ module Networks
4
+
5
+ PUBLIC = "Public Global Stellar Network ; September 2015"
6
+ TESTNET = "Test SDF Network ; September 2015"
7
+
8
+ end
9
+
10
+ # Configures the default stellar network passphrase for the current process. Unless otherwise
11
+ # specified in a method that needs the passphrase, this value will be used.
12
+ #
13
+ # NOTE: This method is not thread-safe. It's best to just call this at startup once and use the other
14
+ # methods of specifying a network if you need two threads in the same process to communicate with
15
+ # different networks
16
+ #
17
+ # @see Stellar.default_network
18
+ # @see Stellar.on_network
19
+ def self.default_network=(passphrase)
20
+ @default_network = passphrase
21
+ end
22
+
23
+ # Returns the passphrase for the currently-configured network, as set by Stellar.default_network
24
+ # or Stellar.on_network
25
+ def self.current_network
26
+ Thread.current["stellar_network_passphrase"] ||
27
+ @default_network ||
28
+ Stellar::Networks::PUBLIC
29
+ end
30
+
31
+ # Returns the id for the currently configured network, suitable for use in generating
32
+ # a signature base string or making the root account's keypair.
33
+ def self.current_network_id
34
+ Digest::SHA256.digest(self.current_network)
35
+ end
36
+
37
+ # Executes the provided block in the context of the provided network.
38
+ def self.on_network(passphrase, &block)
39
+ old = Thread.current["stellar_network_passphrase"]
40
+ Thread.current["stellar_network_passphrase"] = passphrase
41
+ block.call
42
+ ensure
43
+ Thread.current["stellar_network_passphrase"] = old
44
+ end
45
+ end
@@ -126,7 +126,8 @@ module Stellar
126
126
 
127
127
  def signature_base_prefix
128
128
  val = Stellar::EnvelopeType.envelope_type_tx
129
- Stellar::EnvelopeType.to_xdr(val)
129
+
130
+ Stellar.current_network_id + Stellar::EnvelopeType.to_xdr(val)
130
131
  end
131
132
 
132
133
  def to_envelope(*key_pairs)
@@ -101,6 +101,14 @@ describe Stellar::KeyPair do
101
101
  end
102
102
  end
103
103
 
104
+ describe ".master" do
105
+ subject{ Stellar::KeyPair.master }
106
+
107
+ it "returns a keypair whose raw_seed is the current_network_id" do
108
+ expect(subject.raw_seed).to eql(Stellar.current_network_id)
109
+ end
110
+ end
111
+
104
112
  describe "#raw_public_key" do
105
113
  let(:key_pair){ Stellar::KeyPair.random }
106
114
  subject{ key_pair.raw_public_key }
@@ -0,0 +1,77 @@
1
+ require "spec_helper"
2
+
3
+ describe Stellar, ".default_network=" do
4
+
5
+ before(:each){ Stellar.default_network = "foo" }
6
+ after(:each){ Stellar.default_network = nil }
7
+
8
+ it "sets the value returned by current_network " do
9
+ expect(Stellar.current_network).to eql("foo")
10
+ end
11
+
12
+ end
13
+
14
+ describe Stellar, ".current_network" do
15
+
16
+ after(:each){ Stellar.default_network = nil }
17
+
18
+ it "returns the public network absent any other configuration" do
19
+ expect(Stellar.current_network).to eql(Stellar::Networks::PUBLIC)
20
+ end
21
+
22
+ it "returns the default network if configured and not within a call to on_network" do
23
+ Stellar.default_network = "foo"
24
+ expect(Stellar.current_network).to eql("foo")
25
+ end
26
+
27
+ it "returns the network as specified by on_network, even when a default is set" do
28
+ Stellar.default_network = "foo"
29
+
30
+ Stellar.on_network "bar" do
31
+ expect(Stellar.current_network).to eql("bar")
32
+ end
33
+
34
+ expect(Stellar.current_network).to eql("foo")
35
+ end
36
+ end
37
+
38
+ describe Stellar, ".current_network_id" do
39
+ it "returns the sha256 of the current_network" do
40
+ expect(Stellar.current_network_id).to eql(Digest::SHA256.digest(Stellar.current_network))
41
+ end
42
+ end
43
+
44
+ describe Stellar, ".on_network" do
45
+
46
+ after(:each){ Thread.current["stellar_network_passphrase"] = nil }
47
+
48
+
49
+ it "sets the current_network and a thread local" do
50
+ Stellar.on_network "bar" do
51
+ expect(Stellar.current_network).to eql("bar")
52
+ expect(Thread.current["stellar_network_passphrase"]).to eql("bar")
53
+ end
54
+ end
55
+
56
+
57
+ it "nests" do
58
+ Stellar.on_network "foo" do
59
+ expect(Stellar.current_network).to eql("foo")
60
+ Stellar.on_network "bar" do
61
+ expect(Stellar.current_network).to eql("bar")
62
+ end
63
+ expect(Stellar.current_network).to eql("foo")
64
+ end
65
+ end
66
+
67
+
68
+ it "resets the network value when an error is raised" do
69
+ begin
70
+ Stellar.on_network "foo" do
71
+ raise "kablow"
72
+ end
73
+ rescue
74
+ expect(Stellar.current_network).to_not eql("foo")
75
+ end
76
+ end
77
+ end
@@ -39,4 +39,16 @@ describe Stellar::Transaction do
39
39
  expect(result.signatures.first.signature).to eq(subject.sign(key_pair))
40
40
  end
41
41
  end
42
+
43
+ describe "#signature_base" do
44
+
45
+ it "is prefixed with the current network id" do
46
+ expect(subject.signature_base).to start_with(Stellar.current_network_id)
47
+ end
48
+
49
+ it "includes the envelope type" do
50
+ expect(subject.signature_base[32...36]).to eql("\x00\x00\x00\x02")
51
+ end
52
+
53
+ end
42
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stellar-base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Fleckenstein
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-31 00:00:00.000000000 Z
11
+ date: 2015-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xdr
@@ -366,6 +366,7 @@ files:
366
366
  - lib/stellar/base/version.rb
367
367
  - lib/stellar/convert.rb
368
368
  - lib/stellar/key_pair.rb
369
+ - lib/stellar/networks.rb
369
370
  - lib/stellar/operation.rb
370
371
  - lib/stellar/path_payment_result.rb
371
372
  - lib/stellar/price.rb
@@ -379,6 +380,7 @@ files:
379
380
  - spec/lib/stellar/asset_spec.rb
380
381
  - spec/lib/stellar/convert_spec.rb
381
382
  - spec/lib/stellar/key_pair_spec.rb
383
+ - spec/lib/stellar/networks_spec.rb
382
384
  - spec/lib/stellar/path_payment_result_spec.rb
383
385
  - spec/lib/stellar/price_spec.rb
384
386
  - spec/lib/stellar/thresholds_spec.rb
@@ -428,6 +430,7 @@ test_files:
428
430
  - spec/lib/stellar/asset_spec.rb
429
431
  - spec/lib/stellar/convert_spec.rb
430
432
  - spec/lib/stellar/key_pair_spec.rb
433
+ - spec/lib/stellar/networks_spec.rb
431
434
  - spec/lib/stellar/path_payment_result_spec.rb
432
435
  - spec/lib/stellar/price_spec.rb
433
436
  - spec/lib/stellar/thresholds_spec.rb