Created
November 3, 2019 12:12
-
-
Save 44uk/1ba813a1d72cbb443ccf4c6816236d37 to your computer and use it in GitHub Desktop.
Generate nem2-cli profile .rc from address.yaml generated by catapult-service-bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# ruby profile_generator.rb \ | |
# -n MAIN_NET \ | |
# -b ./data/peer-node-0/00000/00001.dat \ | |
# -u http://host.test:3000 \ | |
# build/generated-addresses/addresses.yaml \ | |
# > .nem2rc.json | |
# | |
require 'uri' | |
require 'json' | |
require 'yaml' | |
require 'net/http' | |
require 'optparse' | |
# to be singular name | |
PROFILE_NAME_ASSIGNMENTS = { | |
"peer_nodes" => "peer_node", | |
"api_nodes" => "api_node", | |
"rest_gateways" => "rest_gateway", | |
"nemesis_addresses_harvesting" => "nemesis_address_harvesting", | |
"nemesis_generation_hash" => "nemesis_generation_hash", | |
"nemesis_signer_private_key" => "nemesis_signer_private_key", | |
"nemesis_addresses" => "nemesis_address", | |
} | |
DICT_NETWORK_TYPE = { | |
"MIJIN_TEST" => 144, | |
"MIJIN" => 96, | |
"MAIN_NET" => 104, | |
"TEST_NET" => 152, | |
} | |
class ProfileGenerator | |
class CLI | |
def parse_options(argv = ARGV) | |
op = OptionParser.new | |
self.class.module_eval do | |
define_method(:usage) do |msg = nil| | |
puts op.to_s | |
puts "error: #{msg}" if msg | |
exit 1 | |
end | |
end | |
# default value | |
opts = { | |
network: 'MIJIN_TEST', | |
url: 'http://localhost:3000', | |
uglify: false | |
} | |
# Network | |
op.on('-n', '--network <network>', "Network Type (MAIN_NET, TEST_NET, MIJIN, MIJIN_TEST) (default: #{opts[:network]})") { |v| | |
opts[:network] = v | |
} | |
# URL | |
op.on('-u', '--url <url>', "NEM2 Node URL. (default: #{opts[:url]})") { |v| | |
opts[:url] = v | |
} | |
# Generation Hash | |
op.on('-h', '--hash <generation_hash>', "Network GenerationHash.") { |v| | |
opts[:generation_hash] = v | |
} | |
# Block path | |
op.on('-b', '--block <block_path>', "Block file path.") { |v| | |
opts[:block_path] = v | |
} | |
# Prettify | |
op.on('-g', '--uglify', "Uglify output JSON profile.") { |v| | |
opts[:uglify] = v | |
} | |
op.banner += ' path/to/addresses.yml' | |
begin | |
args = op.parse(argv) | |
rescue OptionParser::InvalidOption => e | |
usage e.message | |
end | |
input = args.first | |
unless input | |
usage 'Input file path is required.' | |
end | |
unless File.exist?(input) | |
abort "File(#{input}) is not found." | |
end | |
[opts, args] | |
end | |
def run | |
opts, args = parse_options | |
input = *args | |
if opts[:block_path] && opts[:generation_hash].nil? | |
blockfile = File.binread(opts[:block_path]) | |
byte_start, byte_size = 4900, 32 | |
opts[:generation_hash] = blockfile[byte_start...byte_start + byte_size] | |
.each_byte.map { |b| b.to_s(16).rjust(2, '0') } | |
.join | |
.upcase | |
elsif opts[:generation_hash].nil? | |
uri = URI.parse(opts[:url]) | |
uri.path = '/block/1' | |
resp = JSON.parse(Net::HTTP.get(uri), symbolize_names: true) | |
opts[:generation_hash] = resp[:meta][:generationHash] | |
end | |
Generator.run(input, opts) | |
end | |
end | |
class Generator | |
def self.run(input, opts) | |
open(File.join(input)) do |f| | |
y = YAML.load(f) | |
profiles = y.keys.inject do |accum, key| | |
accounts = y[key] | |
accounts.each.with_index.inject({}) do |accum2, (account, idx)| | |
profile_name = "#{PROFILE_NAME_ASSIGNMENTS[key]}_#{idx}" | |
accum2[profile_name] = { | |
_address: account["address"], | |
_publicKey: account["public"], | |
privateKey: account["private"], | |
networkType: DICT_NETWORK_TYPE[opts[:network]], | |
url: opts[:url], | |
networkGenerationHash: opts[:generation_hash] | |
} | |
accum2 | |
end | |
end | |
print JSON.send(opts[:uglify] ? :generate : :pretty_generate , profiles) | |
end | |
end | |
end | |
end | |
ProfileGenerator::CLI.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment