Created
January 31, 2020 13:23
-
-
Save royaltm/055525c851da9433d8a51aed5aed36cb to your computer and use it in GitHub Desktop.
Daedalus ITN1 automatic trusted peers update script for jormungandr-config.yaml
This file contains hidden or 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
require 'json' | |
require 'yaml' | |
require 'uri' | |
require 'net/http' | |
require 'ipaddr' | |
############## CONFIG ############## | |
API_KEY = "YOUR API KEY, REGISTER AT https://adapools.org/register" | |
CONFIG_DIR = 'C:\Program Files\Daedalus - Rewards v1' | |
PEERS_URL = "https://api.adapools.org/v0/peers" | |
CONFIG_FILE_NAME = "jormungandr-config.yaml" | |
UPDATE_CMD = "update.cmd" | |
#################################### | |
res = Net::HTTP.post_form URI(PEERS_URL), { "api_key" => API_KEY } | |
trusted_peers = if res.code == "200" | |
body = JSON.parse(res.body) | |
puts "Limit: #{body["request"]["limit"]}" | |
body["result"]["list"] | |
end | |
if !trusted_peers.is_a?(Array) | |
$stderr.puts res.inspect | |
exit 1 | |
end | |
trusted_peers.map! do |peer| | |
ip, port, node_id = peer.fetch_values("ip", "port", "node_id") | |
ip = IPAddr.new ip | |
address = if ip.ipv4? | |
"/ip4/#{ip}/tcp/#{port}" | |
elsif ip.ipv6? | |
"/ip6/#{ip}/tcp/#{port}" | |
end | |
{"address" => address, "id" => node_id} | |
end | |
config_main = File.expand_path(CONFIG_FILE_NAME, CONFIG_DIR) | |
config = File.read(config_main) | |
config = YAML.load(config) | |
puts "Replacing trusted peers in config:" | |
puts YAML.dump(config) | |
puts | |
puts "with:" | |
puts YAML.dump({"p2p" => {"trusted_peers" => trusted_peers}}) | |
config["p2p"]["trusted_peers"] = trusted_peers | |
config_local = File.expand_path(CONFIG_FILE_NAME, __dir__) | |
File.open(config_local, "wt") do |f| | |
f.write YAML.dump(config) | |
end | |
File.open(File.expand_path(UPDATE_CMD, __dir__), "wt") do |f| | |
f.puts "COPY \"#{config_local.gsub(/\//, '\\')}\" \"#{config_main.gsub(/\//, '\\')}\"", "PAUSE" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment