Skip to content

Instantly share code, notes, and snippets.

@movitto
Created April 30, 2019 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save movitto/5055115049a0fa7befdb7beb8aee1472 to your computer and use it in GitHub Desktop.
Save movitto/5055115049a0fa7befdb7beb8aee1472 to your computer and use it in GitHub Desktop.
XRP Account Generator
#!/usr/bin/ruby
#
# Written by Dev Null Productions <devnullproductions@gmail.com>
#
# Small script to generate new XRPL account as described in the following document:
# https://developers.ripple.com/accounts.html
#
# Simply run it with:
# ruby xag.rb
#
# Output:
# Account: rBfgMVusEncfmtgMg6VZeFEWnZNvaP1j3h
# Public Key: 024D462BEA18564079FDDD32B06726716B62E08A7656DF3D1E1596BB06BC2B58BC
# Private Key: 298C84B55ADACB4D7889FC4A8E38CF64919EE7BB5D27825A8C6987733902C34D
#
# Note: This will just create the credentials for a new account (public & private key, account id).
# The account will not be activated on the XRP Ledger until a payment of the minimum account
# reserve is sent to the account.
require 'securerandom'
require 'openssl'
require 'base58'
class String
# Return bignum corresponding to string
def to_bn
bytes.inject(0) { |bn, b| (bn << 8) | b }
end
end
# Private Key Seed:
# seed = "my secret"
seed = SecureRandom.random_bytes(32)
# Private Key:
priv = OpenSSL::Digest::SHA256.new.digest(seed)
# Public Key:
ecgrp = OpenSSL::PKey::EC::Group.new('secp256k1')
ecpriv = OpenSSL::PKey::EC.new("secp256k1")
pub = ecgrp.generator.mul(priv.to_bn)
# Account ID:
key = pub.to_bn(:compressed).to_s(16)
sha256 = OpenSSL::Digest::SHA256.new
ripemd160 = OpenSSL::Digest::RIPEMD160.new
account_id = [0].pack("C") + ripemd160.digest(sha256.digest([key].pack("H*")))
# Checksum:
chksum = sha256.digest(sha256.digest(account_id))[0..3]
# Full account representation:
account = Base58.binary_to_base58(account_id + chksum, :ripple)
puts "Account: #{account}"
puts "Public Key: #{key}"
puts "Private Key: #{priv.unpack("H*").first.upcase}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment