wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 24 kB
JavaScript
import{sha256}from'@noble/hashes/sha256';import{bytesToHex}from'@noble/hashes/utils';import*as a179a from'fs';import*as a179b from'path';import*as a179c from'https';import*as a179d from'http';const WIP_SCHEMA_URL='https://github.com/wowok-ai/docs/blob/main/WIP.md',WIP_VERSION='1.0.0';export const DEFAULT_LIMITS={'maxImageSize':0x2*0x400*0x400,'maxTotalSize':0xa*0x400*0x400,'maxImageCount':0xa,'maxTextLength':0x2710};export class WipError extends Error{constructor(a){super(a),this['name']='WipError';}}export class WipValidationError extends WipError{constructor(a){super(a),this['name']='WipValidationError';}}export class WipSizeError extends WipError{constructor(a){super(a),this['name']='WipSizeError';}}function detectMimeType(a){if(a['length']>=0x4&&a[0x0]===0x89&&a[0x1]===0x50&&a[0x2]===0x4e&&a[0x3]===0x47)return'image/png';if(a['length']>=0x3&&a[0x0]===0xff&&a[0x1]===0xd8&&a[0x2]===0xff)return'image/jpeg';if(a['length']>=0x6&&a[0x0]===0x47&&a[0x1]===0x49&&a[0x2]===0x46)return'image/gif';if(a['length']>=0xc&&a[0x0]===0x52&&a[0x1]===0x49&&a[0x2]===0x46&&a[0x3]===0x46&&a[0x8]===0x57&&a[0x9]===0x45&&a[0xa]===0x42&&a[0xb]===0x50)return'image/webp';return null;}function uint8ArrayToBase64(a){let b='';const c=a['length'];for(let d=0x0;d<c;d++){b+=String['fromCharCode'](a[d]);}return btoa(b);}function base64ToUint8Array(a){const b=atob(a),c=b['length'],d=new Uint8Array(c);for(let e=0x0;e<c;e++){d[e]=b['charCodeAt'](e);}return d;}function canonicalizeJson(a){if(a===null||typeof a!=='object')return JSON['stringify'](a);if(Array['isArray'](a))return'['+a['map'](canonicalizeJson)['join'](',')+']';const b=a,c=Object['keys'](b)['sort'](),d=c['map'](e=>'\x22'+e+'\x22:'+canonicalizeJson(b[e]));return'{'+d['join'](',')+'}';}function computeHash(a){const b=sha256(new TextEncoder()['encode'](a));return bytesToHex(b);}function generateId(a){const b=Date['now']()['toString'](0x24),c=Math['random']()['toString'](0x24)['substring'](0x2,0x8);return a+'-'+b+'-'+c;}function getExtensionFromMimeType(a){const b={'image/png':'png','image/jpeg':'jpg','image/gif':'gif','image/webp':'webp'};return b[a]||'bin';}class ResourceLoader{['maxSize'];constructor(a){this['maxSize']=a;}async['load'](a){if(!a||a['trim']()==='')throw new WipError('WIP\x20file\x20path\x20cannot\x20be\x20empty');if(a['startsWith']('data:'))return this['loadFromDataUrl'](a);if(a['startsWith']('http://')||a['startsWith']('https://'))return this['loadFromNetwork'](a);return this['loadFromFile'](a);}['loadFromDataUrl'](a){const b=a['match'](/^data:image\/(\w+);base64,(.+)$/);if(!b)throw new WipValidationError('Invalid\x20data\x20URL\x20format:\x20'+a['substring'](0x0,0x32)+'...');const c=b[0x2];try{return base64ToUint8Array(c);}catch(d){throw new WipError('Failed\x20to\x20decode\x20data\x20URL:\x20'+d);}}async['loadFromNetwork'](a){return new Promise((b,c)=>{const d=setTimeout(()=>{c(new WipError('Network\x20timeout\x20while\x20fetching:\x20'+a));},0x7530),e=a['startsWith']('https:'),f=e?a179c:a179d,g={'headers':{'Accept':'application/json,*/*'}};e&&(g['rejectUnauthorized']=![]);const h=f['get'](a,g,i=>{clearTimeout(d);if(i['statusCode']!==0xc8){c(new WipError('Failed\x20to\x20fetch:\x20'+i['statusCode']+'\x20'+i['statusMessage']));return;}const j=i['headers']['content-length'];if(j&&parseInt(j)>this['maxSize']){c(new WipSizeError('Resource\x20from\x20'+a+'\x20size\x20('+j+'\x20bytes)\x20exceeds\x20limit\x20('+this['maxSize']+'\x20bytes)'));return;}const k=[];i['on']('data',l=>{k['push'](l);}),i['on']('end',()=>{const l=Buffer['concat'](k);if(l['length']>this['maxSize']){c(new WipSizeError('Resource\x20from\x20'+a+'\x20size\x20('+l['length']+'\x20bytes)\x20exceeds\x20limit\x20('+this['maxSize']+'\x20bytes)'));return;}b(new Uint8Array(l));}),i['on']('error',l=>{c(new WipError('Network\x20error:\x20'+l['message']));});});h['on']('error',i=>{clearTimeout(d),c(new WipError('Network\x20error:\x20'+i['message']));});});}['loadFromFile'](a){const b=a179b['resolve'](a);if(!a179a['existsSync'](b))throw new WipError('File\x20not\x20found:\x20'+b);const c=a179a['statSync'](b);if(!c['isFile']())throw new WipError('Path\x20is\x20not\x20a\x20file:\x20'+b);if(c['size']>this['maxSize'])throw new WipSizeError('File\x20'+b+'\x20size\x20('+c['size']+'\x20bytes)\x20exceeds\x20limit\x20('+this['maxSize']+'\x20bytes)');const d=a179a['readFileSync'](b);return new Uint8Array(d);}}export class WIP{['limits'];['resourceLoader'];constructor(a={}){this['limits']={...DEFAULT_LIMITS,...a},this['resourceLoader']=new ResourceLoader(this['limits']['maxImageSize']);}async['create'](a){if(a['text']['length']>this['limits']['maxTextLength'])throw new WipSizeError('Text\x20length\x20('+a['text']['length']+')\x20exceeds\x20limit\x20('+this['limits']['maxTextLength']+')');const b=[];if(a['images']&&a['images']['length']>0x0){if(a['images']['length']>this['limits']['maxImageCount'])throw new WipSizeError('Image\x20count\x20('+a['images']['length']+')\x20exceeds\x20limit\x20('+this['limits']['maxImageCount']+')');for(let j=0x0;j<a['images']['length'];j++){const k=await this['processImage'](a['images'][j],j);b['push'](k);}}const c={'content':{'text':a['text'],'format':a['textFormat']||'plain'},'media':b},d={'wip':WIP_SCHEMA_URL,'payload':c},e=computeHash(canonicalizeJson(d)),f={'type':'wip','version':WIP_VERSION,'created':new Date()['toISOString'](),'hash':e,'algorithm':'sha256'};if(a['signKeyPair']){const {signature:l,...m}=f,n=computeHash(canonicalizeJson(m)),o=await this['sign'](n,a['signKeyPair']['privateKey'],a['signAlgorithm']||'Falcon512');f['signature']={'value':o,'publicKey':a['signKeyPair']['publicKey'],'algorithm':a['signAlgorithm']||'Falcon512'};}const g={'wip':WIP_SCHEMA_URL,'payload':c,'meta':f},h=JSON['stringify'](g);if(h['length']>this['limits']['maxTotalSize'])throw new WipSizeError('Total\x20WIP\x20file\x20size\x20('+h['length']+'\x20bytes)\x20exceeds\x20limit\x20('+this['limits']['maxTotalSize']+'\x20bytes)');return g;}async['processImage'](a,b){const c=await this['resourceLoader']['load'](a['source']),d=detectMimeType(c);if(!d)throw new WipValidationError('Unsupported\x20image\x20format:\x20'+a['source']);let e=a['filename'];if(!e){if(a['source']['startsWith']('http')){const f=new URL(a['source']),g=f['pathname'];e=g['substring'](g['lastIndexOf']('/')+0x1)||'image-'+b;}else e=a['source']['substring'](a['source']['lastIndexOf']('/')+0x1)||'image-'+b;}return{'id':a['id']||generateId('img'),'type':d,'data':uint8ArrayToBase64(c),'filename':e};}async['sign'](a,b,c){if(c!=='Falcon512')throw new WipError('Unsupported\x20signature\x20algorithm:\x20'+c);const {Falcon512Keypair:d}=await import('../../keypairs/falcon512/index.js'),e=d['fromSecretKey'](b),f=await e['sign'](new TextEncoder()['encode'](a));return uint8ArrayToBase64(f);}async['verify'](a){try{if(!a['wip']||!a['payload']||!a['meta'])return{'valid':![],'error':'Invalid\x20WIP\x20file\x20structure'};if(a['meta']['type']!=='wip')return{'valid':![],'error':'Invalid\x20meta.type:\x20'+a['meta']['type']};const b={'wip':a['wip'],'payload':a['payload']},c=computeHash(canonicalizeJson(b));if(c!==a['meta']['hash'])return{'valid':![],'error':'Hash\x20mismatch\x20-\x20content\x20has\x20been\x20tampered'};const d=[];if(a['meta']['signature']){const {signature:e,...f}=a['meta'],g=computeHash(canonicalizeJson(f)),h=Array['isArray'](a['meta']['signature'])?a['meta']['signature']:[a['meta']['signature']];for(const i of h){const j=await this['verifySignature'](g,i['value'],i['publicKey'],i['algorithm']);d['push']({'address':i['address'],'valid':j});if(!j)return{'valid':![],'error':'Invalid\x20signature','signatures':d};}}return{'valid':!![],'signatures':d};}catch(k){return{'valid':![],'error':'Verification\x20error:\x20'+k};}}async['verifySignature'](a,b,c,d){if(d!=='Falcon512')throw new WipError('Unsupported\x20signature\x20algorithm:\x20'+d);try{const {Falcon512PublicKey:e}=await import('../../keypairs/falcon512/publickey.js');let f;if(c['startsWith']('0x')){const i=Buffer['from'](c['slice'](0x2),'hex');if(i['length']===0x382)f=i['slice'](0x1);else i['length']===0x381?f=i:f=i;}else try{const j=Buffer['from'](c,'base64');j['length']===0x382?f=j['slice'](0x1):f=j;}catch{f=Buffer['from'](c,'base64');}const g=new e(f),h=Uint8Array['from'](Buffer['from'](b,'base64'));return await g['verify'](new TextEncoder()['encode'](a),h);}catch(k){return console['error']('Signature\x20verification\x20error:',k),![];}}async['toHtml'](a,b={}){const {title:title='WIP\x20Document',theme:theme='light'}=b,{payload:c,meta:d}=a,e=o=>{return o['replace'](/&/g,'&')['replace'](/</g,'<')['replace'](/>/g,'>')['replace'](/"/g,'"')['replace'](/'/g,''');},f=(o,p)=>{switch(p){case'html':return o;case'markdown':return o['replace'](/\*\*(.+?)\*\*/g,'<strong>$1</strong>')['replace'](/\*(.+?)\*/g,'<em>$1</em>')['replace'](/\n/g,'<br>');case'plain':default:return e(o)['replace'](/\n/g,'<br>');}},g=theme==='dark'?'#1a1a1a':'#ffffff',h=theme==='dark'?'#e0e0e0':'#333333',i=theme==='dark'?'#444444':'#e0e0e0',j='#4caf50',k='#ff9800',l=await this['verify'](a),m=l['valid']?'<span\x20style=\x22background:\x20'+j+';\x20color:\x20white;\x20padding:\x204px\x208px;\x20border-radius:\x204px;\x20font-size:\x2012px;\x22>✓\x20Verified</span>':'<span\x20style=\x22background:\x20'+k+';\x20color:\x20white;\x20padding:\x204px\x208px;\x20border-radius:\x204px;\x20font-size:\x2012px;\x22>⚠\x20Tampered</span>',n=c['media']['map'](o=>'\x0a\x09\x09\x09\x09<div\x20style=\x22margin:\x2016px\x200;\x20border:\x201px\x20solid\x20'+i+';\x20border-radius:\x208px;\x20overflow:\x20hidden;\x22>\x0a\x09\x09\x09\x09\x09<img\x20src=\x22data:'+o['type']+';base64,'+o['data']+'\x22\x20\x0a\x09\x09\x09\x09\x09\x20\x20\x20\x20\x20style=\x22max-width:\x20100%;\x20height:\x20auto;\x20display:\x20block;\x22\x20\x0a\x09\x09\x09\x09\x09\x20\x20\x20\x20\x20alt=\x22'+e(o['filename']||o['id'])+'\x22\x20/>\x0a\x09\x09\x09\x09\x09'+(o['filename']?'<div\x20style=\x22padding:\x208px;\x20font-size:\x2012px;\x20color:\x20#666;\x20background:\x20'+(theme==='dark'?'#2a2a2a':'#f5f5f5')+';\x20border-top:\x201px\x20solid\x20'+i+';\x22>'+e(o['filename'])+'</div>':'')+'\x0a\x09\x09\x09\x09</div>\x0a\x09\x09\x09')['join']('');return'<!DOCTYPE\x20html>\x0a<html\x20lang=\x22en\x22>\x0a<head>\x0a\x20\x20\x20\x20<meta\x20charset=\x22UTF-8\x22>\x0a\x20\x20\x20\x20<meta\x20name=\x22viewport\x22\x20content=\x22width=device-width,\x20initial-scale=1.0\x22>\x0a\x20\x20\x20\x20<title>'+e(title)+'</title>\x0a</head>\x0a<body\x20style=\x22margin:\x200;\x20padding:\x2020px;\x20font-family:\x20-apple-system,\x20BlinkMacSystemFont,\x20\x27Segoe\x20UI\x27,\x20Roboto,\x20sans-serif;\x20background:\x20'+g+';\x20color:\x20'+h+';\x20line-height:\x201.6;\x22>\x0a\x20\x20\x20\x20<div\x20style=\x22max-width:\x20800px;\x20margin:\x200\x20auto;\x20background:\x20'+(theme==='dark'?'#252525':'#ffffff')+';\x20border-radius:\x2012px;\x20box-shadow:\x200\x202px\x208px\x20rgba(0,0,0,0.1);\x20overflow:\x20hidden;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22padding:\x2020px\x2024px;\x20border-bottom:\x201px\x20solid\x20'+i+';\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22display:\x20flex;\x20justify-content:\x20space-between;\x20align-items:\x20center;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<h1\x20style=\x22margin:\x200;\x20font-size:\x2020px;\x20font-weight:\x20600;\x22>'+e(title)+'</h1>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22font-size:\x2012px;\x20color:\x20#666;\x20margin-top:\x204px;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20WIP\x20v'+d['version']+'\x20•\x20'+new Date(d['created'])['toLocaleString']()+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20'+m+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22font-size:\x2011px;\x20color:\x20#999;\x20margin-top:\x2012px;\x20padding:\x208px;\x20background:\x20'+(theme==='dark'?'#2a2a2a':'#f5f5f5')+';\x20border-radius:\x204px;\x20font-style:\x20italic;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20Important\x20Note:\x20This\x20page\x20is\x20for\x20document\x20browsing\x20and\x20verification\x20reference\x20only.\x20Please\x20verify\x20the\x20document\x20integrity\x20through\x20the\x20API\x20using\x20the\x20WIP\x20file.\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20'+(l['signatures']&&l['signatures']['length']>0x0?'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22margin-top:\x2012px;\x20padding:\x208px;\x20background:\x20'+(theme==='dark'?'#2a3f2a':'#e8f5e9')+';\x20border-radius:\x204px;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22font-size:\x2011px;\x20color:\x20'+(theme==='dark'?'#81c784':'#2e7d32')+';\x20margin-bottom:\x204px;\x22>✓\x20Signature\x20Verification\x20('+l['signatures']['length']+'\x20signature'+(l['signatures']['length']>0x1?'s':'')+')</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20'+l['signatures']['map']((o,p)=>'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22font-size:\x2010px;\x20color:\x20'+h+';\x20margin-left:\x208px;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20'+(p+0x1)+'.\x20'+(o['address']?e(o['address']['slice'](0x0,0x14))+'...':'Unknown')+':\x20'+(o['valid']?'<span\x20style=\x22color:\x20#4caf50;\x22>✓\x20Valid</span>':'<span\x20style=\x22color:\x20#f44336;\x22>✗\x20Invalid</span>')+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20')['join']('')+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20':'')+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22padding:\x2024px;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22font-size:\x2016px;\x20line-height:\x201.8;\x20margin-bottom:\x2024px;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20'+f(c['content']['text'],c['content']['format'])+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20'+n+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22padding:\x2016px\x2024px;\x20background:\x20'+(theme==='dark'?'#2a2a2a':'#f8f9fa')+';\x20border-top:\x201px\x20solid\x20'+i+';\x20font-size:\x2011px;\x20color:\x20#666;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div\x20style=\x22display:\x20flex;\x20justify-content:\x20space-between;\x20align-items:\x20center;\x20flex-wrap:\x20wrap;\x20gap:\x208px;\x22>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<strong>Hash:</strong>\x20<code\x20style=\x22background:\x20'+(theme==='dark'?'#333':'#e9ecef')+';\x20padding:\x202px\x206px;\x20border-radius:\x203px;\x22>'+e(d['hash'])+'</code>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20<div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20'+(l['valid']?'Content\x20integrity\x20verified':'Verification\x20failed:\x20'+e(l['error']||'Unknown\x20error'))+'\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20\x20\x20\x20\x20</div>\x0a\x20\x20\x20\x20</div>\x0a</body>\x0a</html>';}['stringify'](a,b=!![]){return b?JSON['stringify'](a,null,0x2):JSON['stringify'](a);}['parse'](a){try{const b=JSON['parse'](a);if(!b['wip']||!b['payload']||!b['meta'])throw new WipValidationError('Invalid\x20WIP\x20file\x20structure');return b;}catch(c){if(c instanceof WipError)throw c;throw new WipValidationError('Failed\x20to\x20parse\x20WIP\x20file:\x20'+c);}}async['load'](a){const b=await this['resourceLoader']['load'](a),c=new TextDecoder()['decode'](b);return this['parse'](c);}['getHash'](a){const b=a['meta']['hash'];if(b['includes'](':'))return b['split'](':')[0x1];return b;}['extractImages'](a,b){!a179a['existsSync'](b)&&a179a['mkdirSync'](b,{'recursive':!![]});const c=[];for(const d of a['payload']['media']){const e=getExtensionFromMimeType(d['type']),f=d['filename']||d['id']+'.'+e,g=a179b['join'](b,f),h=Buffer['from'](base64ToUint8Array(d['data']));a179a['writeFileSync'](g,h),c['push'](g);}return c;}['save'](a,b,c=!![]){const d=a179b['dirname'](b);!a179a['existsSync'](d)&&a179a['mkdirSync'](d,{'recursive':!![]}),a179a['writeFileSync'](b,this['stringify'](a,c),'utf-8');}['getLimits'](){return{...this['limits']};}['setLimits'](a){this['limits']={...this['limits'],...a},this['resourceLoader']=new ResourceLoader(this['limits']['maxImageSize']);}}export async function createWip(a,b){const c=new WIP(b);return c['create'](a);}export async function verifyWip(a,b){if(b!==undefined){const e=a['meta']['hash']['toLowerCase']()['replace'](/^sha256:/,''),f=b['toLowerCase']()['replace'](/^sha256:/,'');if(e!==f)return{'valid':![],'error':'Hash\x20mismatch'};}const c=new WIP(),d=await c['verify'](a);return{'valid':d['valid'],'error':d['error']};}export async function wipToHtml(a,b){const c=new WIP();return c['toHtml'](a,b);}export async function loadWip(a,b){const c=new WIP(b);return c['load'](a);}export async function generate_wip(a,b){const c=new WIP(DEFAULT_LIMITS);let d=await c['create']({'text':a['markdown_text'],'textFormat':'markdown','images':a['images']});if(a['account']){const {Account:e}=await import('./account.js'),f=await e['Instance']()['get_address'](a['account']);if(!f)throw new WipValidationError('Account\x20not\x20found:\x20'+a['account']);const {signature:g,...h}=d['meta'],i=computeHash(canonicalizeJson(h)),j=await e['Instance']()['sign'](f,i),k=await e['Instance']()['get'](f);if(!k||!k['pubkey'])throw new WipValidationError('Cannot\x20get\x20public\x20key\x20for\x20account:\x20'+f);let l;if(k['pubkey']['startsWith']('0x')){const p=Buffer['from'](k['pubkey']['slice'](0x2),'hex');if(p['length']===0x382)l=p['slice'](0x1)['toString']('base64');else{if(p['length']===0x381)l=p['toString']('base64');else throw new WipValidationError('Invalid\x20public\x20key\x20length:\x20'+p['length']+',\x20expected\x20897\x20or\x20898\x20bytes');}}else l=k['pubkey'];const m=Buffer['from'](j['slice'](0x2),'hex')['toString']('base64'),n={'value':m,'publicKey':l,'algorithm':'Falcon512','address':f};let o;d['meta']['signature']?o=Array['isArray'](d['meta']['signature'])?[...d['meta']['signature'],n]:[d['meta']['signature'],n]:o=[n],d={...d,'meta':{...d['meta'],'signature':o}};}return c['save'](d,b,!![]),b;}export async function verify_wip(a,b,c){const d=new WIP();let e;try{e=await d['load'](a);}catch(k){return{'valid':![],'error':'Failed\x20to\x20load\x20WIP\x20file:\x20'+k,'hashValid':![],'hasSignature':![]};}if(!e['wip']||!e['payload']||!e['meta'])return{'valid':![],'error':'Invalid\x20WIP\x20file\x20structure','hashValid':![],'hasSignature':![]};if(e['meta']['type']!=='wip')return{'valid':![],'error':'Invalid\x20meta.type:\x20'+e['meta']['type'],'hashValid':![],'hasSignature':!!e['meta']['signature']};if(b!==undefined){const l=e['meta']['hash']['toLowerCase']()['replace'](/^sha256:/,''),m=b['toLowerCase']()['replace'](/^sha256:/,'');if(l!==m)return{'valid':![],'error':'Hash\x20mismatch','hashValid':![],'hasSignature':!!e['meta']['signature']};}if(c&&!e['meta']['signature'])return{'valid':![],'error':'Signature\x20required\x20but\x20WIP\x20file\x20has\x20no\x20signature','hashValid':!![],'hasSignature':![]};const f={'wip':e['wip'],'payload':e['payload']},g=computeHash(canonicalizeJson(f)),h=g===e['meta']['hash'];if(!h)return{'valid':![],'error':'Hash\x20mismatch\x20-\x20content\x20has\x20been\x20tampered','hashValid':![],'hasSignature':!!e['meta']['signature']};let i;const j=[];if(e['meta']['signature']){const {signature:n,...o}=e['meta'],p=computeHash(canonicalizeJson(o)),q=Array['isArray'](e['meta']['signature'])?e['meta']['signature']:[e['meta']['signature']];let r=!![];for(const s of q){const t=await d['verifySignature'](p,s['value'],s['publicKey'],s['algorithm']);let u;try{const {Falcon512PublicKey:v}=await import('../../keypairs/falcon512/publickey.js'),w=new v(s['publicKey']);u=w['toWAddress']();}catch{}j['push']({'publicKey':s['publicKey'],'address':u,'valid':t}),!t&&(r=![]);}i=r;if(!i){const x=j['filter'](y=>!y['valid']&&y['address'])['map'](y=>y['address']);return{'valid':![],'error':'Signatures\x20from\x20the\x20following\x20addresses\x20are\x20invalid:\x20'+x['join'](',\x20'),'hashValid':!![],'signatureValid':![],'hasSignature':!![],'signatures':j};}}return{'valid':!![],'hashValid':!![],'signatureValid':i,'hasSignature':!!e['meta']['signature'],'signatures':j['length']>0x0?j:undefined};}export async function sign_wip(a,b,c){const d=new WIP(),e=await d['load'](a);if(!e['wip']||!e['payload']||!e['meta'])throw new WipValidationError('Invalid\x20WIP\x20file\x20structure');if(e['meta']['type']!=='wip')throw new WipValidationError('Invalid\x20meta.type:\x20'+e['meta']['type']);const {Account:f}=await import('./account.js'),g=await f['Instance']()['get_address'](b);if(!g)throw new WipValidationError('Account\x20not\x20found:\x20'+b);const {signature:h,...i}=e['meta'],j=computeHash(canonicalizeJson(i)),k=await f['Instance']()['signData'](g,j);let l;const m=k['publicKey'];if(m['startsWith']('0x')){const r=Buffer['from'](m['slice'](0x2),'hex');if(r['length']===0x382)l=r['slice'](0x1)['toString']('base64');else{if(r['length']===0x381)l=r['toString']('base64');else throw new WipValidationError('Invalid\x20public\x20key\x20length:\x20'+r['length']+',\x20expected\x20897\x20or\x20898\x20bytes');}}else l=m;const n={'value':Buffer['from'](k['signature']['slice'](0x2),'hex')['toString']('base64'),'publicKey':l,'algorithm':'Falcon512','address':k['address']};let o;if(e['meta']['signature']){const s=Array['isArray'](e['meta']['signature'])?[...e['meta']['signature']]:[e['meta']['signature']],t=s['findIndex'](u=>u['publicKey']===l);t>=0x0?(s[t]=n,o=s):o=[...s,n];}else o=[n];const p={...e,'meta':{...e['meta'],'signature':o}};let q;if(c)q=c;else{const u=a179b['dirname'](a),v=a179b['extname'](a),w=a179b['basename'](a,v);q=a179b['join'](u,'signed_'+w+v);}return d['save'](p,q,!![]),q;}export async function wip2html(a,b={}){const c=await import('fs'),d=await import('path'),e=c['statSync'](a);if(e['isFile']()){const f=new WIP(),g=await f['load'](a),h=await f['toHtml'](g,b);if(b['outputPath'])return c['writeFileSync'](b['outputPath'],h),b['outputPath'];const i=d['dirname'](a),j=d['extname'](a),k=d['basename'](a,j),l=d['join'](i,k+'.html');return c['writeFileSync'](l,h),l;}else{if(e['isDirectory']()){const m=c['readdirSync'](a),n=m['filter'](q=>q['endsWith']('.wip'));if(n['length']===0x0)throw new WipValidationError('No\x20.wip\x20files\x20found\x20in\x20directory:\x20'+a);const o=[],p=new WIP();for(const q of n){const r=d['join'](a,q),s=await p['load'](r),t=await p['toHtml'](s,b),u=q['replace']('.wip','.html'),v=b['outputPath']?d['join'](b['outputPath'],u):d['join'](a,u),w=d['dirname'](v);!c['existsSync'](w)&&c['mkdirSync'](w,{'recursive':!![]}),c['writeFileSync'](v,t),o['push'](v);}return o;}else throw new WipValidationError('Invalid\x20path:\x20'+a);}}export default WIP;