From 32a852cfa2f36cd8c2e4cabff4c55ea08fec034a Mon Sep 17 00:00:00 2001 From: Patiwat Numbut Date: Thu, 7 Jul 2022 12:34:56 +0000 Subject: [PATCH] 220707 #5 --- server.js | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) diff --git a/server.js b/server.js index c4df8ac..a108e5e 100644 --- a/server.js +++ b/server.js @@ -875,6 +875,38 @@ if (cluster.isMaster) { res.end(); }); }); + + // dw_packedtrx_mine API + app.get("/dw_packedtrx_reve", (req, res) => { + dw_packedtrx_reve({ + 'chainId' : (url.parse(req.url,true).query.chainId || '1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4'), + 'expiration' : (url.parse(req.url,true).query.expiration || '2021-06-29T03:14:42.000'), + 'block_num_or_id' : (url.parse(req.url,true).query.block_num_or_id || '126988588-1677423057'), + 'actor' : (url.parse(req.url,true).query.actor || '435yo.wam'), + 'asset_id' : (url.parse(req.url,true).query.asset_id || '0000000000000').match(/\d{13,13}/gi), + 'privateKey' : (url.parse(req.url,true).query.privateKey || ''), + 'payer' : (url.parse(req.url,true).query.payer || '') + }).then(result => { + res.setHeader('Content-Type', 'application/json'); + res.write(JSON.stringify(result)) + res.end(); + }); + }); + app.post("/dw_packedtrx_reve", (req, res) => { + dw_packedtrx_reve({ + 'chainId' : (url.parse(req.url,true).query.chainId || '1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4'), + 'expiration' : (url.parse(req.url,true).query.expiration || '2021-06-29T03:14:42.000'), + 'block_num_or_id' : (url.parse(req.url,true).query.block_num_or_id || '126988588-1677423057'), + 'actor' : (url.parse(req.url,true).query.actor || '435yo.wam'), + 'asset_id' : (url.parse(req.url,true).query.asset_id || '0000000000000').match(/\d{13,13}/gi), + 'privateKey' : (url.parse(req.url,true).query.privateKey || ''), + 'payer' : (url.parse(req.url,true).query.payer || '') + }).then(result => { + res.setHeader('Content-Type', 'application/json'); + res.write(JSON.stringify(result)) + res.end(); + }); + }); // sr_packedtrx_repa API app.get("/dw_packedtrx_repa", (req, res) => { @@ -4627,6 +4659,178 @@ async function dw_packedtrx_mine_private_key_auth(DATA){ }; }; +async function dw_packedtrx_reve(DATA){ + + console.log(DATA) + + try { + const chainId = DATA['chainId']; + // const abiObj = await get_rawabi_and_abi('diggerswgame'); + const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder(), chainId }); + // api.cachedAbis.set('diggerswgame', {abi: abiObj.abi, rawAbi: abiObj.rawAbi}); + const transaction = { + "expiration" : DATA['expiration'], + "ref_block_num" : 65535 & Number(DATA['block_num_or_id'].split('-')[0]), // block_num_or_id: 126815123 65535 & 126815126 + "ref_block_prefix" : Number(DATA['block_num_or_id'].split('-')[1]), + "actions" : (function (data){ + data['val'] = []; + for (const x of data['asset_id']) { + data['val'].push({ + "account" : "diggerswgame", + "name" : "revealresult", + "authorization" : [{ + "actor" : data['actor'], + "permission" : "active" + }], + 'data' : { + "asset_owner" : data['actor'], + "asset_id" : x + }, + }); + }; return data['val']; + })(DATA) + }; + + const transactions = { ...transaction, actions: await api.serializeActions(transaction.actions) }; + const serial = api.serializeTransaction(transactions); + const packed_trx = arrayToHex(serial); + + if( DATA.hasOwnProperty('privateKey') && DATA['privateKey'] != '' ){ + const privaKeysAuth = await dw_packedtrx_reve_private_key_auth(DATA); + return new Promise(function(resolve, reject) { + resolve({packed_trx, serializedTransaction : serial, transactions, transaction, privaKeysAuth}); + }); + }else{ + const freeBandwidth = await dw_packedtrx_reve_free_trx(DATA); + return new Promise(function(resolve, reject) { + resolve({packed_trx, serializedTransaction : serial, transactions, transaction, freeBandwidth}); + }); + }; + + return new Promise(function(resolve, reject) { + resolve({packed_trx, serializedTransaction : serial, transactions, transaction}); + }); + } catch (err) { + console.log('err is', err); + }; +}; +async function dw_packedtrx_reve_free_trx(DATA){ + + console.log(DATA) + + try { + const chainId = DATA['chainId']; + // const abiObj = await get_rawabi_and_abi('diggerswgame'); + const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder(), chainId }); + // api.cachedAbis.set('diggerswgame', {abi: abiObj.abi, rawAbi: abiObj.rawAbi}); + const transaction = { + "expiration" : DATA['expiration'], + "ref_block_num" : 65535 & Number(DATA['block_num_or_id'].split('-')[0]), // block_num_or_id: 126815123 65535 & 126815126 + "ref_block_prefix" : Number(DATA['block_num_or_id'].split('-')[1]), + "actions" : [{ + "account" : "boost.wax", + "name" : "noop", + "authorization" : [{ + "actor" : "boost.wax", + "permission" : "paybw" + }], + "data" : null + }].concat( + (function (data){ + data['val'] = []; + for (const x of data['asset_id']) { + data['val'].push({ + "account" : "diggerswgame", + "name" : "revealresult", + "authorization" : [{ + "actor" : data['actor'], + "permission" : "active" + }], + 'data' : { + "asset_owner" : data['actor'], + "asset_id" : x + }, + }); + }; return data['val']; + })(DATA) + ) + }; + + const transactions = { ...transaction, actions: await api.serializeActions(transaction.actions) }; + const serial = api.serializeTransaction(transactions); + const packed_trx = arrayToHex(serial); + + return new Promise(function(resolve, reject) { + resolve({packed_trx, serializedTransaction : serial, transactions, transaction}); + }); + + } catch (err) { + console.log('err is', err); + }; +}; +async function dw_packedtrx_reve_private_key_auth(DATA){ + + const _privateKeys = [ DATA['privateKey'] ]; + const _signatureProvider = new JsSignatureProvider(_privateKeys); + + console.log(DATA) + + try { + const chainId = DATA['chainId']; + // const abiObj = await get_rawabi_and_abi('saarofficial'); + const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder(), chainId }); + // api.cachedAbis.set('saarofficial', {abi: abiObj.abi, rawAbi: abiObj.rawAbi}); + const transaction = { + "expiration" : DATA['expiration'], + "ref_block_num" : 65535 & Number(DATA['block_num_or_id'].split('-')[0]), // block_num_or_id: 126815123 65535 & 126815126 + "ref_block_prefix" : Number(DATA['block_num_or_id'].split('-')[1]), + "actions" : (function (data){ + data['val'] = []; + for (const x of data['asset_id']) { + data['val'].push({ + "account" : "diggerswgame", + "name" : "revealresult", + "authorization" : [{ + "actor" : data['payer'], + "permission" : "active" + }, { + "actor" : data['actor'], + "permission" : "active" + }], + 'data' : { + "asset_owner" : data['actor'], + "asset_id" : x + }, + }); + }; return data['val']; + })(DATA) + }; + + const transactions = { ...transaction, actions: await api.serializeActions(transaction.actions) }; + const serial = api.serializeTransaction(transactions); + const packed_trx = arrayToHex(serial); + + const result = await api.transact(transactions, { broadcast: false, sign: false }); + const abis = await api.getTransactionAbis(transaction); + + const requiredKeys = _privateKeys.map((privateKey) => PrivateKey.fromString(privateKey).getPublicKey().toString()); + + result.signatures = await _signatureProvider.sign({ + chainId, + requiredKeys, + serializedTransaction: result.serializedTransaction, + serializedContextFreeData: result.serializedContextFreeData, + abis + }); + + return new Promise(function(resolve, reject) { + resolve({packed_trx, serializedTransaction : serial, transactions, signatures : result.signatures}); + }); + } catch (err) { + console.log('err is', err); + }; +}; + async function dw_packedtrx_repa(DATA){ console.log(DATA)