Skip to content

Instantly share code, notes, and snippets.

@nsjames
Created October 11, 2018 14:15
Show Gist options
  • Save nsjames/5ca58c233a5b94a3950a6e894d06b532 to your computer and use it in GitHub Desktop.
Save nsjames/5ca58c233a5b94a3950a6e894d06b532 to your computer and use it in GitHub Desktop.
async onboard(){
this.onboardingError = '';
switch(this.onboarding){
case ONBOARD.START:
if(!await this.scatter.suggestNetwork(network)){
this.onboardingError = 'You must accept the Test Network in your Scatter.';
return false;
} else {
return setTimeout(() => {
this.onboarding = ONBOARD.PKEY;
return this.onboard();
}, 1000)
}
case ONBOARD.PKEY:
const key = await this.scatter.getPublicKey('eos');
if(!key){
this.onboardingError = 'You must provide a public key.';
return false;
}
this.onboardingData.key = key;
const eos = Eos({httpEndpoint:`http://${network.host}:${network.port}`, keyProvider:'5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'});
const hasAccount = await eos.getKeyAccounts(key).then(res => !!res.account_names.length);
this.onboarding = hasAccount ? ONBOARD.LINK : ONBOARD.CREATE;
return this.onboard();
case ONBOARD.CREATE:
return setTimeout(() => {
const eos = Eos({httpEndpoint:`http://${network.host}:${network.port}`, keyProvider:'5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'});
const creator = 'eosio';
const randomName = () => {
const size = 12;
let text = "";
const possible = "abcdefghij12345";
for(let i=0; i<size; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
const name = randomName();
eos.transaction(tr => {
tr.newaccount({
creator: creator,
name: name,
owner: this.onboardingData.key,
active: this.onboardingData.key
})
tr.buyrambytes({
payer: creator,
receiver: name,
bytes: 20000
})
tr.delegatebw({
from: creator,
receiver: name,
stake_net_quantity: '1.0000 EOS',
stake_cpu_quantity: '50.0000 EOS',
transfer: 0
})
}).then(done => {
console.log('done', done);
this.onboarding = ONBOARD.LINK;
return this.onboard();
})
}, 2000);
case ONBOARD.LINK:
const account = {
name:'helloworld',
authority:'active',
publicKey:this.onboardingData.key,
};
const linked = await this.scatter.linkAccount(this.onboardingData.key, account, network);
if(!linked){
this.onboardingError = 'You must link the account we created for you to your Scatter.';
return false;
}
if(!await this.scatter.hasAccountFor(network)){
this.onboardingError = 'Your Scatter did not link the account we created for you. Please try again.';
return false;
}
this.onboarding = ONBOARD.DONE;
return true;
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment