Skip to content

Instantly share code, notes, and snippets.

@DylanVerstraete
Created May 9, 2023 11:23
Show Gist options
  • Save DylanVerstraete/8b1a4439721ab078e722fb09abc3e8a1 to your computer and use it in GitHub Desktop.
Save DylanVerstraete/8b1a4439721ab078e722fb09abc3e8a1 to your computer and use it in GitHub Desktop.
const { ApiPromise, WsProvider } = require('@polkadot/api')
const { Keyring } = require('@polkadot/keyring')
const crypto = require('@polkadot/util-crypto')
const axios = require('axios')
const blake = require('blake')
async function activateAccount () {
const provider = new WsProvider('wss://tfchain.dev.grid.tf')
const api = await ApiPromise.create({ provider })
// Construct the keyring after the API (crypto has an async init)
const keyring = new Keyring({ type: 'sr25519' })
const mnemonic = crypto.mnemonicGenerate()
const key = keyring.addFromUri(mnemonic)
console.log(key.address)
console.log(mnemonic)
// Fund account
await fundAccount(key.address)
const documentLink = 'https://library.threefold.me/info/legal/#/tfgrid/terms_conditions_tfgrid3'
const document = await axios.get(documentLink)
const documentHash = blake.blake2bHex(document.data)
// Accept T and C
let nonce = await api.rpc.system.accountNextIndex(key.address)
await api.tx.tfgridModule.userAcceptTc(documentLink, documentHash).signAndSend(key, { nonce })
const pk = ''
const relay = 'wss://relay.grid.tf'
// Create Twin
nonce = await api.rpc.system.accountNextIndex(key.address)
api.tx.tfgridModule.createTwin(relay, pk, { nonce })
}
async function fundAccount (accountId) {
return axios.post('https://activation.grid.tf/activation/activate', {
accountId
})
}
activateAccount()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment