Skip to content

Commit

Permalink
Fixing a bug where calling #compressed or #uncompressed on a PublicKe…
Browse files Browse the repository at this point in the history
…y errantly mutated the object rather than returning a deep clone.
  • Loading branch information
wink committed Dec 19, 2013
1 parent 6ad67f9 commit 08402f1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/money-tree/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ def compression=(compression_type = :compressed)
end

def compressed
compressed_key = self.dup
compressed_key = self.class.new raw_key # deep clone
compressed_key.set_point to_i, compressed: true
compressed_key
end

def uncompressed
uncompressed_key = self.dup
uncompressed_key = self.class.new raw_key # deep clone
uncompressed_key.set_point to_i, compressed: false
uncompressed_key
end
Expand Down
3 changes: 2 additions & 1 deletion lib/money-tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def node_for_path(path)
nodes << self
else
i = parse_index(part)
nodes << nodes.last.subnode(i)
node = nodes.last || self
nodes << node.subnode(i)
end
end
if force_public or parts.first == 'M'
Expand Down
36 changes: 36 additions & 0 deletions spec/lib/money-tree/public_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,40 @@
lambda { @key = MoneyTree::PublicKey.new 'THISISNOTAVALIDKEY' }.should raise_error(MoneyTree::Key::KeyFormatNotFound)
end
end

describe "recalcuating public key" do
it "produces same results" do
results = []
100.times do
results << MoneyTree::PublicKey.new('042dfc2557a007c93092c2915f11e8aa70c4f399a6753e2e908330014091580e4b11203096f1a1c5276a73f91b9465357004c2103cc42c63d6d330df589080d2e4').to_s
end
results.uniq.length.should == 1
end
end

describe "#uncompressed" do
before do
@key = MoneyTree::PublicKey.new('022dfc2557a007c93092c2915f11e8aa70c4f399a6753e2e908330014091580e4b')
end

it "does not mutate key" do
before_str = @key.to_s
@key.uncompressed
after_str = @key.to_s
before_str.should == after_str
end
end

describe "#compressed" do
before do
@key = MoneyTree::PublicKey.new('042dfc2557a007c93092c2915f11e8aa70c4f399a6753e2e908330014091580e4b11203096f1a1c5276a73f91b9465357004c2103cc42c63d6d330df589080d2e4')
end

it "does not mutate key" do
before_str = @key.to_s
@key.compressed
after_str = @key.to_s
before_str.should == after_str
end
end
end

0 comments on commit 08402f1

Please sign in to comment.