UNPKG

wowok

Version:

Wowok Blockchain TypeScript API

1 lines 5.31 kB
import{getFullnodeUrl,WowClient}from'../../client/index.js';import{Transaction}from'../../transactions/index.js';import{toTxObject,MAX_GUARD_TABLE_ITEM_LENGTH,PackageAddress,parseValueType}from'../common.js';import{W_ERROR,WErrors}from'../exception.js';import{Account}from'../local/account.js';import{Config}from'../local/config.js';import{ObjectType,query_objects}from'../query/object.js';import{valueToBcsBytes}from'../util.js';import{getCallEnvAccount}from'./util.js';export const MAX_GUARD_COUNT_ONCE=0x14;export const verify_guard=async(a,b)=>{b['guard']['length']>MAX_GUARD_COUNT_ONCE&&W_ERROR(WErrors['InvalidParam'],'guards\x20invalid:\x20guard\x20count\x20exceeds\x20maximum\x20limit\x20'+MAX_GUARD_COUNT_ONCE);const c=new Transaction(),d=newPassport(c);for(const f of b['guard']){await guardAdd(c,d,f['object'],b['submission']['find'](g=>g['guard']===f['object'])?.['submission']??[],f['impack']);}verify(c,d),freeze(c,d);let e=await Account['Instance']()['sign_and_commit'](c,await getCallEnvAccount(a),a['network']);!e&&W_ERROR(WErrors['Fail'],'sign\x20and\x20commit\x20failed');if(e?.['digest']&&!e?.['objectChanges']){const g=new WowClient({'url':getFullnodeUrl(await Config['Instance']()['network_get'](a['network']))});e=await g['getTransactionBlock']({'digest':e?.['digest'],'options':{'showObjectChanges':!![]}});}return e;};export const get_guardSubmissions=async(a,b)=>{b['length']===0x0&&W_ERROR(WErrors['InvalidParam'],'guards\x20invalid:\x20guards\x20empty');b=[...new Set(b)];b['length']>MAX_GUARD_COUNT_ONCE&&W_ERROR(WErrors['InvalidParam'],'guards\x20invalid:\x20guard\x20count\x20exceeds\x20maximum\x20limit\x20\x20'+MAX_GUARD_COUNT_ONCE+'\x20once');const c=await query_objects({'objects':[...b],'no_cache':a['no_cache'],'network':a['network']});(!c['objects']||c['objects']['length']===0x0)&&W_ERROR(WErrors['InvalidParam'],'Guard\x20objects\x20not\x20found:\x20'+b['join'](',\x20'));const d=[],e=new Map();c['objects']['forEach'](m=>{m['type']!==ObjectType['Guard']&&W_ERROR(WErrors['InvalidParam'],'guards\x20invalid:\x20guard\x20\x27'+m['object']+'\x27\x20is\x20not\x20a\x20guard\x20object');const n=m;n['relies']?.['forEach'](p=>{!d['includes'](p)&&d['push'](p);});const o=n['rep_quote']instanceof Map?n['rep_quote']:new Map(Object['entries'](n['rep_quote']||{}));o['forEach']((p,q)=>{if(e['has'](q)){const s=e['get'](q)||[];p['forEach'](t=>{!s['includes'](t)&&s['push'](t);}),e['set'](q,s);}else e['set'](q,p);});});const f=await query_objects({'objects':[...e['keys']()],'no_cache':a['no_cache'],'network':a['network']}),g=[];f['objects']['forEach'](m=>{m['type']!==ObjectType['Repository']&&W_ERROR(WErrors['InvalidParam'],'rep\x20invalid:\x20rep\x20\x27'+m['object']+'\x27\x20is\x20not\x20a\x20guard\x20object');const n=m;n['policies']['forEach'](o=>{const q=e['get'](n['object'])||[];q['includes'](o['name'])&&o['quote_guard']&&!g['includes'](o['quote_guard'])&&g['push'](o['quote_guard']);});});const h=[...new Set([...g,...c['objects']['map'](m=>m['object'])])],i=new Set(h),j=[...d['filter'](m=>!i['has'](m))['map'](m=>({'object':m,'impack':![]})),...h['map'](m=>({'object':m,'impack':!![]}))];j['length']>MAX_GUARD_COUNT&&W_ERROR(WErrors['InvalidParam'],'guards\x20invalid:\x20guard\x20count(include\x20relies\x20guards)\x20exceeds\x20maximum\x20limit\x20'+MAX_GUARD_COUNT);const k=await query_objects({'objects':[...d,...g],'no_cache':a['no_cache'],'network':a['network']}),l=[];return k['objects']['forEach'](m=>{const n=m,o=[];n['table']?.['forEach'](p=>{p['b_submission']&&o['push'](p);}),o['length']>0x0&&l['find'](p=>p['guard']===n['object'])===undefined&&l['push']({'guard':n['object'],'submission':o});}),c['objects']['forEach'](m=>{const n=m,o=[];n['table']?.['forEach'](p=>{p['b_submission']&&o['push'](p);}),o['length']>0x0&&l['find'](p=>p['guard']===n['object'])===undefined&&l['push']({'guard':n['object'],'submission':o});}),{'type':'submission','guard':j,'submission':l};};export function passportFn(a){return PackageAddress+'::passport::'+a;}export const MAX_GUARD_COUNT=0x14;export function newPassport(a){return a['moveCall']({'target':passportFn('new'),'arguments':[a['object']['clock']()]});}export function freeze(a,b){a['moveCall']({'target':passportFn('freezen'),'arguments':[toTxObject(a,b)]});}export function destroy(a,b){a['moveCall']({'target':passportFn('destroy'),'arguments':[toTxObject(a,b)]});}export const guardAdd=async(a,b,c,d,e)=>{d['length']>MAX_GUARD_TABLE_ITEM_LENGTH&&W_ERROR(WErrors['GuardTableItemLengthExceeded'],'Submission\x20size\x20exceeds\x20maximum\x20limit\x20'+MAX_GUARD_TABLE_ITEM_LENGTH);const f=a['pure']['vector']('u8',d['map'](j=>j['identifier'])),g=[];for(let j=0x0;j<d['length'];j++){!d[j]['value']&&W_ERROR(WErrors['IsValidValue'],'Value\x20is\x20undefined\x20for\x20identifier\x20'+d[j]['identifier']);const k=parseValueType(d[j]['value_type']),l=await valueToBcsBytes(k,d[j]['value']);g['push'](Array['from'](l));}const h=a['pure']['vector']('vector<u8>',g);a['moveCall']({'target':passportFn('guard_add'),'arguments':[toTxObject(a,b),toTxObject(a,c),f,h,a['pure']['bool'](e)]});};export function guardClear(a,b){a['moveCall']({'target':passportFn('guard_clear'),'arguments':[toTxObject(a,b)]});}export function verify(a,b){return a['moveCall']({'target':passportFn('verify'),'arguments':[toTxObject(a,b)]});}