Skip to content

Instantly share code, notes, and snippets.

@mruv
Created June 16, 2019 20:31
Show Gist options
  • Save mruv/26aa497fb19e23dfa613ef8c7d19a572 to your computer and use it in GitHub Desktop.
Save mruv/26aa497fb19e23dfa613ef8c7d19a572 to your computer and use it in GitHub Desktop.
const { Api, JsonRpc, RpcError } = require('eosjs');
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');
const fetch = require('node-fetch');
const { TextDecoder, TextEncoder } = require('text-encoding');
const signatureProvider = new JsSignatureProvider(["5JUVf4PgAppQciKbxngmmfWHsmaxGcfKu53X6zpKTK2zg25MsEn"]);
const rpc = new JsonRpc('http://node.dealin.io:1888', { fetch });
const api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
});
// creates a serialized EOS.IO transaction.
//
// this transaction can be then be signed separately and pushed
const createTransaction = async (account, name, actor, permission, data) => {
try {
return await api.transact({
actions: [{
account: account,
name: name,
authorization: [{
actor: actor,
permission: permission,
}],
data: data,
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
broadcast: false,
sign: false
});
} catch (e) {
if (e instanceof RpcError)
console.log(JSON.stringify(e.json, null, 2));
}
}
// Sign a serialized transaction, `unsigned`
//
// The value returned can be PUSHED to the HTTP server
const signTransaction = async (unsigned, provider) => {
const keys = await provider.getAvailableKeys()
unsigned.requiredKeys = keys
// Dealin Node Chain ID -- can change.
unsigned.chainId = "e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473"
let sigs = unsigned.signatures || null
const signed = await provider.sign(unsigned)
if (sigs) {
signed.signatures = signed.signatures.concat(sigs)
}
return signed
}
// configures EOS.IO account.
//
// adds the eosio.code pseudo-authority
const updateAuth = async (account, activePubKey, ownerPrivateKey) => {
const txData = {
// account to be configured
account: account,
auth: {
accounts: [{
permission: {
// should always be the platform account
actor: "dealination2",
// the new pseudo-auth required for an account to use inline actions
permission: "eosio.code"
}, weight: 1
}],
keys: [
{
// active public key
key: activePubKey,
weight: 1
}
],
threshold: 1,
waits: []
},
parent: "owner",
permission: "active"
}
const serializedTx = await createTransaction(
// platform account -- 'eosio' is the platform account for the eosio.system SC.
"eosio",
// action name
"updateauth",
// account signing this transaction
account,
// account permission
"owner",
// request body - action parameters
txData
);
const signedTx = await signTransaction(
serializedTx,
// create a sig-provider with one private key
new JsSignatureProvider([ownerPrivateKey]));
const res = await rpc.push_transaction(signedTx);
return res;
}
(async () => {
const res = await updateAuth(
// account to configure
'ylervjtonchy',
// active pub key
'EOS7Vkw5ZKn2RkVyrz6jo1TEY2Afd4JotpsbmqTdeMkk4hMQFZLDm',
// owner private key - updateauth transactions are signed using the owner private key
'5KUXAxXYoKQrpwo4TsnMUn4vmG2zYrA9UwFxdAFF35TDABFypDc'
);
console.log(res);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment