wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 4.3 kB
JavaScript
import{BcsEnum,BcsStruct,BcsTuple,BcsType,bigUIntBcsType,dynamicSizeBcsType,fixedSizeBcsType,lazyBcsType,stringLikeBcsType,uIntBcsType}from'./bcs-type.js';import{ulebEncode}from'./uleb.js';function fixedArray(a,b,c){return new BcsType({'read':d=>{const e=new Array(a);for(let f=0x0;f<a;f++){e[f]=b['read'](d);}return e;},'write':(d,e)=>{for(const f of d){b['write'](f,e);}},...c,'name':c?.['name']??b['name']+'['+a+']','validate':d=>{c?.['validate']?.(d);if(!d||typeof d!=='object'||!('length'in d))throw new TypeError('Expected\x20array,\x20found\x20'+typeof d);if(d['length']!==a)throw new TypeError('Expected\x20array\x20of\x20length\x20'+a+',\x20found\x20'+d['length']);}});}function option(a){return bcs['enum']('Option<'+a['name']+'>',{'None':null,'Some':a})['transform']({'input':b=>{if(b==null)return{'None':!![]};return{'Some':b};},'output':b=>{if(b['$kind']==='Some')return b['Some'];return null;}});}function vector(a,b){return new BcsType({'read':c=>{const d=c['readULEB'](),e=new Array(d);for(let f=0x0;f<d;f++){e[f]=a['read'](c);}return e;},'write':(c,d)=>{d['writeULEB'](c['length']);for(const e of c){a['write'](e,d);}},...b,'name':b?.['name']??'vector<'+a['name']+'>','validate':c=>{b?.['validate']?.(c);if(!c||typeof c!=='object'||!('length'in c))throw new TypeError('Expected\x20array,\x20found\x20'+typeof c);}});}function map(a,b){return bcs['vector'](bcs['tuple']([a,b]))['transform']({'name':'Map<'+a['name']+',\x20'+b['name']+'>','input':c=>{return[...c['entries']()];},'output':c=>{const d=new Map();for(const [e,f]of c){d['set'](e,f);}return d;}});}export const bcs={'u8'(a){return uIntBcsType({'readMethod':'read8','writeMethod':'write8','size':0x1,'maxValue':0x2**0x8-0x1,...a,'name':a?.['name']??'u8'});},'u16'(a){return uIntBcsType({'readMethod':'read16','writeMethod':'write16','size':0x2,'maxValue':0x2**0x10-0x1,...a,'name':a?.['name']??'u16'});},'u32'(a){return uIntBcsType({'readMethod':'read32','writeMethod':'write32','size':0x4,'maxValue':0x2**0x20-0x1,...a,'name':a?.['name']??'u32'});},'u64'(a){return bigUIntBcsType({'readMethod':'read64','writeMethod':'write64','size':0x8,'maxValue':0x2n**0x40n-0x1n,...a,'name':a?.['name']??'u64'});},'u128'(a){return bigUIntBcsType({'readMethod':'read128','writeMethod':'write128','size':0x10,'maxValue':0x2n**0x80n-0x1n,...a,'name':a?.['name']??'u128'});},'u256'(a){return bigUIntBcsType({'readMethod':'read256','writeMethod':'write256','size':0x20,'maxValue':0x2n**0x100n-0x1n,...a,'name':a?.['name']??'u256'});},'bool'(a){return fixedSizeBcsType({'size':0x1,'read':b=>b['read8']()===0x1,'write':(b,c)=>c['write8'](b?0x1:0x0),...a,'name':a?.['name']??'bool','validate':b=>{a?.['validate']?.(b);if(typeof b!=='boolean')throw new TypeError('Expected\x20boolean,\x20found\x20'+typeof b);}});},'uleb128'(a){return dynamicSizeBcsType({'read':b=>b['readULEB'](),'serialize':b=>{return Uint8Array['from'](ulebEncode(b));},...a,'name':a?.['name']??'uleb128'});},'bytes'(a,b){return fixedSizeBcsType({'size':a,'read':c=>c['readBytes'](a),'write':(c,d)=>{d['writeBytes'](new Uint8Array(c));},...b,'name':b?.['name']??'bytes['+a+']','validate':c=>{b?.['validate']?.(c);if(!c||typeof c!=='object'||!('length'in c))throw new TypeError('Expected\x20array,\x20found\x20'+typeof c);if(c['length']!==a)throw new TypeError('Expected\x20array\x20of\x20length\x20'+a+',\x20found\x20'+c['length']);}});},'byteVector'(a){return new BcsType({'read':b=>{const c=b['readULEB']();return b['readBytes'](c);},'write':(b,c)=>{const d=new Uint8Array(b);c['writeULEB'](d['length']),c['writeBytes'](d);},...a,'name':a?.['name']??'vector<u8>','serializedSize':b=>{const c='length'in b?b['length']:null;return c==null?null:ulebEncode(c)['length']+c;},'validate':b=>{a?.['validate']?.(b);if(!b||typeof b!=='object'||!('length'in b))throw new TypeError('Expected\x20array,\x20found\x20'+typeof b);}});},'string'(a){return stringLikeBcsType({'toBytes':b=>new TextEncoder()['encode'](b),'fromBytes':b=>new TextDecoder()['decode'](b),...a,'name':a?.['name']??'string'});},'fixedArray':fixedArray,'option':option,'vector':vector,'tuple'(a,b){return new BcsTuple({'fields':a,...b});},'struct'(a,b,c){return new BcsStruct({'name':a,'fields':b,...c});},'enum'(a,b,c){return new BcsEnum({'name':a,'fields':b,...c});},'map':map,'lazy'(a){return lazyBcsType(a);}};