wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 5.85 kB
JavaScript
import{fromBase58,fromBase64,toBase58,toBase64,fromHex,toHex}from'../utils/index.js';import{BcsReader}from'./reader.js';import{ulebEncode}from'./uleb.js';import{BcsWriter}from'./writer.js';export class BcsType{['$inferType'];['$inferInput'];['name'];['read'];['serializedSize'];['validate'];#write;#serialize;constructor(a){this['name']=a['name'],this['read']=a['read'],this['serializedSize']=a['serializedSize']??(()=>null),this.#write=a['write'],this.#serialize=a['serialize']??((b,c)=>{const d=new BcsWriter({'initialSize':this['serializedSize'](b)??undefined,...c});return this.#write(b,d),d['toBytes']();}),this['validate']=a['validate']??(()=>{});}['write'](a,b){this['validate'](a),this.#write(a,b);}['serialize'](a,b){return this['validate'](a),new SerializedBcs(this,this.#serialize(a,b));}['parse'](a){const b=new BcsReader(a);return this['read'](b);}['fromHex'](a){return this['parse'](fromHex(a));}['fromBase58'](a){return this['parse'](fromBase58(a));}['fromBase64'](a){return this['parse'](fromBase64(a));}['transform']({name:a,input:b,output:c,validate:d}){return new BcsType({'name':a??this['name'],'read':e=>c?c(this['read'](e)):this['read'](e),'write':(e,f)=>this.#write(b?b(e):e,f),'serializedSize':e=>this['serializedSize'](b?b(e):e),'serialize':(e,f)=>this.#serialize(b?b(e):e,f),'validate':e=>{d?.(e),this['validate'](b?b(e):e);}});}}const SERIALIZED_BCS_BRAND=Symbol['for']('@mysten/serialized-bcs');export function isSerializedBcs(a){return!!a&&typeof a==='object'&&a[SERIALIZED_BCS_BRAND]===!![];}export class SerializedBcs{#schema;#bytes;get[SERIALIZED_BCS_BRAND](){return!![];}constructor(a,b){this.#schema=a,this.#bytes=b;}['toBytes'](){return this.#bytes;}['toHex'](){return toHex(this.#bytes);}['toBase64'](){return toBase64(this.#bytes);}['toBase58'](){return toBase58(this.#bytes);}['parse'](){return this.#schema['parse'](this.#bytes);}}export function fixedSizeBcsType({size:a,...b}){return new BcsType({...b,'serializedSize':()=>a});}export function uIntBcsType({readMethod:a,writeMethod:b,...c}){return fixedSizeBcsType({...c,'read':d=>d[a](),'write':(d,e)=>e[b](d),'validate':d=>{if(d<0x0||d>c['maxValue'])throw new TypeError('Invalid\x20'+c['name']+'\x20value:\x20'+d+'.\x20Expected\x20value\x20in\x20range\x200-'+c['maxValue']);c['validate']?.(d);}});}export function bigUIntBcsType({readMethod:a,writeMethod:b,...c}){return fixedSizeBcsType({...c,'read':d=>d[a](),'write':(d,e)=>e[b](BigInt(d)),'validate':d=>{const e=BigInt(d);if(e<0x0||e>c['maxValue'])throw new TypeError('Invalid\x20'+c['name']+'\x20value:\x20'+e+'.\x20Expected\x20value\x20in\x20range\x200-'+c['maxValue']);c['validate']?.(e);}});}export function dynamicSizeBcsType({serialize:a,...b}){const c=new BcsType({...b,'serialize':a,'write':(d,e)=>{for(const f of c['serialize'](d)['toBytes']()){e['write8'](f);}}});return c;}export function stringLikeBcsType({toBytes:a,fromBytes:b,...c}){return new BcsType({...c,'read':d=>{const e=d['readULEB'](),f=d['readBytes'](e);return b(f);},'write':(d,e)=>{const f=a(d);e['writeULEB'](f['length']);for(let g=0x0;g<f['length'];g++){e['write8'](f[g]);}},'serialize':d=>{const e=a(d),f=ulebEncode(e['length']),g=new Uint8Array(f['length']+e['length']);return g['set'](f,0x0),g['set'](e,f['length']),g;},'validate':d=>{if(typeof d!=='string')throw new TypeError('Invalid\x20'+c['name']+'\x20value:\x20'+d+'.\x20Expected\x20string');c['validate']?.(d);}});}export function lazyBcsType(a){let b=null;function c(){return!b&&(b=a()),b;}return new BcsType({'name':'lazy','read':d=>c()['read'](d),'serializedSize':d=>c()['serializedSize'](d),'write':(d,e)=>c()['write'](d,e),'serialize':(d,e)=>c()['serialize'](d,e)['toBytes']()});}export class BcsStruct extends BcsType{constructor({name:a,fields:b,...c}){const d=Object['entries'](b);super({'name':a,'serializedSize':e=>{let f=0x0;for(const [g,h]of d){const i=h['serializedSize'](e[g]);if(i==null)return null;f+=i;}return f;},'read':e=>{const f={};for(const [g,h]of d){f[g]=h['read'](e);}return f;},'write':(e,f)=>{for(const [g,h]of d){h['write'](e[g],f);}},...c,'validate':e=>{c?.['validate']?.(e);if(typeof e!=='object'||e==null)throw new TypeError('Expected\x20object,\x20found\x20'+typeof e);}});}}export class BcsEnum extends BcsType{constructor({fields:a,...b}){const c=Object['entries'](a);super({'read':d=>{const e=d['readULEB'](),f=c[e];if(!f)throw new TypeError('Unknown\x20value\x20'+e+'\x20for\x20enum\x20'+b['name']);const [g,h]=f;return{[g]:h?.['read'](d)??!![],'$kind':g};},'write':(d,e)=>{const [f,g]=Object['entries'](d)['filter'](([h])=>Object['hasOwn'](a,h))[0x0];for(let h=0x0;h<c['length'];h++){const [j,k]=c[h];if(j===f){e['writeULEB'](h),k?.['write'](g,e);return;}}},...b,'validate':d=>{b?.['validate']?.(d);if(typeof d!=='object'||d==null)throw new TypeError('Expected\x20object,\x20found\x20'+typeof d);const e=Object['keys'](d)['filter'](g=>d[g]!==undefined&&Object['hasOwn'](a,g));if(e['length']!==0x1)throw new TypeError('Expected\x20object\x20with\x20one\x20key,\x20but\x20found\x20'+e['length']+'\x20for\x20type\x20'+b['name']+'}');const [f]=e;if(!Object['hasOwn'](a,f))throw new TypeError('Invalid\x20enum\x20variant\x20'+f);}});}}export class BcsTuple extends BcsType{constructor({fields:a,name:b,...c}){super({'name':b??'('+a['map'](d=>d['name'])['join'](',\x20')+')','serializedSize':d=>{let e=0x0;for(let f=0x0;f<a['length'];f++){const g=a[f]['serializedSize'](d[f]);if(g==null)return null;e+=g;}return e;},'read':d=>{const e=[];for(const f of a){e['push'](f['read'](d));}return e;},'write':(d,e)=>{for(let f=0x0;f<a['length'];f++){a[f]['write'](d[f],e);}},...c,'validate':d=>{c?.['validate']?.(d);if(!Array['isArray'](d))throw new TypeError('Expected\x20array,\x20found\x20'+typeof d);if(d['length']!==a['length'])throw new TypeError('Expected\x20array\x20of\x20length\x20'+a['length']+',\x20found\x20'+d['length']);}});}}