Created
January 20, 2019 17:31
-
-
Save bryanjos/09f69d7fd84919a0324e5647d2f29ebd to your computer and use it in GitHub Desktop.
Add IPFS folder to a Stellar Account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const StellarSDK = require('stellar-sdk') | |
const IPFS = require('ipfs') | |
const axios = require('axios') | |
/** | |
* Promisifies the initialize process of an IPFS node | |
*/ | |
function initIPFSNode() { | |
return new Promise((resolve, reject) => { | |
const node = new IPFS() | |
node.on('ready', () => { | |
resolve(node) | |
}) | |
node.on('error', error => { | |
reject(error) | |
}) | |
}) | |
} | |
/** | |
* Creates a Stellar Account on the test network, | |
* Funds it with friendbot, | |
* Creates a folder in IPFS, and | |
* Adds that folder the the accounts data | |
* Returns the create account keypair | |
*/ | |
async function createAccount() { | |
// Create keypair | |
const pair = StellarSDK.Keypair.random() | |
//Fund with friendbot to make it an actual account | |
await axios.get(`https://friendbot.stellar.org?addr=${pair.publicKey()}`) | |
//Inits the IPFS node | |
const node = await initIPFSNode() | |
// We are on the test network | |
const server = new StellarSDK.Server('https://horizon-testnet.stellar.org') | |
StellarSDK.Network.useTestNetwork() | |
// Load the account for use | |
const account = await server.loadAccount(pair.publicKey()) | |
//Our directory in IPFS will begin with the public key name | |
const docsDirectory = `/${pair.publicKey()}` | |
await node.files.mkdir(docsDirectory) | |
//Add the directory to our created account's data | |
const transaction = new StellarSDK.TransactionBuilder(account) | |
.addOperation( | |
StellarSDK.Operation.manageData({ | |
name: 'docs', | |
value: docsDirectory, | |
}) | |
) | |
.build() | |
//Sign and submit the transaction | |
transaction.sign(pair) | |
await server.submitTransaction(transaction) | |
//Stop our IPFS node | |
await node.stop() | |
// Return the keypair | |
return pair | |
} | |
module.exports = { | |
createAccount, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const index = require('../src/index') | |
const StellarSDK = require('stellar-sdk') | |
const chai = require('chai') | |
const expect = chai.expect | |
describe('createAccount', async () => { | |
it('creates account', async () => { | |
const pair = await index.createAccount() | |
const server = new StellarSDK.Server('https://horizon-testnet.stellar.org') | |
StellarSDK.Network.useTestNetwork() | |
const account = await server.loadAccount(pair.publicKey()) | |
// Value will be in base64 so we have to decode it | |
const docs = await account.data({key: 'docs'}) | |
expect(Buffer.from(docs.value, 'base64').toString()).to.equal( | |
'/' + pair.publicKey() | |
) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment