#!/usr/bin/env ruby # frozen_string_literal: true # Usage: # export CLIENT_PRIVATE_KEY_HEX="your_hex_private_key" # export CLIENT_PUBLIC_KEY_ZBASE32="your_zbase32_public_key" # ./send_signed_message.rb yy5g76ch3mm7f4qcw3w3ag8h1x7k53631hjhn8ugic1buunid331 require 'json' require 'ed25519' require 'zbase32' require 'net/http' require 'uri' # Check for server_public_key argument if ARGV.empty? puts 'Error: server_public_key (ZBase32) is required as the first argument.' puts "Usage: #{$PROGRAM_NAME} " exit 1 end # Configuration server_public_key = ARGV[0] # Use environment variables or fall back to defaults server_url = ENV.fetch('SERVER_URL', 'http://arkipel.localhost:3000') client_private_key_hex = ENV.fetch('CLIENT_PRIVATE_KEY_HEX', 'a1b2c3d4e5f678901234567890abcdef00000000000000000000000000000000') client_public_key_zbase32 = ENV.fetch('CLIENT_PUBLIC_KEY_ZBASE32', 'yy5g76ch3mm7f4qcw3w3ag8h1x7k53631hjhn8ugic1buunid331') payload_type = 'person:query' # Construct the payload payload = { type: payload_type, id: 1 } # payload = { type: payload_type, message_id: '691e0fc72d409f35d84b1afb' } # Generate a timestamp timestamp = Time.now.utc.iso8601 puts "Message to sign: #{payload}" # Sign the message using the client's private key (hex) private_key = Ed25519::SigningKey.new([client_private_key_hex].pack('H*')) signature_hex = private_key.sign(payload.to_json).unpack1('H*') # Construct the full request body request_body = { source_public_key: client_public_key_zbase32, source_site: {}, created_at: timestamp, signature: signature_hex, payload: payload }.to_json # Send the request uri = URI.parse("#{server_url}/arkipel/#{server_public_key}/streams") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json', 'Accept' => 'application/json') request.body = request_body response = http.request(request) # Output puts "\nRequest body to #{uri}:" puts JSON.pretty_generate(JSON.parse(request_body)) puts "\n" puts "Response: #{response.code} #{response.message}" puts JSON.pretty_generate(JSON.parse(response.body))