Skip to content

Instantly share code, notes, and snippets.

@hackervera
Last active August 17, 2017 04:13
Show Gist options
  • Save hackervera/26fd139005d900ce47849250161f53f6 to your computer and use it in GitHub Desktop.
Save hackervera/26fd139005d900ce47849250161f53f6 to your computer and use it in GitHub Desktop.
require "dag"
require "rbnacl"
require "bencode"
require "time"
require "socket"
require "json"
include RbNaCl::Util
$dag = DAG.new
$signing_key = RbNaCl::SigningKey.generate
$verify_key = bin2hex($signing_key.verify_key)
def find_parents
p "PARENTS"
$dag.vertices.select{|v| v.successors == []}.map{|v| v[:hash]}
end
def generate_channel(channel_name)
data = {author: $verify_key, channel_name: channel_name}.bencode
hash = bin2hex($signing_key.sign(data))
{channel_name: channel_name, author: $verify_key, hash: hash}
end
def generate_message(body)
parents = find_parents
time = Time.now.utc.iso8601
data = {author: $verify_key, body: body, parents: parents, timestamp: time}.bencode
hash = bin2hex($signing_key.sign(data))
{body: body, author: $verify_key, timestamp: time, hash: hash, parents: parents}
end
# These are the snatch-rpc methods
def new_message(body, author, timestamp, parents, hash)
to_vertex = $dag.add_vertex(method: "new_message", data: {body: body, parents: parents, timestamp: timestamp}, hash: hash)
parents.each do |parent|
from_vertex = $dag.vertices.find{|v| v[:hash] == parent }
$dag.add_edge from: from_vertex, to: to_vertex
end
end
def new_channel(channel_name, author, hash)
$dag.add_vertex(method: "new_channel", data: {author: author, channel_name: channel_name}, hash: hash, )
end
channel = generate_channel("testing")
p channel
new_channel(channel[:channel_name], channel[:author], channel[:hash])
p $dag
message = generate_message("this is a test")
p message
new_message(message[:body], message[:author], message[:timestamp], message[:parents], message[:hash])
server = TCPServer.new 2000
Thread.new{
loop do
client = server.accept
loop do
data = client.gets.strip
incoming = JSON.parse data
p "INCOMING"
p incoming
case incoming["method"]
when "new_message"
new_message(*incoming["params"])
p "EDGES"
p $dag.edges
end
end
end
}
s = TCPSocket.new 'localhost', 2000
message = generate_message("second_message")
notification = {method: "new_message", params: [message[:body], message[:author], message[:timestamp], message[:parents], message[:hash]], id: nil}.to_json
s.puts notification
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment