Skip to content

Instantly share code, notes, and snippets.

@tiffany4772
Created June 16, 2020 00:42
Show Gist options
  • Save tiffany4772/63fa99229044649b48faac1a247a5d03 to your computer and use it in GitHub Desktop.
Save tiffany4772/63fa99229044649b48faac1a247a5d03 to your computer and use it in GitHub Desktop.
set-home-domain.js
// Sample code to log a validator's home domain on ledger:
//
// Copyright (c) 2020, Tiffany Hayden
// Licensed under the 3-Clause BSD License
const StellarSdk = require('stellar-sdk')
function usage() {
console.log('');
console.log('Usage:');
console.log('node set-home-domain.js [-nosubmit] [-xdr] [-testnet] [-fund <source account seed>] <validator seed> <domain>');
}
console.log('This tool will help you prepare and submit a transaction to');
console.log('set the home domain for your Stellar validator.');
var args = process.argv.slice(2);
if (args.length == 0) {
usage();
return;
}
console.log('');
console.log('Parsing command line arguments');
// Whether to submit the transaction or not
var submitTx = true;
// Determine whether to show the XDR
var showXDR = false;
// The keys used to fund the validator's account
var clFundKeys = null;
// The keys for the validator
var clValidatorKeys = null;
// The network to which we will connect
var clNetwork = StellarSdk.Networks.PUBLIC;
var clServer = 'https://horizon.stellar.org';
while (args.length > 2) {
if (args[0] == '-xdr') {
showXDR = true;
args.splice(0, 1);
if (args.length == 0) {
usage();
return;
}
continue;
}
if (args[0] == '-nosubmit') {
submitTx = false;
showXDR = true;
args.splice(0, 1);
if (args.length == 0) {
usage();
return;
}
continue;
}
if (args[0] == '-testnet') {
clNetwork = StellarSdk.Networks.TESTNET;
clServer = 'https://horizon-testnet.stellar.org';
args.splice(0, 1);
if (args.length == 0) {
usage();
return;
}
continue;
}
if (args[0] == '-fund') {
if (args.length < 2) {
console.log('Error: -fund requires an argument');
return;
}
try {
clFundKeys = StellarSdk.Keypair.fromSecret(args[1]);
} catch (e) {
console.log('Error: Unable to parse funding account seed: ' + e);
return;
}
args.splice(0, 2);
continue;
}
usage();
return;
}
if (args.length != 2) {
usage();
return;
}
try {
clValidatorKeys = StellarSdk.Keypair.fromSecret(args[0]);
if (clFundKeys == clValidatorKeys) {
console.log('You are trying to fund an account from itself.');
return;
}
}
catch (e) {
console.log('Error: Unable to parse funding account seed: ' + e);
return;
}
console.log('');
(async () => {
try {
console.log('Connecting to ' + clServer);
console.log('');
const server = new StellarSdk.Server(clServer);
console.log('Building transaction to set domain for ' + clValidatorKeys.publicKey() + '.');
const account = await server.loadAccount(clFundKeys.publicKey());
var builder = new StellarSdk.TransactionBuilder(account, {
fee: 100,
memo: StellarSdk.Memo.text('https://armajeddon.com/'),
networkPassphrase: clNetwork
});
console.log('');
console.log('The transaction will:');
if (clFundKeys.publicKey() != clValidatorKeys.publicKey()) {
console.log(' - Send 1 XLM from ' + clFundKeys.publicKey());
console.log(' to fund a new account for the validator.');
builder = builder.addOperation(StellarSdk.Operation.createAccount({
destination: clValidatorKeys.publicKey(),
startingBalance: '1'
}))
}
console.log(' - Set the domain field to: \'' + args[1] + '\'.');
builder = builder.addOperation(StellarSdk.Operation.setOptions({
source: clValidatorKeys.publicKey(),
homeDomain: args[1]
}));
var tx = builder.setTimeout(5).build();
if (clFundKeys.publicKey() != clValidatorKeys.publicKey())
tx.sign(clFundKeys);
tx.sign(clValidatorKeys);
if (showXDR) {
console.log('');
console.log('The XDR for the transaction is:');
console.log(tx.toXDR());
}
if (submitTx) {
console.log('');
console.log('Submitting the transaction');
const result = await server.submitTransaction(tx);
console.log('');
console.log('The transaction was successfully submitted.');
console.log('See: ' + ._links.transaction.href);
}
}
catch (e) {
console.log('Error: An exception occured: ' + e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment