hl_cli_demo 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +26 -0
- data/Rakefile +2 -0
- data/bin/hl +130 -0
- data/hyperledger_cli.gemspec +25 -0
- data/lib/data/data.rb +81 -0
- data/lib/hyperledger_cli.rb +4 -0
- data/lib/hyperledger_cli/error_printer.rb +11 -0
- data/lib/hyperledger_cli/version.rb +3 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 332ae2e56b7cf3dc5f82593fcc1395518e8946a8
|
4
|
+
data.tar.gz: 02601fdb9b51b00ec6505d6f82665050550be17f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 615c574876abb0e4f8feafbf3e0b8846e611f3e3bf03c359ec1cfd340064453805a69644bfbf5bb61ebeff0d46b67f66932a43bc0444ac1448630015b25033d5
|
7
|
+
data.tar.gz: 9ccfdbc4da660c9d9e404072fad15cb91a9203a2ed94edc3d1cb0fdc19ade3832508c0eba7e5184443d385e5997bae73a1a1db7ca6709b522c230a3eec8561e3
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Daniel Feichtinger
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# HyperledgerCli
|
2
|
+
|
3
|
+
This is a simple CLI for the hyperledger protocol. It is young software
|
4
|
+
under active development.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Hyperledger CLI can be installed with Rubygems:
|
9
|
+
|
10
|
+
gem 'hyperledger_cli'
|
11
|
+
|
12
|
+
You will then be able to access hyperledger from the command line:
|
13
|
+
|
14
|
+
$ hyperledger
|
15
|
+
|
16
|
+
## Contributing
|
17
|
+
|
18
|
+
1. Fork it ( https://github.com/hyperledger/hyperledger-cli/fork )
|
19
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
20
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
21
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
22
|
+
5. Create a new Pull Request
|
23
|
+
|
24
|
+
## License
|
25
|
+
|
26
|
+
hyperledger CLI is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/hl
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'openssl'
|
6
|
+
require 'rest_client'
|
7
|
+
require 'base64'
|
8
|
+
require 'json'
|
9
|
+
require 'securerandom'
|
10
|
+
require 'colorize'
|
11
|
+
require_relative '../lib/hyperledger_cli/error_printer'
|
12
|
+
require_relative '../lib/data/data'
|
13
|
+
|
14
|
+
class Ledger < Thor
|
15
|
+
desc 'list', 'List all known ledgers'
|
16
|
+
def list
|
17
|
+
Data.ledgers.each do |c|
|
18
|
+
if File.file?("./lib/data/#{c.hash}.json")
|
19
|
+
puts "Name: Northern Rock GPB Liabilities"
|
20
|
+
end
|
21
|
+
puts "Hash: #{c.hash.yellow}"
|
22
|
+
puts "Public Key: #{c.key}"
|
23
|
+
puts "\n"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'register <hash>', 'Registers a new ledger.'
|
28
|
+
def register(hash)
|
29
|
+
puts "New ledger created"
|
30
|
+
puts "Hash: #{ledger[:name]}"
|
31
|
+
puts "Primary Account: #{ledger[:primary_account][:code]}"
|
32
|
+
puts "\n"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Account < Thor
|
37
|
+
desc 'list', 'List all known accounts'
|
38
|
+
def list
|
39
|
+
Data.accounts.each do |a|
|
40
|
+
puts "Public Key: #{a.key.yellow}"
|
41
|
+
if File.file?("./lib/data/#{a.ledger}.json")
|
42
|
+
puts "Balance: £#{a.balance / 100.0}"
|
43
|
+
else
|
44
|
+
puts "Balance: #{a.balance}"
|
45
|
+
end
|
46
|
+
puts "Ledger: #{a.ledger}"
|
47
|
+
puts "\n"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
desc 'register <ledger>', 'Registers a new account in <ledger>.'
|
52
|
+
def register(ledger)
|
53
|
+
a = AccountData.new
|
54
|
+
puts 'New account created'
|
55
|
+
puts "Public Key: #{a.key}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Hyperledger < Thor
|
60
|
+
include HyperledgerCli::ErrorPrinter
|
61
|
+
|
62
|
+
class_option :server, type: :string, default: 'http://hyperledger-staging-1.herokuapp.com'
|
63
|
+
|
64
|
+
desc 'ledger SUBCOMMAND', 'Subcommands relating to ledgers.'
|
65
|
+
subcommand 'ledger', Ledger
|
66
|
+
|
67
|
+
desc 'account SUBCOMMAND', 'Subcommands relating to accounts.'
|
68
|
+
subcommand 'account', Account
|
69
|
+
|
70
|
+
desc 'log', 'List all entries in the log'
|
71
|
+
def log
|
72
|
+
Data.log.each do |l|
|
73
|
+
puts "Id: #{l.id}"
|
74
|
+
puts "View: #{l.view}"
|
75
|
+
puts "Command: #{l.command.green}"
|
76
|
+
puts "Data: #{l.data}"
|
77
|
+
puts "Signature: #{l.signature}"
|
78
|
+
puts "Prepared: #{l.prepared.to_s.yellow}"
|
79
|
+
puts "Committed: #{l.prepared.to_s.yellow}"
|
80
|
+
puts "Executed: #{l.prepared.to_s.yellow}"
|
81
|
+
puts "\n"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
desc 'nodes', 'List all nodes in the consensus pool'
|
86
|
+
def nodes
|
87
|
+
Data.nodes.each do |l|
|
88
|
+
puts "URL: #{l.url.yellow} #{"<- Primary".green if l.url == "https://hl-5/"}"
|
89
|
+
puts "Public Key: #{l.key.to_s}"
|
90
|
+
puts "\n"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
desc 'import <file_path>', 'Import a contract file'
|
95
|
+
def import(file_path)
|
96
|
+
FileUtils.cp file_path, "./lib/data/"
|
97
|
+
end
|
98
|
+
|
99
|
+
desc 'issue <amount> <ledger>', 'Issue <amount> new units to <ledger>.'
|
100
|
+
def issue(amount, ledger)
|
101
|
+
data = { ledger: ledger, amount: amount.to_i, uuid: SecureRandom.uuid }
|
102
|
+
|
103
|
+
key = OpenSSL::PKey::RSA.new File.read("#{ENV['HOME']}/.hyperledger/#{ledger.downcase}.pem")
|
104
|
+
sign = Base64.encode64 key.sign(OpenSSL::Digest::SHA256.new, data.to_json)
|
105
|
+
|
106
|
+
begin
|
107
|
+
resp = RestClient.post "#{options[:server]}/issues", { issue: data.merge({resource_signature: sign}) }.to_json, content_type: :json, accept: :json
|
108
|
+
puts "#{amount} new units issued to #{ledger}"
|
109
|
+
rescue => e
|
110
|
+
print_error(e)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
desc 'transfer <amount> <source> <destination>', 'Transfer <amount> of units from <source> to <destination>'
|
115
|
+
def transfer(amount, source, destination)
|
116
|
+
key = OpenSSL::PKey::RSA.new File.read("#{ENV['HOME']}/.hyperledger/#{source}.pem")
|
117
|
+
|
118
|
+
data = { source: source, destination: destination, amount: amount.to_i, uuid: SecureRandom.uuid }
|
119
|
+
sign = Base64.encode64 key.sign(OpenSSL::Digest::SHA256.new, data.to_json)
|
120
|
+
|
121
|
+
begin
|
122
|
+
resp = RestClient.post "#{options.server}/transfers", { transfer: data.merge({resource_signature: sign}) }.to_json, content_type: :json, accept: :json
|
123
|
+
puts 'Transfer completed!'
|
124
|
+
rescue => e
|
125
|
+
print_error(e)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
Hyperledger.start(ARGV)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hyperledger_cli/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hl_cli_demo"
|
8
|
+
spec.version = HyperledgerCli::VERSION
|
9
|
+
spec.authors = ["Daniel Feichtinger"]
|
10
|
+
spec.email = ["df@hyperledger.org"]
|
11
|
+
spec.summary = "A sample CLI for the Hyperledger protocol."
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.3"
|
21
|
+
spec.add_runtime_dependency "rest-client"
|
22
|
+
spec.add_runtime_dependency "colorize"
|
23
|
+
spec.add_runtime_dependency "thor", "~> 0.19"
|
24
|
+
spec.add_runtime_dependency "rbnacl-libsodium"
|
25
|
+
end
|
data/lib/data/data.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'openssl'
|
3
|
+
require 'rbnacl/libsodium'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
class Data
|
7
|
+
def self.ledgers
|
8
|
+
(1..10).map do |i| LedgerData.new(i) end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.accounts
|
12
|
+
(1..10).map do |i| AccountData.new(i) end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.log
|
16
|
+
(1..1000).map do |i| LogData.new(i) end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.nodes
|
20
|
+
(1..10).map do |i| NodeData.new(i) end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class LedgerData
|
25
|
+
attr :key, :hash
|
26
|
+
def initialize(hash)
|
27
|
+
digest = OpenSSL::Digest::SHA256.new
|
28
|
+
@key = digest.hexdigest "#{hash}+ledger"
|
29
|
+
@hash = digest.hexdigest hash.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class AccountData
|
34
|
+
attr :key, :balance, :ledger
|
35
|
+
def initialize(hash)
|
36
|
+
digest = OpenSSL::Digest::SHA256.new
|
37
|
+
@ledger = digest.hexdigest "1"
|
38
|
+
@key = digest.hexdigest "#{hash}+account"
|
39
|
+
@balance = rand 100_000_000
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class LogData
|
44
|
+
attr :command, :data, :signature, :prepared, :confirmed, :executed, :id, :view
|
45
|
+
def initialize(id)
|
46
|
+
@id = id
|
47
|
+
@view = (id / 20) + 1
|
48
|
+
@command = ["register ledger", "register account", "issue", "transfer"][rand(4)]
|
49
|
+
digest = OpenSSL::Digest::SHA256.new
|
50
|
+
@signature = digest.hexdigest "#{hash}+log"
|
51
|
+
@prepared = @confirmed = @executed = true
|
52
|
+
end
|
53
|
+
|
54
|
+
def data
|
55
|
+
case command
|
56
|
+
when "register ledger"
|
57
|
+
ledger = LedgerData.new(@id)
|
58
|
+
account = AccountData.new(@id)
|
59
|
+
JSON.generate({ledger:{hash: ledger.hash, publicKey: ledger.key, primaryAccountPublicKey: account.key}})
|
60
|
+
when "register account"
|
61
|
+
account = AccountData.new(@id)
|
62
|
+
JSON.generate({account:{publicKey: account.key}})
|
63
|
+
when "issue"
|
64
|
+
ledger = LedgerData.new(rand(9) + 1)
|
65
|
+
JSON.generate({issue:{ledger: ledger.hash, amount: rand(1_000_000)}})
|
66
|
+
when "transfer"
|
67
|
+
account1 = AccountData.new(@id + 2)
|
68
|
+
account2 = AccountData.new(@id + 1)
|
69
|
+
JSON.generate({transfer:{source: account1.key, destination: account2.key, amount: rand(1_000_000)}})
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class NodeData
|
75
|
+
attr :url, :key
|
76
|
+
def initialize(n)
|
77
|
+
@url = "https://hl-#{n}/"
|
78
|
+
key = RbNaCl::SigningKey.generate
|
79
|
+
@key = key.to_bytes.unpack('H*').first
|
80
|
+
end
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hl_cli_demo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.10
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Feichtinger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rest-client
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: colorize
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: thor
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.19'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.19'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rbnacl-libsodium
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- df@hyperledger.org
|
100
|
+
executables:
|
101
|
+
- hl
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/hl
|
111
|
+
- hyperledger_cli.gemspec
|
112
|
+
- lib/data/data.rb
|
113
|
+
- lib/hyperledger_cli.rb
|
114
|
+
- lib/hyperledger_cli/error_printer.rb
|
115
|
+
- lib/hyperledger_cli/version.rb
|
116
|
+
homepage:
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.4.6
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: A sample CLI for the Hyperledger protocol.
|
140
|
+
test_files: []
|