@make-software/ces-js-parser
Version:
Parser for contract-level events encoded with Casper Event Standard (CES)
1 lines • 10.6 kB
JavaScript
(()=>{"use strict";var e={85:e=>{e.exports=require("casper-js-sdk")},105:function(e,t,r){var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(a,o){function s(e){try{c(n.next(e))}catch(e){o(e)}}function i(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,i)}c((n=n.apply(e,t||[])).next())}))},a=this&&this.__generator||function(e,t){var r,n,a,o,s={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function i(i){return function(c){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,i[0]&&(s=0)),s;)try{if(r=1,n&&(a=2&i[0]?n.return:i[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,i[1])).done)return a;switch(n=0,a&&(i=[2&i[0],a.value]),i[0]){case 0:case 1:a=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!((a=(a=s.trys).length>0&&a[a.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!a||i[1]>a[0]&&i[1]<a[3])){s.label=i[1];break}if(6===i[0]&&s.label<a[1]){s.label=a[1],a=i;break}if(a&&s.label<a[2]){s.label=a[2],s.ops.push(i);break}a[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=a=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.fetchContractSchemasBytes=t.parseSchemaFromBytesWithRemainder=t.parseSchemasFromBytes=void 0;var o=r(85),s=r(337);t.parseSchemasFromBytes=function(e){for(var t=new o.CLTypeMap(o.CLTypeString,new o.CLTypeList(new o.CLTypeTuple2(o.CLTypeString,new o.CLTypeDynamic(o.TypeID.String,o.CLTypeString)))),r={},n=o.CLValueParser.fromBytesByType(e,t).result.map.getMap(),a=0,s=Object.entries(n);a<s.length;a++){var i=s[a],c=i[0],u=i[1];r[c]=u.list.elements.map((function(e){return{property:e.tuple2.inner1.stringVal.toString(),value:e.tuple2.inner2.type.inner}}))}return r},t.parseSchemaFromBytesWithRemainder=function(e){var t=new o.CLTypeList(new o.CLTypeTuple2(o.CLTypeString,new o.CLTypeDynamic(o.TypeID.String,o.CLTypeString))),r=o.CLValueParser.fromBytesByType(e,t);return{result:r.result.list.elements.map((function(e){return{property:e.tuple2.inner1.stringVal.toString(),value:e.tuple2.inner2.type.inner}})),bytes:r.bytes}},t.fetchContractSchemasBytes=function(e,t,r){return n(this,void 0,void 0,(function(){var n,o,i;return a(this,(function(a){switch(a.label){case 0:return[4,e.getStateItem(r,"hash-".concat(t),[])];case 1:if(!(n=a.sent())||!n.storedValue.contract)throw new Error("contract data not found");if(!(o=n.storedValue.contract.namedKeys.keys.find((function(e){return e.name===s.EVENTS_SCHEMA_NAMED_KEY}))))throw new Error("'".concat(s.EVENTS_SCHEMA_NAMED_KEY,"' uref not found for contract '").concat(t,"'"));return[4,e.getStateItem(r,o.key.toString(),[])];case 2:if(!(i=a.sent()).storedValue.clValue)throw new Error("no CLValue for schema");return[2,i.storedValue.clValue.bytes()]}}))}))}},164:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.parseEventDataFromBytes=t.parseEventNameAndData=t.parseEventNameWithRemainder=void 0;var n=r(85);function a(e){var t=n.CLValueParser.fromBytesByType(e,n.CLTypeString),r=t.result.toString();if(!r.startsWith("event_"))throw new Error("no event_ prefix for event");return{result:r.replace("event_",""),bytes:t.bytes}}function o(e,t){for(var r={},a=t,o=0,s=e;o<s.length;o++){var i=s[o],c=n.CLValueParser.fromBytesByType(a,i.value);if(!c.bytes)throw new Error("remainder is empty");r[i.property]=c.result,a=c.bytes}return r}t.parseEventNameWithRemainder=a,t.parseEventNameAndData=function(e,t){var r=n.Conversions.decodeBase16(e),s=n.CLValueParser.fromBytesWithType(r);if(s.result.bytes().length<4)throw new Error("invalid event bytes");var i=a(s.result.bytes().subarray(4)),c=t[i.result];if(!c)throw new Error("event name not in schema");return{name:i.result,data:o(c,i.bytes)}},t.parseEventDataFromBytes=o},337:function(e,t,r){var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(a,o){function s(e){try{c(n.next(e))}catch(e){o(e)}}function i(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,i)}c((n=n.apply(e,t||[])).next())}))},a=this&&this.__generator||function(e,t){var r,n,a,o,s={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function i(i){return function(c){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,i[0]&&(s=0)),s;)try{if(r=1,n&&(a=2&i[0]?n.return:i[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,i[1])).done)return a;switch(n=0,a&&(i=[2&i[0],a.value]),i[0]){case 0:case 1:a=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!((a=(a=s.trys).length>0&&a[a.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!a||i[1]>a[0]&&i[1]<a[3])){s.label=i[1];break}if(6===i[0]&&s.label<a[1]){s.label=a[1],a=i;break}if(a&&s.label<a[2]){s.label=a[2],s.ops.push(i);break}a[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=a=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.fetchContractSchemasBytes=t.Parser=t.EVENTS_NAMED_KEY=t.EVENTS_SCHEMA_NAMED_KEY=void 0;var o=r(85),s=r(164),i=r(105);t.EVENTS_SCHEMA_NAMED_KEY="__events_schema",t.EVENTS_NAMED_KEY="__events";var c=function(){function e(e){this.contractsMetadata=e}return e.create=function(t,r){return n(this,void 0,void 0,(function(){var n;return a(this,(function(a){switch(a.label){case 0:return[4,e.getContractsMetadata(t,r)];case 1:return n=a.sent(),[2,Promise.resolve(new e(n))]}}))}))},e.getContractsMetadata=function(e,r){return n(this,void 0,void 0,(function(){var n,s,c,u,l,f,y,p,h,v,m,d,E,b;return a(this,(function(a){switch(a.label){case 0:return n={},[4,e.getStateRootHashLatest()];case 1:s=a.sent(),c=0,u=r,a.label=2;case 2:return c<u.length?(l=u[c],[4,e.getStateItem(s.stateRootHash.toHex(),"hash-".concat(l),[])]):[3,6];case 3:if(f=a.sent().storedValue.contract,y=Object.values(f.namedKeys),!f)throw new Error("contract data not found");for(p="",h="",v=0,m=y;v<m.length&&((d=m[v]).name===t.EVENTS_SCHEMA_NAMED_KEY?p=d.key:d.name===t.EVENTS_NAMED_KEY&&(h=d.key),""===p||""===h);v++);if(!p)throw new Error("no '".concat(t.EVENTS_SCHEMA_NAMED_KEY,"' uref found"));if(!h)throw new Error("no '".concat(t.EVENTS_NAMED_KEY,"' uref found"));return[4,e.getStateItem(s.stateRootHash.toHex(),p.toString(),[])];case 4:if(!(E=a.sent()).storedValue.clValue)throw new Error("no schema uref for ".concat(p));b=(0,i.parseSchemasFromBytes)(o.Conversions.decodeBase16(E.rawJSON.stored_value.CLValue.bytes)),n[h.toString()]={schemas:b,contractHash:o.Hash.fromHex(l),contractPackageHash:f.contractPackageHash.hash,eventsSchemaUref:p,eventsUref:h},a.label=5;case 5:return c++,[3,2];case 6:return[2,n]}}))}))},e.prototype.parseExecutionResult=function(e){if(!e.effects||0===e.effects.length)throw new Error("failed transaction ".concat(e.errorMessage));for(var t=[],r=0,n=e.effects;r<n.length;r++){var a=n[r];if(a.key.dictionary&&a.kind.isWriteCLValue()){var o=a.kind.parseAsWriteCLValue();if(o.any){var i=void 0,c=void 0;try{i=this.newDictionaryFromBytes(o.any.bytes()),c=(0,s.parseEventNameWithRemainder)(i.value)}catch(e){continue}var u={contractHash:null,contractPackageHash:null,name:c.result,eventId:parseInt(i.key),data:{}},l=this.contractsMetadata[i.uref];if(l){u.contractHash=l.contractHash,u.contractPackageHash=l.contractPackageHash;var f=l.schemas[c.result];if(f){try{u.data=(0,s.parseEventDataFromBytes)(f,c.bytes)}catch(e){t.push({event:u,error:"failed to parse event data bytes, err: ".concat(e.message)});continue}t.push({event:u,error:null})}else t.push({event:u,error:"event name not in schema"})}else t.push({event:u,error:"invalid event uref"})}}}return t},e.prototype.newDictionaryFromBytes=function(e){var t=o.CLValueParser.fromBytesWithType(e);if(!(t.result.type instanceof o.CLTypeList)||t.result.type.elementsType.getTypeID()!==o.TypeID.U8)throw new Error("failed to parse CLList(CLU8) from bytes");var r=o.CLValueUInt32.fromBytes(t.bytes),n=o.CLValueParser.fromBytesByType(r.bytes,new o.CLTypeByteArray(r.result.toNumber()));if(!n.result.byteArray)throw new Error("failed to parse CLByteArray from bytes");var a="uref-".concat(o.Conversions.encodeBase16(n.result.byteArray.bytes()),"-007"),s=o.CLValueParser.fromBytesByType(n.bytes,o.CLTypeString);if(!s.result.stringVal)throw new Error("failed to parse CLString from bytes");return{uref:a,key:s.result.stringVal.toString(),value:new Uint8Array(t.result.list.elements.map((function(e){return e.ui8.toNumber()})))}},e}();t.Parser=c,t.fetchContractSchemasBytes=function(e,r,o){return n(this,void 0,void 0,(function(){var n;return a(this,(function(a){switch(a.label){case 0:return[4,e.getStateItem(o,"hash-".concat(r),[t.EVENTS_SCHEMA_NAMED_KEY])];case 1:if(!(n=a.sent()).storedValue.clValue)throw new Error("no clvalue for contract schema");return[2,n.storedValue.clValue.bytes()]}}))}))}}},t={};function r(n){var a=t[n];if(void 0!==a)return a.exports;var o=t[n]={exports:{}};return e[n].call(o.exports,o,o.exports,r),o.exports}var n={};(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.parseEventNameWithRemainder=e.parseEventDataFromBytes=e.parseEventNameAndData=e.parseSchemasFromBytes=e.fetchContractSchemasBytes=e.Parser=void 0;var t=r(337);Object.defineProperty(e,"Parser",{enumerable:!0,get:function(){return t.Parser}}),Object.defineProperty(e,"fetchContractSchemasBytes",{enumerable:!0,get:function(){return t.fetchContractSchemasBytes}});var a=r(105);Object.defineProperty(e,"parseSchemasFromBytes",{enumerable:!0,get:function(){return a.parseSchemasFromBytes}});var o=r(164);Object.defineProperty(e,"parseEventNameAndData",{enumerable:!0,get:function(){return o.parseEventNameAndData}}),Object.defineProperty(e,"parseEventDataFromBytes",{enumerable:!0,get:function(){return o.parseEventDataFromBytes}}),Object.defineProperty(e,"parseEventNameWithRemainder",{enumerable:!0,get:function(){return o.parseEventNameWithRemainder}})})(),module.exports=n})();