lib/bitcoin/wallet/keystore.rb in bitcoin-ruby-0.0.1 vs lib/bitcoin/wallet/keystore.rb in bitcoin-ruby-0.0.2
- old
+ new
@@ -1,5 +1,7 @@
+# encoding: ascii-8bit
+
require 'json'
require 'stringio'
module Bitcoin::Wallet
@@ -10,10 +12,11 @@
# Initialize keystore.
# [config] Hash of settings ({:file => "/foo/bar.json"})
def initialize config
@config = Hash[config.map{|k,v|[k.to_sym,v]}]
+ @config[:file].sub!("~", ENV["HOME"]) if @config[:file].is_a?(String)
@keys = []
load_keys
end
# List all stored keys.
@@ -90,13 +93,14 @@
# Import key from given +base58+ string.
# (See Bitcoin::Key.from_base58)
def import(base58, label = nil)
raise ArgumentError, "Label #{label} already in use" if label && find_key(label)
key = Bitcoin::Key.from_base58(base58)
+ raise ArgumentError, "Address #{key.addr} already in use" if label && find_key(key.addr)
@keys << {:label => label, :addr => key.addr, :key => key}
save_keys
- key.addr
+ key
end
# Load keys from file.
# If file is empty this will generate a new key
# and store it, creating the file.
@@ -137,10 +141,10 @@
if @config[:file].is_a?(StringIO)
@config[:file].reopen
dumper.call(@config[:file])
@config[:file].rewind
- elsif File.exists?(@config[:file])
+ else
File.open(@config[:file], 'w'){|file| dumper.call(file) }
end
end
private