@abuiles/

CreateStellarAccount

Nodejs

Shows how to create a Stellar account in the test network.

fork
loading
Files
  • index.js
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7992330364495039","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgU3RlbGxhclNkayA9IHJlcXVpcmUoJ3N0ZWxsYXItc2RrJykKY29uc3QgZmV0Y2ggPSByZXF1aXJlKCdub2RlLWZldGNoJykKCi8vIEdlbmVyYXRlcyBhIGtleXBhaXIgYW5kIGZ1bmRzIGFjY291bnQgd2l0aCBmcmllbmRib3QKYXN5bmMgZnVuY3Rpb24gY3JlYXRlQWNjb3VudCgpIHsKICBjb25zdCBwYWlyID0gU3RlbGxhclNkay5LZXlwYWlyLnJhbmRvbSgpCiAgY29uc29sZS5sb2coJ1JlcXVlc3RpbmcgTHVtZW5zJykKCiBhd2FpdCBmZXRjaChgaHR0cHM6Ly9ob3Jpem9uLXRlc3RuZXQuc3RlbGxhci5vcmcvZnJpZW5kYm90P2FkZHI9JHtwYWlyLnB1YmxpY0tleSgpfWApCgogIHJldHVybiBwYWlyCn0KCmFzeW5jIGZ1bmN0aW9uIHJ1bigpIHsKICBjb25zdCBwYWlyID0gYXdhaXQgY3JlYXRlQWNjb3VudCgpCgogIGNvbnNvbGUubG9nKGAKICAgIENvbmdyYXRzLCB5b3UgaGF2ZSBhIFN0ZWxsYXIgYWNjb3VudCBpbiB0aGUgdGVzdCBuZXR3b3JrIQogICAgc2VlZDogJHtwYWlyLnNlY3JldCgpfQogICAgaWQ6ICR7cGFpci5wdWJsaWNLZXkoKX0KICBgKQoKICBjb25zdCB1cmwgPSBgaHR0cHM6Ly9ob3Jpem9uLXRlc3RuZXQuc3RlbGxhci5vcmcvYWNjb3VudHMvJHtwYWlyLnB1YmxpY0tleSgpfWAKCiAgY29uc29sZS5sb2coYAogICAgTG9hZGluZyBhY2NvdW50IGZyb20gdGVzdCBuZXR3b3JrOgogICAgJHt1cmx9CiAgYCkKCiAgY29uc3QgcmVzcG9uc2UgPSBhd2FpdCBmZXRjaCh1cmwpCiAgY29uc3QgcGF5bG9hZCA9IGF3YWl0IHJlc3BvbnNlLmpzb24oKQp9CgpydW4oKQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const StellarSdk = require('stellar-sdk')
const fetch = require('node-fetch')

// Generates a keypair and funds account with friendbot
async function createAccount() {
  const pair = StellarSdk.Keypair.random()
  console.log('Requesting Lumens')

 await fetch(`https://horizon-testnet.stellar.org/friendbot?addr=${pair.publicKey()}`)

  return pair
}

async function run() {
  const pair = await createAccount()

  console.log(`
    Congrats, you have a Stellar account in the test network!
    seed: ${pair.secret()}
    id: ${pair.publicKey()}
  `)

  const url = `https://horizon-testnet.stellar.org/accounts/${pair.publicKey()}`

  console.log(`
    Loading account from test network:
    ${url}
  `)

  const response = await fetch(url)
  const payload = await response.json()
}

run()
node v10.16.0