additional experimentation added also for creating account
(async () => {
Eos = require('eosjs')
const ecc = Eos.modules.ecc
let eos
let promises = [], keys = [], keyPairs = []
for(let i=0; i<5; i++) {
promises.push(ecc.randomKey())
}
const kys = await Promise.all(promises)
keys = kys
keyPairs = keys.map(k => {
return {private: k, public: ecc.privateToPublic(k)}
})
console.log(JSON.stringify(keyPairs, null, 2))
eos = Eos({keyProvider: ["5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"].concat(keys), // fred.abc key plus newly created
expireInSeconds: 60,
chainId: "706a7ddd808de9fc2b8879904f3b392256c83104c1d544b38302cc07d9fca477" // cleos get info - to get this
})
const pubkey = keyPairs[0].public
eos.transaction(tr => {
tr.newaccount({
creator: 'eosio',
name: 'myaccount',
owner: pubkey,
active: pubkey
})
})
let c = await eos.contract('fred.abc')
let r = await c.add({username: 'myaccount', full_name: "Auto Gen 4"}, {
authorization: 'myaccount',
broadcast: true,
sign: true
})
console.log(JSON.stringify(r, null, 2))
let t = await eos.getTableRows({
code: "fred.abc",
scope: "fred.abc",
table: "log",
json: true
})
console.log(JSON.stringify(t,null,2))
})()
contract
deployed contract with account fred.abc, slightly modified from course to allow update and show account name along with id
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
using std::string;
class hello : public eosio::contract {
public:
using contract::contract;
/// @abi action
void add(const name username, const string& full_name){
require_auth(username);
print("Hello, ", name{username});
addEntry(username, name{username}.to_string(), full_name);
}
private:
/// @abi table log i64
struct log{
uint64_t username;
string str_name;
string full_name;
uint64_t primary_key() const {return username;}
EOSLIB_SERIALIZE(log, (username)(str_name)(full_name));
};
typedef multi_index<N(log), log> helloIndex;
void addEntry(const name username, const string& str_name, const string& full_name){
helloIndex entries(_self, _self);
auto iterator = entries.find(username);
if(iterator == entries.end())
entries.emplace(username, [&](auto& logEntry){
logEntry.username = username;
logEntry.str_name = str_name;
logEntry.full_name = full_name;
});
else
entries.modify(iterator, username, [&](auto& logEntry){
logEntry.username = username;
logEntry.str_name = str_name;
logEntry.full_name = full_name;
});
}
};
EOSIO_ABI(hello, (add))