Skip to content

Commit

Permalink
Merge pull request #1 from fioprotocol/eric/new-tests
Browse files Browse the repository at this point in the history
Add updateauth, renewdomain setdomainpub examples
  • Loading branch information
ericbutz authored Aug 25, 2021
2 parents cda5a2f + c1bc093 commit 57ca5b9
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
109 changes: 109 additions & 0 deletions eosio-updateauth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Adds a new permission to an account. In this example it adds a "regaddress" permission to the account.
* This permission can then be linked to a contract action (see eosio-linkauth.js) to enable a secondary
* account to execute actions on the primary accounts behalf.
*
* This script only uses the fiojs library.
*/

const { Fio } = require('@fioprotocol/fiojs');
fetch = require('node-fetch');
const properties = require('./properties.js');

const privateKey = properties.privateKey,
account = properties.account,
permissionName = 'regaddress',
parent = 'active',
registrarAccount = '', // The account that will register addresses on behalf of the domain owner
permission = 'active',
max_fee = 1000000000000

const baseUrl = properties.server + '/v1/';
const fiourl = baseUrl + 'chain/';

const callFioApiSigned = async (endPoint, txn) => {
const info = await (await fetch(fiourl + 'get_info')).json();
const blockInfo = await (await fetch(fiourl + 'get_block', { body: `{"block_num_or_id": ${info.last_irreversible_block_num}}`, method: 'POST' })).json()
const chainId = info.chain_id;
const currentDate = new Date();
const timePlusTen = currentDate.getTime() + 10000;
const timeInISOString = (new Date(timePlusTen)).toISOString();
const expiration = timeInISOString.substr(0, timeInISOString.length - 1);

const transaction = {
expiration,
ref_block_num: blockInfo.block_num & 0xffff,
ref_block_prefix: blockInfo.ref_block_prefix,
actions: [{
account: txn.account,
name: txn.action,
authorization: [{
actor: txn.actor,
permission: 'active',
}],
data: txn.data,
}]
};

const abiMap = new Map()
const tokenRawAbi = await (await fetch(fiourl + 'get_raw_abi', { body: '{"account_name": "' + txn.account + '"}', method: 'POST' })).json()
abiMap.set(txn.account, tokenRawAbi)

var privateKeys = [txn.privKey];

const tx = await Fio.prepareTransaction({
transaction,
chainId,
privateKeys,
abiMap,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
});

const pushResult = await fetch(fiourl + endPoint, {
body: JSON.stringify(tx),
method: 'POST',
});

const json = await pushResult.json()
return json;
};


const updateauth = async () => {

try {

const result = await callFioApiSigned('push_transaction', {
action: 'updateauth',
account: 'eosio',
actor: account,
privKey: privateKey,
data: {
"account": account,
"permission": permissionName,
"parent": parent,
"auth": {
"threshold": 1,
"keys": [],
"waits": [],
"accounts": [{
"permission": {
"actor": registrarAccount,
"permission": permission
},
"weight": 1
}
]
},
"max_fee": max_fee
}
})

console.log('Result: ', result)
} catch (err) {
console.log('Error: ', err.json)
}
}

updateauth();
42 changes: 42 additions & 0 deletions fio.address-renewdomain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const {FIOSDK } = require('@fioprotocol/fiosdk')
fetch = require('node-fetch')
const properties = require('./properties.js')

const fetchJson = async (uri, opts = {}) => {
return fetch(uri, opts)
}

const baseUrl = properties.server + '/v1/'

const privateKey = properties.privateKey,
publicKey = properties.publicKey,
fio_domain = '',
max_fee = 1000000000000


const renewdomain = async () => {

user = new FIOSDK(
privateKey,
publicKey,
baseUrl,
fetchJson
)

try {
const result = await user.genericAction('pushTransaction', {
action: 'renewdomain',
account: 'fio.address',
data: {
fio_domain: fio_domain,
max_fee: max_fee,
tpid: ''
}
})
console.log('Result: ', result)
} catch (err) {
console.log('Error: ', err)
}
}

renewdomain();
44 changes: 44 additions & 0 deletions fio.address-setdomainpub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const {FIOSDK } = require('@fioprotocol/fiosdk')
fetch = require('node-fetch')
const properties = require('./properties.js')

const fetchJson = async (uri, opts = {}) => {
return fetch(uri, opts)
}

const baseUrl = properties.server + '/v1/'

const privateKey = properties.privateKey,
publicKey = properties.publicKey,
fio_domain = '',
isPublic = 0, // 1 = public, 0 = private
max_fee = 1000000000000


const setdomainpublic = async () => {

user = new FIOSDK(
privateKey,
publicKey,
baseUrl,
fetchJson
)

try {
const result = await user.genericAction('pushTransaction', {
action: 'setdomainpub',
account: 'fio.address',
data: {
fio_domain: fio_domain,
is_public: isPublic,
max_fee: max_fee,
tpid: ''
}
})
console.log('Result: ', result)
} catch (err) {
console.log('Error: ', err.json)
}
}

setdomainpublic();

0 comments on commit 57ca5b9

Please sign in to comment.