@vleap/warps 0.0.53 → 0.0.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -97,6 +97,12 @@ type Brand = {
|
|
|
97
97
|
label: string;
|
|
98
98
|
url: string;
|
|
99
99
|
};
|
|
100
|
+
meta?: BrandMeta;
|
|
101
|
+
};
|
|
102
|
+
type BrandMeta = {
|
|
103
|
+
hash: string;
|
|
104
|
+
creator: string;
|
|
105
|
+
createdAt: string;
|
|
100
106
|
};
|
|
101
107
|
|
|
102
108
|
declare class BrandBuilder {
|
|
@@ -213,4 +219,4 @@ declare class WarpRegistry {
|
|
|
213
219
|
private getController;
|
|
214
220
|
}
|
|
215
221
|
|
|
216
|
-
export { type Brand, BrandBuilder, type ChainEnv, Config, type TrustStatus, type Warp, type WarpAction, type WarpActionExecutionResult, WarpActionExecutor, type WarpActionInput, type WarpActionInputPosition, type WarpActionInputSource, type WarpActionInputType, type WarpActionType, WarpBuilder, type WarpCacheConfig, type WarpConfig, type WarpContractAction, type WarpIdType, type WarpInfo, WarpLink, type WarpLinkAction, type WarpMeta, WarpRegistry };
|
|
222
|
+
export { type Brand, BrandBuilder, type BrandMeta, type ChainEnv, Config, type TrustStatus, type Warp, type WarpAction, type WarpActionExecutionResult, WarpActionExecutor, type WarpActionInput, type WarpActionInputPosition, type WarpActionInputSource, type WarpActionInputType, type WarpActionType, WarpBuilder, type WarpCacheConfig, type WarpConfig, type WarpContractAction, type WarpIdType, type WarpInfo, WarpLink, type WarpLinkAction, type WarpMeta, WarpRegistry };
|
package/dist/index.d.ts
CHANGED
|
@@ -97,6 +97,12 @@ type Brand = {
|
|
|
97
97
|
label: string;
|
|
98
98
|
url: string;
|
|
99
99
|
};
|
|
100
|
+
meta?: BrandMeta;
|
|
101
|
+
};
|
|
102
|
+
type BrandMeta = {
|
|
103
|
+
hash: string;
|
|
104
|
+
creator: string;
|
|
105
|
+
createdAt: string;
|
|
100
106
|
};
|
|
101
107
|
|
|
102
108
|
declare class BrandBuilder {
|
|
@@ -213,4 +219,4 @@ declare class WarpRegistry {
|
|
|
213
219
|
private getController;
|
|
214
220
|
}
|
|
215
221
|
|
|
216
|
-
export { type Brand, BrandBuilder, type ChainEnv, Config, type TrustStatus, type Warp, type WarpAction, type WarpActionExecutionResult, WarpActionExecutor, type WarpActionInput, type WarpActionInputPosition, type WarpActionInputSource, type WarpActionInputType, type WarpActionType, WarpBuilder, type WarpCacheConfig, type WarpConfig, type WarpContractAction, type WarpIdType, type WarpInfo, WarpLink, type WarpLinkAction, type WarpMeta, WarpRegistry };
|
|
222
|
+
export { type Brand, BrandBuilder, type BrandMeta, type ChainEnv, Config, type TrustStatus, type Warp, type WarpAction, type WarpActionExecutionResult, WarpActionExecutor, type WarpActionInput, type WarpActionInputPosition, type WarpActionInputSource, type WarpActionInputType, type WarpActionType, WarpBuilder, type WarpCacheConfig, type WarpConfig, type WarpContractAction, type WarpIdType, type WarpInfo, WarpLink, type WarpLinkAction, type WarpMeta, WarpRegistry };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var $=Object.create;var C=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,O=Object.prototype.hasOwnProperty;var Q=(i,r)=>{for(var t in r)C(i,t,{get:r[t],enumerable:!0})},S=(i,r,t,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of _(r))!O.call(i,n)&&n!==t&&C(i,n,{get:()=>r[n],enumerable:!(e=D(r,n))||e.enumerable});return i};var I=(i,r,t)=>(t=i!=null?$(k(i)):{},S(r||!i||!i.__esModule?C(t,"default",{value:i,enumerable:!0}):t,i)),H=i=>S(C({},"__esModule",{value:!0}),i);var K={};Q(K,{BrandBuilder:()=>P,Config:()=>c,WarpActionExecutor:()=>R,WarpBuilder:()=>B,WarpLink:()=>U,WarpRegistry:()=>v});module.exports=H(K);var h=require("@multiversx/sdk-core"),E=I(require("ajv"));var c={ProtocolNameWarp:"warp",ProtocolNameBrand:"warp-brand",LatestProtocolVersion:"0.1.0",LatestWarpSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/v0.1.0.schema.json",LatestBrandSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/brand/v0.1.0.schema.json",DefaultClientUrl:i=>i==="devnet"?"https://devnet.xwarp.me/to":i==="testnet"?"###Not implemented###":"https://xwarp.me/to",Chain:{ApiUrl:i=>i==="devnet"?"https://devnet-api.multiversx.com":i==="testnet"?"https://testnet-api.multiversx.com":"https://api.multiversx.com"},Registry:{Contract:i=>i==="devnet"?"erd1qqqqqqqqqqqqqpgqje2f99vr6r7sk54thg03c9suzcvwr4nfl3tsfkdl36":i==="testnet"?"####":"erd1qqqqqqqqqqqqqpgq3mrpj3u6q7tejv6d7eqhnyd27n9v5c5tl3ts08mffe"},AvailableActionInputSources:["field","query"],AvailableActionInputTypes:["string","uint8","uint16","uint32","uint64","biguint","boolean","address"],AvailableActionInputPositions:["value","arg:1","arg:2","arg:3","arg:4","arg:5","arg:6","arg:7","arg:8","arg:9","arg:10"]};var y=i=>i==="devnet"?"D":i==="testnet"?"T":"1",b=i=>`${i}:${c.LatestProtocolVersion}`,A=i=>{var r,t;return{hash:i.hash.toString("hex"),alias:((r=i.alias)==null?void 0:r.toString())||null,trust:i.trust.toString(),creator:i.creator.toString(),createdAt:i.created_at.toNumber(),brand:((t=i.brand)==null?void 0:t.toString())||null}};var P=class{config;pendingBrand={protocol:b(c.ProtocolNameBrand),name:"",description:"",logo:"",website:""};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("BrandBuilder: user address not set");let t=new h.TransactionsFactoryConfig({chainID:y(this.config.env)}),e=new h.TransferTransactionsFactory({config:t}),n=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:h.Address.newFromBech32(this.config.userAddress),receiver:h.Address.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(n).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){return await this.createFromRaw(r.data.toString(),t)}async createFromTransactionHash(r){let t=new h.ApiNetworkProvider(this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env));try{let e=await t.getTransaction(r);return this.createFromTransaction(e)}catch(e){return console.error("BrandBuilder: Error creating from transaction hash",e),null}}setName(r){return this.pendingBrand.name=r,this}setDescription(r){return this.pendingBrand.description=r,this}setLogo(r){return this.pendingBrand.logo=r,this}setWebsite(r){return this.pendingBrand.website=r,this}setColors(r){return this.pendingBrand.colors=r,this}setCta(r){return this.pendingBrand.cta=r,this}async build(){return this.ensure(this.pendingBrand.name,"name is required"),this.ensure(this.pendingBrand.description,"description is required"),this.ensure(this.pendingBrand.logo,"logo is required"),await this.ensureValidSchema(this.pendingBrand),this.pendingBrand}ensure(r,t){if(!r)throw new Error(`Warp: ${t}`)}async ensureValidSchema(r){let t=this.config.brandSchemaUrl||c.LatestBrandSchemaUrl,n=await(await fetch(t)).json(),a=new E.default,o=a.compile(n);if(!o(r))throw new Error(`BrandBuilder: schema validation failed: ${a.errorsText(o.errors)}`)}};var u=require("@multiversx/sdk-core/out");var R=class{config;url;constructor(r,t){this.config=r,this.url=new URL(t)}createTransactionForExecute(r){if(!this.config.userAddress)throw new Error("WarpActionExecutor: user address not set");let t=new u.TransactionsFactoryConfig({chainID:y(this.config.env)}),e=new u.SmartContractTransactionsFactory({config:t}),n=this.getTypedArgsWithInputs(r),a=this.getPositionValueFromUrl(r,"value"),o=BigInt(a||r.value||0);return e.createTransactionForExecute({sender:u.Address.newFromBech32(this.config.userAddress),contract:u.Address.newFromBech32(r.address),function:r.func||"",gasLimit:BigInt(r.gasLimit),arguments:n,nativeTransferAmount:o})}getPositionValueFromUrl(r,t){var o,g;let e=new URLSearchParams(this.url.search),n=(o=r.inputs)==null?void 0:o.filter(l=>l.source==="query"),a=(g=n==null?void 0:n.find(l=>l.position===t))==null?void 0:g.name;return a?e.get(a):null}getTypedArgsWithInputs(r){return r.args.map(t=>{let[e,n]=t.split(":");return this.toTypedArg(n,e)})}toTypedArg(r,t){if(t==="string")return u.BytesValue.fromUTF8(r);if(t==="uint8")return new u.U8Value(Number(r));if(t==="uint16")return new u.U16Value(Number(r));if(t==="uint32")return new u.U32Value(Number(r));if(t==="uint64")return new u.U64Value(BigInt(r));if(t==="biguint")return new u.BigUIntValue(BigInt(r));if(t==="boolean")return new u.BooleanValue(r==="true");if(t==="address")return new u.AddressValue(u.Address.newFromBech32(r));if(t==="hex")return u.BytesValue.fromHex(r);throw new Error(`WarpActionExecutor: Unsupported input type: ${t}`)}};var m=require("@multiversx/sdk-core"),L=I(require("ajv"));var W={Warp:i=>`warp:${i}`,WarpInfo:i=>`warp-info:${i}`,Brand:i=>`brand:${i}`},w=class{cache=new Map;set(r,t,e){let n=Date.now()+e*1e3;this.cache.set(r,{value:t,expiresAt:n})}get(r){let t=this.cache.get(r);return t?Date.now()>t.expiresAt?(this.cache.delete(r),null):t.value:null}clear(){this.cache.clear()}};var B=class{config;cache=new w;pendingWarp={protocol:b(c.ProtocolNameWarp),name:"",title:"",description:null,preview:"",actions:[]};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("WarpBuilder: user address not set");let t=new m.TransactionsFactoryConfig({chainID:y(this.config.env)}),e=new m.TransferTransactionsFactory({config:t}),n=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:m.Address.newFromBech32(this.config.userAddress),receiver:m.Address.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(n).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){let e=await this.createFromRaw(r.data.toString(),t);return e.meta={hash:r.hash,creator:r.sender.bech32(),createdAt:new Date(r.timestamp).toISOString()},e}async createFromTransactionHash(r,t){let e=W.Warp(r);if(t){let a=this.cache.get(e);if(a)return console.log(`WarpBuilder (createFromTransactionHash): Warp found in cache: ${r}`),a}let n=new m.ApiNetworkProvider(this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env));try{let a=await n.getTransaction(r),o=await this.createFromTransaction(a);return t&&t.ttl&&o&&this.cache.set(e,o,t.ttl),o}catch(a){return console.error("WarpBuilder: Error creating from transaction hash",a),null}}setName(r){return this.pendingWarp.name=r,this}setTitle(r){return this.pendingWarp.title=r,this}setDescription(r){return this.pendingWarp.description=r,this}setPreview(r){return this.pendingWarp.preview=r,this}setActions(r){return this.pendingWarp.actions=r,this}addAction(r){return this.pendingWarp.actions.push(r),this}async build(){return this.ensure(this.pendingWarp.protocol,"protocol is required"),this.ensure(this.pendingWarp.name,"name is required"),this.ensure(this.pendingWarp.title,"title is required"),this.ensure(this.pendingWarp.actions.length>0,"actions are required"),await this.ensureValidSchema(this.pendingWarp),this.pendingWarp}ensure(r,t){if(!r)throw new Error(`WarpBuilder: ${t}`)}async ensureValidSchema(r){let t=this.config.warpSchemaUrl||c.LatestWarpSchemaUrl,n=await(await fetch(t)).json(),a=new L.default,o=a.compile(n);if(!o(r))throw new Error(`WarpBuilder: schema validation failed: ${a.errorsText(o.errors)}`)}};var V=I(require("qr-code-styling"));var s=require("@multiversx/sdk-core/out");var F={buildInfo:{rustc:{version:"1.80.0-nightly",commitHash:"791adf759cc065316f054961875052d5bc03e16c",commitDate:"2024-05-21",channel:"Nightly",short:"rustc 1.80.0-nightly (791adf759 2024-05-21)"},contractCrate:{name:"registry",version:"0.0.1"},framework:{name:"multiversx-sc",version:"0.50.6"}},name:"RegistryContract",constructor:{inputs:[{name:"unit_price",type:"BigUint"}],outputs:[]},upgradeConstructor:{inputs:[],outputs:[]},endpoints:[{name:"registerWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"},{name:"alias_opt",type:"optional<bytes>",multi_arg:!0},{name:"brand_opt",type:"optional<bytes>",multi_arg:!0}],outputs:[],allow_multiple_var_args:!0},{name:"publishWarp",mutability:"mutable",inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"assignAlias",mutability:"mutable",inputs:[{name:"alias",type:"bytes"}],outputs:[]},{name:"getUserWarps",mutability:"readonly",inputs:[{name:"address",type:"Address"}],outputs:[{type:"variadic<WarpInfoView>",multi_result:!0}]},{name:"getInfoByAlias",mutability:"readonly",inputs:[{name:"alias",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"getInfoByHash",mutability:"readonly",inputs:[{name:"hash",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"setUnitPrice",onlyOwner:!0,mutability:"mutable",inputs:[{name:"amount",type:"BigUint"}],outputs:[]},{name:"getConfig",mutability:"readonly",inputs:[],outputs:[{type:"BigUint"}]},{name:"registerBrand",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"brandWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"warp",type:"bytes"},{name:"brand",type:"bytes"}],outputs:[]},{name:"getUserBrands",mutability:"readonly",inputs:[{name:"user",type:"Address"}],outputs:[{type:"variadic<bytes>",multi_result:!0}]}],events:[{identifier:"warpRegistered",inputs:[{name:"hash",type:"bytes",indexed:!0},{name:"alias",type:"bytes",indexed:!0}]}],esdtAttributes:[],hasCallback:!1,types:{WarpInfoView:{type:"struct",fields:[{name:"hash",type:"bytes"},{name:"alias",type:"Option<bytes>"},{name:"trust",type:"bytes"},{name:"creator",type:"Address"},{name:"created_at",type:"u64"}]}}};var v=class{config;unitPrice;cache=new w;constructor(r){this.config=r,this.unitPrice=BigInt(0)}async init(){await this.loadRegistryConfigs()}createRegisterWarpTransaction(r,t){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");let e=t?this.unitPrice*BigInt(2):this.unitPrice;return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"registerWarp",gasLimit:BigInt(1e7),nativeTransferAmount:e,arguments:t?[s.BytesValue.fromHex(r),s.BytesValue.fromUTF8(t)]:[s.BytesValue.fromHex(r)]})}createRegisterBrandTransaction(r){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"registerBrand",gasLimit:BigInt(1e7),nativeTransferAmount:this.unitPrice,arguments:[s.BytesValue.fromHex(r)]})}createAliasAssignTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"assignAlias",gasLimit:BigInt(1e7),arguments:[s.BytesValue.fromHex(r),s.BytesValue.fromUTF8(t)]})}createPublishWarpTransaction(r){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"publishWarp",gasLimit:BigInt(1e7),arguments:[s.BytesValue.fromHex(r)]})}createBrandWarpTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"brandWarp",gasLimit:BigInt(1e7),arguments:[s.BytesValue.fromHex(r),s.BytesValue.fromHex(t)]})}async getInfoByAlias(r,t){let e=W.WarpInfo(r);if(t){let d=this.cache.get(e);if(d)return console.log(`WarpRegistry (getInfoByAlias): WarpInfo found in cache: ${r}`),d}let n=c.Registry.Contract(this.config.env),a=this.getController(),o=a.createQuery({contract:n,function:"getInfoByAlias",arguments:[s.BytesValue.fromUTF8(r)]}),g=await a.runQuery(o),[l]=a.parseQueryResponse(g),p=l?A(l):null,f=p!=null&&p.brand?await this.fetchBrand(p.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:p,brand:f},t.ttl),{warp:p,brand:f}}async getInfoByHash(r,t){let e=W.WarpInfo(r);if(t){let d=this.cache.get(e);if(d)return console.log(`WarpRegistry (getInfoByHash): WarpInfo found in cache: ${r}`),d}let n=c.Registry.Contract(this.config.env),a=this.getController(),o=a.createQuery({contract:n,function:"getInfoByHash",arguments:[s.BytesValue.fromUTF8(r)]}),g=await a.runQuery(o),[l]=a.parseQueryResponse(g),p=l?A(l):null,f=p!=null&&p.brand?await this.fetchBrand(p.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:p,brand:f},t.ttl),{warp:p,brand:f}}async getUserWarpInfos(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=c.Registry.Contract(this.config.env),n=this.getController(),a=n.createQuery({contract:e,function:"getUserWarps",arguments:[new s.AddressValue(new s.Address(t))]}),o=await n.runQuery(a),[g]=n.parseQueryResponse(o);return g.map(A)}async getUserBrands(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=c.Registry.Contract(this.config.env),n=this.getController(),a=n.createQuery({contract:e,function:"getUserBrands",arguments:[new s.AddressValue(new s.Address(t))]}),o=await n.runQuery(a),[g]=n.parseQueryResponse(o),l=g.map(d=>d.toString("hex")),p={ttl:365*24*60*60};return(await Promise.all(l.map(d=>this.fetchBrand(d,p)))).filter(d=>d!==null)}async fetchBrand(r,t){let e=W.Brand(r);if(t){let a=this.cache.get(e);if(a)return console.log(`WarpRegistry (fetchBrand): Brand found in cache: ${r}`),a}let n=new s.ApiNetworkProvider(this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env));try{let a=await n.getTransaction(r),o=JSON.parse(a.data.toString());return t&&t.ttl&&this.cache.set(e,o,t.ttl),o}catch(a){return console.error("WarpRegistry: Error fetching brand from transaction hash",a),null}}async loadRegistryConfigs(){let r=c.Registry.Contract(this.config.env),t=this.getController(),e=t.createQuery({contract:r,function:"getConfig",arguments:[]}),n=await t.runQuery(e),[a]=t.parseQueryResponse(n),o=BigInt(a.toString());this.unitPrice=o}getFactory(){let r=new s.TransactionsFactoryConfig({chainID:y(this.config.env)}),t=s.AbiRegistry.create(F);return new s.SmartContractTransactionsFactory({config:r,abi:t})}getController(){let r=this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env),t=new s.ApiNetworkProvider(r,{timeout:3e4}),e=new s.QueryRunnerAdapter({networkProvider:t}),n=s.AbiRegistry.create(F);return new s.SmartContractQueriesController({queryRunner:e,abi:n})}};var x="xwarp",T=":",N="alias",U=class{constructor(r){this.config=r;this.config=r}async detect(r){let n=new URL(r).searchParams.get(x);if(!n)return{match:!1,warp:null};let a=decodeURIComponent(n),o=a.includes(T)?a:`${N}${T}${a}`,[g,l]=o.split(T),p=new B(this.config),f=new v(this.config),d=null;if(g==="hash")d=await p.createFromTransactionHash(l);else if(g==="alias"){let{warp:q}=await f.getInfoByAlias(l);q&&(d=await p.createFromTransactionHash(q.hash))}return d?{match:!0,warp:d}:{match:!1,warp:null}}build(r,t){let e=this.config.clientUrl||c.DefaultClientUrl(this.config.env);return r===N?`${e}?${x}=${encodeURIComponent(t)}`:`${e}?${x}=${encodeURIComponent(r+T+t)}`}generateQrCode(r,t,e=512,n="white",a="black",o="#23F7DD"){let g=this.build(r,t);return new V.default({type:"svg",width:e,height:e,data:String(g),margin:16,qrOptions:{typeNumber:0,mode:"Byte",errorCorrectionLevel:"Q"},backgroundOptions:{color:n},dotsOptions:{type:"extra-rounded",color:a},cornersSquareOptions:{type:"extra-rounded",color:a},cornersDotOptions:{type:"square",color:a},imageOptions:{hideBackgroundDots:!0,imageSize:.4,margin:8},image:`data:image/svg+xml;utf8,<svg width="16" height="16" viewBox="0 0 100 100" fill="${encodeURIComponent(o)}" xmlns="http://www.w3.org/2000/svg"><path d="M54.8383 50.0242L95 28.8232L88.2456 16L51.4717 30.6974C50.5241 31.0764 49.4759 31.0764 48.5283 30.6974L11.7544 16L5 28.8232L45.1616 50.0242L5 71.2255L11.7544 84.0488L48.5283 69.351C49.4759 68.9724 50.5241 68.9724 51.4717 69.351L88.2456 84.0488L95 71.2255L54.8383 50.0242Z"/></svg>`})}};0&&(module.exports={BrandBuilder,Config,WarpActionExecutor,WarpBuilder,WarpLink,WarpRegistry});
|
|
1
|
+
"use strict";var D=Object.create;var C=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty;var Q=(i,r)=>{for(var t in r)C(i,t,{get:r[t],enumerable:!0})},S=(i,r,t,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let a of O(r))!k.call(i,a)&&a!==t&&C(i,a,{get:()=>r[a],enumerable:!(e=$(r,a))||e.enumerable});return i};var I=(i,r,t)=>(t=i!=null?D(_(i)):{},S(r||!i||!i.__esModule?C(t,"default",{value:i,enumerable:!0}):t,i)),H=i=>S(C({},"__esModule",{value:!0}),i);var K={};Q(K,{BrandBuilder:()=>P,Config:()=>c,WarpActionExecutor:()=>R,WarpBuilder:()=>B,WarpLink:()=>U,WarpRegistry:()=>v});module.exports=H(K);var h=require("@multiversx/sdk-core"),E=I(require("ajv"));var c={ProtocolNameWarp:"warp",ProtocolNameBrand:"warp-brand",LatestProtocolVersion:"0.1.0",LatestWarpSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/v0.1.0.schema.json",LatestBrandSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/brand/v0.1.0.schema.json",DefaultClientUrl:i=>i==="devnet"?"https://devnet.xwarp.me/to":i==="testnet"?"###Not implemented###":"https://xwarp.me/to",Chain:{ApiUrl:i=>i==="devnet"?"https://devnet-api.multiversx.com":i==="testnet"?"https://testnet-api.multiversx.com":"https://api.multiversx.com"},Registry:{Contract:i=>i==="devnet"?"erd1qqqqqqqqqqqqqpgqje2f99vr6r7sk54thg03c9suzcvwr4nfl3tsfkdl36":i==="testnet"?"####":"erd1qqqqqqqqqqqqqpgq3mrpj3u6q7tejv6d7eqhnyd27n9v5c5tl3ts08mffe"},AvailableActionInputSources:["field","query"],AvailableActionInputTypes:["string","uint8","uint16","uint32","uint64","biguint","boolean","address"],AvailableActionInputPositions:["value","arg:1","arg:2","arg:3","arg:4","arg:5","arg:6","arg:7","arg:8","arg:9","arg:10"]};var y=i=>i==="devnet"?"D":i==="testnet"?"T":"1",b=i=>`${i}:${c.LatestProtocolVersion}`,A=i=>{var r,t;return{hash:i.hash.toString("hex"),alias:((r=i.alias)==null?void 0:r.toString())||null,trust:i.trust.toString(),creator:i.creator.toString(),createdAt:i.created_at.toNumber(),brand:((t=i.brand)==null?void 0:t.toString())||null}};var P=class{config;pendingBrand={protocol:b(c.ProtocolNameBrand),name:"",description:"",logo:"",website:""};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("BrandBuilder: user address not set");let t=new h.TransactionsFactoryConfig({chainID:y(this.config.env)}),e=new h.TransferTransactionsFactory({config:t}),a=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:h.Address.newFromBech32(this.config.userAddress),receiver:h.Address.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(a).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){return await this.createFromRaw(r.data.toString(),t)}async createFromTransactionHash(r){let t=new h.ApiNetworkProvider(this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env));try{let e=await t.getTransaction(r);return this.createFromTransaction(e)}catch(e){return console.error("BrandBuilder: Error creating from transaction hash",e),null}}setName(r){return this.pendingBrand.name=r,this}setDescription(r){return this.pendingBrand.description=r,this}setLogo(r){return this.pendingBrand.logo=r,this}setWebsite(r){return this.pendingBrand.website=r,this}setColors(r){return this.pendingBrand.colors=r,this}setCta(r){return this.pendingBrand.cta=r,this}async build(){return this.ensure(this.pendingBrand.name,"name is required"),this.ensure(this.pendingBrand.description,"description is required"),this.ensure(this.pendingBrand.logo,"logo is required"),await this.ensureValidSchema(this.pendingBrand),this.pendingBrand}ensure(r,t){if(!r)throw new Error(`Warp: ${t}`)}async ensureValidSchema(r){let t=this.config.brandSchemaUrl||c.LatestBrandSchemaUrl,a=await(await fetch(t)).json(),n=new E.default,o=n.compile(a);if(!o(r))throw new Error(`BrandBuilder: schema validation failed: ${n.errorsText(o.errors)}`)}};var u=require("@multiversx/sdk-core/out");var R=class{config;url;constructor(r,t){this.config=r,this.url=new URL(t)}createTransactionForExecute(r){if(!this.config.userAddress)throw new Error("WarpActionExecutor: user address not set");let t=new u.TransactionsFactoryConfig({chainID:y(this.config.env)}),e=new u.SmartContractTransactionsFactory({config:t}),a=this.getTypedArgsWithInputs(r),n=this.getPositionValueFromUrl(r,"value"),o=BigInt(n||r.value||0);return e.createTransactionForExecute({sender:u.Address.newFromBech32(this.config.userAddress),contract:u.Address.newFromBech32(r.address),function:r.func||"",gasLimit:BigInt(r.gasLimit),arguments:a,nativeTransferAmount:o})}getPositionValueFromUrl(r,t){var o,g;let e=new URLSearchParams(this.url.search),a=(o=r.inputs)==null?void 0:o.filter(l=>l.source==="query"),n=(g=a==null?void 0:a.find(l=>l.position===t))==null?void 0:g.name;return n?e.get(n):null}getTypedArgsWithInputs(r){return r.args.map(t=>{let[e,a]=t.split(":");return this.toTypedArg(a,e)})}toTypedArg(r,t){if(t==="string")return u.BytesValue.fromUTF8(r);if(t==="uint8")return new u.U8Value(Number(r));if(t==="uint16")return new u.U16Value(Number(r));if(t==="uint32")return new u.U32Value(Number(r));if(t==="uint64")return new u.U64Value(BigInt(r));if(t==="biguint")return new u.BigUIntValue(BigInt(r));if(t==="boolean")return new u.BooleanValue(r==="true");if(t==="address")return new u.AddressValue(u.Address.newFromBech32(r));if(t==="hex")return u.BytesValue.fromHex(r);throw new Error(`WarpActionExecutor: Unsupported input type: ${t}`)}};var m=require("@multiversx/sdk-core"),L=I(require("ajv"));var W={Warp:i=>`warp:${i}`,WarpInfo:i=>`warp-info:${i}`,Brand:i=>`brand:${i}`},w=class{cache=new Map;set(r,t,e){let a=Date.now()+e*1e3;this.cache.set(r,{value:t,expiresAt:a})}get(r){let t=this.cache.get(r);return t?Date.now()>t.expiresAt?(this.cache.delete(r),null):t.value:null}clear(){this.cache.clear()}};var B=class{config;cache=new w;pendingWarp={protocol:b(c.ProtocolNameWarp),name:"",title:"",description:null,preview:"",actions:[]};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("WarpBuilder: user address not set");let t=new m.TransactionsFactoryConfig({chainID:y(this.config.env)}),e=new m.TransferTransactionsFactory({config:t}),a=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:m.Address.newFromBech32(this.config.userAddress),receiver:m.Address.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(a).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){let e=await this.createFromRaw(r.data.toString(),t);return e.meta={hash:r.hash,creator:r.sender.bech32(),createdAt:new Date(r.timestamp).toISOString()},e}async createFromTransactionHash(r,t){let e=W.Warp(r);if(t){let n=this.cache.get(e);if(n)return console.log(`WarpBuilder (createFromTransactionHash): Warp found in cache: ${r}`),n}let a=new m.ApiNetworkProvider(this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env));try{let n=await a.getTransaction(r),o=await this.createFromTransaction(n);return t&&t.ttl&&o&&this.cache.set(e,o,t.ttl),o}catch(n){return console.error("WarpBuilder: Error creating from transaction hash",n),null}}setName(r){return this.pendingWarp.name=r,this}setTitle(r){return this.pendingWarp.title=r,this}setDescription(r){return this.pendingWarp.description=r,this}setPreview(r){return this.pendingWarp.preview=r,this}setActions(r){return this.pendingWarp.actions=r,this}addAction(r){return this.pendingWarp.actions.push(r),this}async build(){return this.ensure(this.pendingWarp.protocol,"protocol is required"),this.ensure(this.pendingWarp.name,"name is required"),this.ensure(this.pendingWarp.title,"title is required"),this.ensure(this.pendingWarp.actions.length>0,"actions are required"),await this.ensureValidSchema(this.pendingWarp),this.pendingWarp}ensure(r,t){if(!r)throw new Error(`WarpBuilder: ${t}`)}async ensureValidSchema(r){let t=this.config.warpSchemaUrl||c.LatestWarpSchemaUrl,a=await(await fetch(t)).json(),n=new L.default,o=n.compile(a);if(!o(r))throw new Error(`WarpBuilder: schema validation failed: ${n.errorsText(o.errors)}`)}};var V=I(require("qr-code-styling"));var s=require("@multiversx/sdk-core/out");var F={buildInfo:{rustc:{version:"1.80.0-nightly",commitHash:"791adf759cc065316f054961875052d5bc03e16c",commitDate:"2024-05-21",channel:"Nightly",short:"rustc 1.80.0-nightly (791adf759 2024-05-21)"},contractCrate:{name:"registry",version:"0.0.1"},framework:{name:"multiversx-sc",version:"0.50.6"}},name:"RegistryContract",constructor:{inputs:[{name:"unit_price",type:"BigUint"}],outputs:[]},upgradeConstructor:{inputs:[],outputs:[]},endpoints:[{name:"registerWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"},{name:"alias_opt",type:"optional<bytes>",multi_arg:!0},{name:"brand_opt",type:"optional<bytes>",multi_arg:!0}],outputs:[],allow_multiple_var_args:!0},{name:"publishWarp",mutability:"mutable",inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"assignAlias",mutability:"mutable",inputs:[{name:"alias",type:"bytes"}],outputs:[]},{name:"getUserWarps",mutability:"readonly",inputs:[{name:"address",type:"Address"}],outputs:[{type:"variadic<WarpInfoView>",multi_result:!0}]},{name:"getInfoByAlias",mutability:"readonly",inputs:[{name:"alias",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"getInfoByHash",mutability:"readonly",inputs:[{name:"hash",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"setUnitPrice",onlyOwner:!0,mutability:"mutable",inputs:[{name:"amount",type:"BigUint"}],outputs:[]},{name:"getConfig",mutability:"readonly",inputs:[],outputs:[{type:"BigUint"}]},{name:"registerBrand",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"brandWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"warp",type:"bytes"},{name:"brand",type:"bytes"}],outputs:[]},{name:"getUserBrands",mutability:"readonly",inputs:[{name:"user",type:"Address"}],outputs:[{type:"variadic<bytes>",multi_result:!0}]}],events:[{identifier:"warpRegistered",inputs:[{name:"hash",type:"bytes",indexed:!0},{name:"alias",type:"bytes",indexed:!0}]}],esdtAttributes:[],hasCallback:!1,types:{WarpInfoView:{type:"struct",fields:[{name:"hash",type:"bytes"},{name:"alias",type:"Option<bytes>"},{name:"trust",type:"bytes"},{name:"creator",type:"Address"},{name:"created_at",type:"u64"}]}}};var v=class{config;unitPrice;cache=new w;constructor(r){this.config=r,this.unitPrice=BigInt(0)}async init(){await this.loadRegistryConfigs()}createRegisterWarpTransaction(r,t){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");let e=t?this.unitPrice*BigInt(2):this.unitPrice;return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"registerWarp",gasLimit:BigInt(1e7),nativeTransferAmount:e,arguments:t?[s.BytesValue.fromHex(r),s.BytesValue.fromUTF8(t)]:[s.BytesValue.fromHex(r)]})}createRegisterBrandTransaction(r){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"registerBrand",gasLimit:BigInt(1e7),nativeTransferAmount:this.unitPrice,arguments:[s.BytesValue.fromHex(r)]})}createAliasAssignTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"assignAlias",gasLimit:BigInt(1e7),arguments:[s.BytesValue.fromHex(r),s.BytesValue.fromUTF8(t)]})}createPublishWarpTransaction(r){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"publishWarp",gasLimit:BigInt(1e7),arguments:[s.BytesValue.fromHex(r)]})}createBrandWarpTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:s.Address.newFromBech32(this.config.userAddress),contract:s.Address.newFromBech32(c.Registry.Contract(this.config.env)),function:"brandWarp",gasLimit:BigInt(1e7),nativeTransferAmount:this.unitPrice,arguments:[s.BytesValue.fromHex(r),s.BytesValue.fromHex(t)]})}async getInfoByAlias(r,t){let e=W.WarpInfo(r);if(t){let d=this.cache.get(e);if(d)return console.log(`WarpRegistry (getInfoByAlias): WarpInfo found in cache: ${r}`),d}let a=c.Registry.Contract(this.config.env),n=this.getController(),o=n.createQuery({contract:a,function:"getInfoByAlias",arguments:[s.BytesValue.fromUTF8(r)]}),g=await n.runQuery(o),[l]=n.parseQueryResponse(g),p=l?A(l):null,f=p!=null&&p.brand?await this.fetchBrand(p.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:p,brand:f},t.ttl),{warp:p,brand:f}}async getInfoByHash(r,t){let e=W.WarpInfo(r);if(t){let d=this.cache.get(e);if(d)return console.log(`WarpRegistry (getInfoByHash): WarpInfo found in cache: ${r}`),d}let a=c.Registry.Contract(this.config.env),n=this.getController(),o=n.createQuery({contract:a,function:"getInfoByHash",arguments:[s.BytesValue.fromUTF8(r)]}),g=await n.runQuery(o),[l]=n.parseQueryResponse(g),p=l?A(l):null,f=p!=null&&p.brand?await this.fetchBrand(p.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:p,brand:f},t.ttl),{warp:p,brand:f}}async getUserWarpInfos(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=c.Registry.Contract(this.config.env),a=this.getController(),n=a.createQuery({contract:e,function:"getUserWarps",arguments:[new s.AddressValue(new s.Address(t))]}),o=await a.runQuery(n),[g]=a.parseQueryResponse(o);return g.map(A)}async getUserBrands(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=c.Registry.Contract(this.config.env),a=this.getController(),n=a.createQuery({contract:e,function:"getUserBrands",arguments:[new s.AddressValue(new s.Address(t))]}),o=await a.runQuery(n),[g]=a.parseQueryResponse(o),l=g.map(d=>d.toString("hex")),p={ttl:365*24*60*60};return(await Promise.all(l.map(d=>this.fetchBrand(d,p)))).filter(d=>d!==null)}async fetchBrand(r,t){let e=W.Brand(r);if(t){let n=this.cache.get(e);if(n)return console.log(`WarpRegistry (fetchBrand): Brand found in cache: ${r}`),n}let a=new s.ApiNetworkProvider(this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env));try{let n=await a.getTransaction(r),o=JSON.parse(n.data.toString());return o.meta={hash:n.hash,creator:n.sender.bech32(),createdAt:new Date(n.timestamp).toISOString()},t&&t.ttl&&this.cache.set(e,o,t.ttl),o}catch(n){return console.error("WarpRegistry: Error fetching brand from transaction hash",n),null}}async loadRegistryConfigs(){let r=c.Registry.Contract(this.config.env),t=this.getController(),e=t.createQuery({contract:r,function:"getConfig",arguments:[]}),a=await t.runQuery(e),[n]=t.parseQueryResponse(a),o=BigInt(n.toString());this.unitPrice=o}getFactory(){let r=new s.TransactionsFactoryConfig({chainID:y(this.config.env)}),t=s.AbiRegistry.create(F);return new s.SmartContractTransactionsFactory({config:r,abi:t})}getController(){let r=this.config.chainApiUrl||c.Chain.ApiUrl(this.config.env),t=new s.ApiNetworkProvider(r,{timeout:3e4}),e=new s.QueryRunnerAdapter({networkProvider:t}),a=s.AbiRegistry.create(F);return new s.SmartContractQueriesController({queryRunner:e,abi:a})}};var x="xwarp",T=":",N="alias",U=class{constructor(r){this.config=r;this.config=r}async detect(r){let a=new URL(r).searchParams.get(x);if(!a)return{match:!1,warp:null};let n=decodeURIComponent(a),o=n.includes(T)?n:`${N}${T}${n}`,[g,l]=o.split(T),p=new B(this.config),f=new v(this.config),d=null;if(g==="hash")d=await p.createFromTransactionHash(l);else if(g==="alias"){let{warp:q}=await f.getInfoByAlias(l);q&&(d=await p.createFromTransactionHash(q.hash))}return d?{match:!0,warp:d}:{match:!1,warp:null}}build(r,t){let e=this.config.clientUrl||c.DefaultClientUrl(this.config.env);return r===N?`${e}?${x}=${encodeURIComponent(t)}`:`${e}?${x}=${encodeURIComponent(r+T+t)}`}generateQrCode(r,t,e=512,a="white",n="black",o="#23F7DD"){let g=this.build(r,t);return new V.default({type:"svg",width:e,height:e,data:String(g),margin:16,qrOptions:{typeNumber:0,mode:"Byte",errorCorrectionLevel:"Q"},backgroundOptions:{color:a},dotsOptions:{type:"extra-rounded",color:n},cornersSquareOptions:{type:"extra-rounded",color:n},cornersDotOptions:{type:"square",color:n},imageOptions:{hideBackgroundDots:!0,imageSize:.4,margin:8},image:`data:image/svg+xml;utf8,<svg width="16" height="16" viewBox="0 0 100 100" fill="${encodeURIComponent(o)}" xmlns="http://www.w3.org/2000/svg"><path d="M54.8383 50.0242L95 28.8232L88.2456 16L51.4717 30.6974C50.5241 31.0764 49.4759 31.0764 48.5283 30.6974L11.7544 16L5 28.8232L45.1616 50.0242L5 71.2255L11.7544 84.0488L48.5283 69.351C49.4759 68.9724 50.5241 68.9724 51.4717 69.351L88.2456 84.0488L95 71.2255L54.8383 50.0242Z"/></svg>`})}};0&&(module.exports={BrandBuilder,Config,WarpActionExecutor,WarpBuilder,WarpLink,WarpRegistry});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Address as P,ApiNetworkProvider as V,TransactionsFactoryConfig as $,TransferTransactionsFactory as D}from"@multiversx/sdk-core";import _ from"ajv";var o={ProtocolNameWarp:"warp",ProtocolNameBrand:"warp-brand",LatestProtocolVersion:"0.1.0",LatestWarpSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/v0.1.0.schema.json",LatestBrandSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/brand/v0.1.0.schema.json",DefaultClientUrl:a=>a==="devnet"?"https://devnet.xwarp.me/to":a==="testnet"?"###Not implemented###":"https://xwarp.me/to",Chain:{ApiUrl:a=>a==="devnet"?"https://devnet-api.multiversx.com":a==="testnet"?"https://testnet-api.multiversx.com":"https://api.multiversx.com"},Registry:{Contract:a=>a==="devnet"?"erd1qqqqqqqqqqqqqpgqje2f99vr6r7sk54thg03c9suzcvwr4nfl3tsfkdl36":a==="testnet"?"####":"erd1qqqqqqqqqqqqqpgq3mrpj3u6q7tejv6d7eqhnyd27n9v5c5tl3ts08mffe"},AvailableActionInputSources:["field","query"],AvailableActionInputTypes:["string","uint8","uint16","uint32","uint64","biguint","boolean","address"],AvailableActionInputPositions:["value","arg:1","arg:2","arg:3","arg:4","arg:5","arg:6","arg:7","arg:8","arg:9","arg:10"]};var m=a=>a==="devnet"?"D":a==="testnet"?"T":"1",w=a=>`${a}:${o.LatestProtocolVersion}`,W=a=>{var r,t;return{hash:a.hash.toString("hex"),alias:((r=a.alias)==null?void 0:r.toString())||null,trust:a.trust.toString(),creator:a.creator.toString(),createdAt:a.created_at.toNumber(),brand:((t=a.brand)==null?void 0:t.toString())||null}};var R=class{config;pendingBrand={protocol:w(o.ProtocolNameBrand),name:"",description:"",logo:"",website:""};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("BrandBuilder: user address not set");let t=new $({chainID:m(this.config.env)}),e=new D({config:t}),i=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:P.newFromBech32(this.config.userAddress),receiver:P.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(i).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){return await this.createFromRaw(r.data.toString(),t)}async createFromTransactionHash(r){let t=new V(this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env));try{let e=await t.getTransaction(r);return this.createFromTransaction(e)}catch(e){return console.error("BrandBuilder: Error creating from transaction hash",e),null}}setName(r){return this.pendingBrand.name=r,this}setDescription(r){return this.pendingBrand.description=r,this}setLogo(r){return this.pendingBrand.logo=r,this}setWebsite(r){return this.pendingBrand.website=r,this}setColors(r){return this.pendingBrand.colors=r,this}setCta(r){return this.pendingBrand.cta=r,this}async build(){return this.ensure(this.pendingBrand.name,"name is required"),this.ensure(this.pendingBrand.description,"description is required"),this.ensure(this.pendingBrand.logo,"logo is required"),await this.ensureValidSchema(this.pendingBrand),this.pendingBrand}ensure(r,t){if(!r)throw new Error(`Warp: ${t}`)}async ensureValidSchema(r){let t=this.config.brandSchemaUrl||o.LatestBrandSchemaUrl,i=await(await fetch(t)).json(),n=new _,s=n.compile(i);if(!s(r))throw new Error(`BrandBuilder: schema validation failed: ${n.errorsText(s.errors)}`)}};import{Address as b,AddressValue as k,BigUIntValue as O,BooleanValue as Q,BytesValue as F,SmartContractTransactionsFactory as H,TransactionsFactoryConfig as j,U16Value as K,U32Value as G,U64Value as J,U8Value as z}from"@multiversx/sdk-core/out";var x=class{config;url;constructor(r,t){this.config=r,this.url=new URL(t)}createTransactionForExecute(r){if(!this.config.userAddress)throw new Error("WarpActionExecutor: user address not set");let t=new j({chainID:m(this.config.env)}),e=new H({config:t}),i=this.getTypedArgsWithInputs(r),n=this.getPositionValueFromUrl(r,"value"),s=BigInt(n||r.value||0);return e.createTransactionForExecute({sender:b.newFromBech32(this.config.userAddress),contract:b.newFromBech32(r.address),function:r.func||"",gasLimit:BigInt(r.gasLimit),arguments:i,nativeTransferAmount:s})}getPositionValueFromUrl(r,t){var s,p;let e=new URLSearchParams(this.url.search),i=(s=r.inputs)==null?void 0:s.filter(d=>d.source==="query"),n=(p=i==null?void 0:i.find(d=>d.position===t))==null?void 0:p.name;return n?e.get(n):null}getTypedArgsWithInputs(r){return r.args.map(t=>{let[e,i]=t.split(":");return this.toTypedArg(i,e)})}toTypedArg(r,t){if(t==="string")return F.fromUTF8(r);if(t==="uint8")return new z(Number(r));if(t==="uint16")return new K(Number(r));if(t==="uint32")return new G(Number(r));if(t==="uint64")return new J(BigInt(r));if(t==="biguint")return new O(BigInt(r));if(t==="boolean")return new Q(r==="true");if(t==="address")return new k(b.newFromBech32(r));if(t==="hex")return F.fromHex(r);throw new Error(`WarpActionExecutor: Unsupported input type: ${t}`)}};import{Address as U,ApiNetworkProvider as M,TransactionsFactoryConfig as Z,TransferTransactionsFactory as X}from"@multiversx/sdk-core";import Y from"ajv";var y={Warp:a=>`warp:${a}`,WarpInfo:a=>`warp-info:${a}`,Brand:a=>`brand:${a}`},f=class{cache=new Map;set(r,t,e){let i=Date.now()+e*1e3;this.cache.set(r,{value:t,expiresAt:i})}get(r){let t=this.cache.get(r);return t?Date.now()>t.expiresAt?(this.cache.delete(r),null):t.value:null}clear(){this.cache.clear()}};var B=class{config;cache=new f;pendingWarp={protocol:w(o.ProtocolNameWarp),name:"",title:"",description:null,preview:"",actions:[]};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("WarpBuilder: user address not set");let t=new Z({chainID:m(this.config.env)}),e=new X({config:t}),i=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:U.newFromBech32(this.config.userAddress),receiver:U.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(i).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){let e=await this.createFromRaw(r.data.toString(),t);return e.meta={hash:r.hash,creator:r.sender.bech32(),createdAt:new Date(r.timestamp).toISOString()},e}async createFromTransactionHash(r,t){let e=y.Warp(r);if(t){let n=this.cache.get(e);if(n)return console.log(`WarpBuilder (createFromTransactionHash): Warp found in cache: ${r}`),n}let i=new M(this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env));try{let n=await i.getTransaction(r),s=await this.createFromTransaction(n);return t&&t.ttl&&s&&this.cache.set(e,s,t.ttl),s}catch(n){return console.error("WarpBuilder: Error creating from transaction hash",n),null}}setName(r){return this.pendingWarp.name=r,this}setTitle(r){return this.pendingWarp.title=r,this}setDescription(r){return this.pendingWarp.description=r,this}setPreview(r){return this.pendingWarp.preview=r,this}setActions(r){return this.pendingWarp.actions=r,this}addAction(r){return this.pendingWarp.actions.push(r),this}async build(){return this.ensure(this.pendingWarp.protocol,"protocol is required"),this.ensure(this.pendingWarp.name,"name is required"),this.ensure(this.pendingWarp.title,"title is required"),this.ensure(this.pendingWarp.actions.length>0,"actions are required"),await this.ensureValidSchema(this.pendingWarp),this.pendingWarp}ensure(r,t){if(!r)throw new Error(`WarpBuilder: ${t}`)}async ensureValidSchema(r){let t=this.config.warpSchemaUrl||o.LatestWarpSchemaUrl,i=await(await fetch(t)).json(),n=new Y,s=n.compile(i);if(!s(r))throw new Error(`WarpBuilder: schema validation failed: ${n.errorsText(s.errors)}`)}};import ar from"qr-code-styling";import{AbiRegistry as q,Address as g,AddressValue as S,ApiNetworkProvider as E,BytesValue as l,QueryRunnerAdapter as tr,SmartContractQueriesController as er,SmartContractTransactionsFactory as nr,TransactionsFactoryConfig as ir}from"@multiversx/sdk-core/out";var A={buildInfo:{rustc:{version:"1.80.0-nightly",commitHash:"791adf759cc065316f054961875052d5bc03e16c",commitDate:"2024-05-21",channel:"Nightly",short:"rustc 1.80.0-nightly (791adf759 2024-05-21)"},contractCrate:{name:"registry",version:"0.0.1"},framework:{name:"multiversx-sc",version:"0.50.6"}},name:"RegistryContract",constructor:{inputs:[{name:"unit_price",type:"BigUint"}],outputs:[]},upgradeConstructor:{inputs:[],outputs:[]},endpoints:[{name:"registerWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"},{name:"alias_opt",type:"optional<bytes>",multi_arg:!0},{name:"brand_opt",type:"optional<bytes>",multi_arg:!0}],outputs:[],allow_multiple_var_args:!0},{name:"publishWarp",mutability:"mutable",inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"assignAlias",mutability:"mutable",inputs:[{name:"alias",type:"bytes"}],outputs:[]},{name:"getUserWarps",mutability:"readonly",inputs:[{name:"address",type:"Address"}],outputs:[{type:"variadic<WarpInfoView>",multi_result:!0}]},{name:"getInfoByAlias",mutability:"readonly",inputs:[{name:"alias",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"getInfoByHash",mutability:"readonly",inputs:[{name:"hash",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"setUnitPrice",onlyOwner:!0,mutability:"mutable",inputs:[{name:"amount",type:"BigUint"}],outputs:[]},{name:"getConfig",mutability:"readonly",inputs:[],outputs:[{type:"BigUint"}]},{name:"registerBrand",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"brandWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"warp",type:"bytes"},{name:"brand",type:"bytes"}],outputs:[]},{name:"getUserBrands",mutability:"readonly",inputs:[{name:"user",type:"Address"}],outputs:[{type:"variadic<bytes>",multi_result:!0}]}],events:[{identifier:"warpRegistered",inputs:[{name:"hash",type:"bytes",indexed:!0},{name:"alias",type:"bytes",indexed:!0}]}],esdtAttributes:[],hasCallback:!1,types:{WarpInfoView:{type:"struct",fields:[{name:"hash",type:"bytes"},{name:"alias",type:"Option<bytes>"},{name:"trust",type:"bytes"},{name:"creator",type:"Address"},{name:"created_at",type:"u64"}]}}};var v=class{config;unitPrice;cache=new f;constructor(r){this.config=r,this.unitPrice=BigInt(0)}async init(){await this.loadRegistryConfigs()}createRegisterWarpTransaction(r,t){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");let e=t?this.unitPrice*BigInt(2):this.unitPrice;return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"registerWarp",gasLimit:BigInt(1e7),nativeTransferAmount:e,arguments:t?[l.fromHex(r),l.fromUTF8(t)]:[l.fromHex(r)]})}createRegisterBrandTransaction(r){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"registerBrand",gasLimit:BigInt(1e7),nativeTransferAmount:this.unitPrice,arguments:[l.fromHex(r)]})}createAliasAssignTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"assignAlias",gasLimit:BigInt(1e7),arguments:[l.fromHex(r),l.fromUTF8(t)]})}createPublishWarpTransaction(r){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"publishWarp",gasLimit:BigInt(1e7),arguments:[l.fromHex(r)]})}createBrandWarpTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"brandWarp",gasLimit:BigInt(1e7),arguments:[l.fromHex(r),l.fromHex(t)]})}async getInfoByAlias(r,t){let e=y.WarpInfo(r);if(t){let u=this.cache.get(e);if(u)return console.log(`WarpRegistry (getInfoByAlias): WarpInfo found in cache: ${r}`),u}let i=o.Registry.Contract(this.config.env),n=this.getController(),s=n.createQuery({contract:i,function:"getInfoByAlias",arguments:[l.fromUTF8(r)]}),p=await n.runQuery(s),[d]=n.parseQueryResponse(p),c=d?W(d):null,h=c!=null&&c.brand?await this.fetchBrand(c.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:c,brand:h},t.ttl),{warp:c,brand:h}}async getInfoByHash(r,t){let e=y.WarpInfo(r);if(t){let u=this.cache.get(e);if(u)return console.log(`WarpRegistry (getInfoByHash): WarpInfo found in cache: ${r}`),u}let i=o.Registry.Contract(this.config.env),n=this.getController(),s=n.createQuery({contract:i,function:"getInfoByHash",arguments:[l.fromUTF8(r)]}),p=await n.runQuery(s),[d]=n.parseQueryResponse(p),c=d?W(d):null,h=c!=null&&c.brand?await this.fetchBrand(c.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:c,brand:h},t.ttl),{warp:c,brand:h}}async getUserWarpInfos(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=o.Registry.Contract(this.config.env),i=this.getController(),n=i.createQuery({contract:e,function:"getUserWarps",arguments:[new S(new g(t))]}),s=await i.runQuery(n),[p]=i.parseQueryResponse(s);return p.map(W)}async getUserBrands(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=o.Registry.Contract(this.config.env),i=this.getController(),n=i.createQuery({contract:e,function:"getUserBrands",arguments:[new S(new g(t))]}),s=await i.runQuery(n),[p]=i.parseQueryResponse(s),d=p.map(u=>u.toString("hex")),c={ttl:365*24*60*60};return(await Promise.all(d.map(u=>this.fetchBrand(u,c)))).filter(u=>u!==null)}async fetchBrand(r,t){let e=y.Brand(r);if(t){let n=this.cache.get(e);if(n)return console.log(`WarpRegistry (fetchBrand): Brand found in cache: ${r}`),n}let i=new E(this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env));try{let n=await i.getTransaction(r),s=JSON.parse(n.data.toString());return t&&t.ttl&&this.cache.set(e,s,t.ttl),s}catch(n){return console.error("WarpRegistry: Error fetching brand from transaction hash",n),null}}async loadRegistryConfigs(){let r=o.Registry.Contract(this.config.env),t=this.getController(),e=t.createQuery({contract:r,function:"getConfig",arguments:[]}),i=await t.runQuery(e),[n]=t.parseQueryResponse(i),s=BigInt(n.toString());this.unitPrice=s}getFactory(){let r=new ir({chainID:m(this.config.env)}),t=q.create(A);return new nr({config:r,abi:t})}getController(){let r=this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env),t=new E(r,{timeout:3e4}),e=new tr({networkProvider:t}),i=q.create(A);return new er({queryRunner:e,abi:i})}};var T="xwarp",C=":",L="alias",N=class{constructor(r){this.config=r;this.config=r}async detect(r){let i=new URL(r).searchParams.get(T);if(!i)return{match:!1,warp:null};let n=decodeURIComponent(i),s=n.includes(C)?n:`${L}${C}${n}`,[p,d]=s.split(C),c=new B(this.config),h=new v(this.config),u=null;if(p==="hash")u=await c.createFromTransactionHash(d);else if(p==="alias"){let{warp:I}=await h.getInfoByAlias(d);I&&(u=await c.createFromTransactionHash(I.hash))}return u?{match:!0,warp:u}:{match:!1,warp:null}}build(r,t){let e=this.config.clientUrl||o.DefaultClientUrl(this.config.env);return r===L?`${e}?${T}=${encodeURIComponent(t)}`:`${e}?${T}=${encodeURIComponent(r+C+t)}`}generateQrCode(r,t,e=512,i="white",n="black",s="#23F7DD"){let p=this.build(r,t);return new ar({type:"svg",width:e,height:e,data:String(p),margin:16,qrOptions:{typeNumber:0,mode:"Byte",errorCorrectionLevel:"Q"},backgroundOptions:{color:i},dotsOptions:{type:"extra-rounded",color:n},cornersSquareOptions:{type:"extra-rounded",color:n},cornersDotOptions:{type:"square",color:n},imageOptions:{hideBackgroundDots:!0,imageSize:.4,margin:8},image:`data:image/svg+xml;utf8,<svg width="16" height="16" viewBox="0 0 100 100" fill="${encodeURIComponent(s)}" xmlns="http://www.w3.org/2000/svg"><path d="M54.8383 50.0242L95 28.8232L88.2456 16L51.4717 30.6974C50.5241 31.0764 49.4759 31.0764 48.5283 30.6974L11.7544 16L5 28.8232L45.1616 50.0242L5 71.2255L11.7544 84.0488L48.5283 69.351C49.4759 68.9724 50.5241 68.9724 51.4717 69.351L88.2456 84.0488L95 71.2255L54.8383 50.0242Z"/></svg>`})}};export{R as BrandBuilder,o as Config,x as WarpActionExecutor,B as WarpBuilder,N as WarpLink,v as WarpRegistry};
|
|
1
|
+
import{Address as P,ApiNetworkProvider as V,TransactionsFactoryConfig as D,TransferTransactionsFactory as $}from"@multiversx/sdk-core";import O from"ajv";var o={ProtocolNameWarp:"warp",ProtocolNameBrand:"warp-brand",LatestProtocolVersion:"0.1.0",LatestWarpSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/v0.1.0.schema.json",LatestBrandSchemaUrl:"https://raw.githubusercontent.com/vLeapGroup/warps-specs/refs/heads/main/schemas/brand/v0.1.0.schema.json",DefaultClientUrl:i=>i==="devnet"?"https://devnet.xwarp.me/to":i==="testnet"?"###Not implemented###":"https://xwarp.me/to",Chain:{ApiUrl:i=>i==="devnet"?"https://devnet-api.multiversx.com":i==="testnet"?"https://testnet-api.multiversx.com":"https://api.multiversx.com"},Registry:{Contract:i=>i==="devnet"?"erd1qqqqqqqqqqqqqpgqje2f99vr6r7sk54thg03c9suzcvwr4nfl3tsfkdl36":i==="testnet"?"####":"erd1qqqqqqqqqqqqqpgq3mrpj3u6q7tejv6d7eqhnyd27n9v5c5tl3ts08mffe"},AvailableActionInputSources:["field","query"],AvailableActionInputTypes:["string","uint8","uint16","uint32","uint64","biguint","boolean","address"],AvailableActionInputPositions:["value","arg:1","arg:2","arg:3","arg:4","arg:5","arg:6","arg:7","arg:8","arg:9","arg:10"]};var m=i=>i==="devnet"?"D":i==="testnet"?"T":"1",w=i=>`${i}:${o.LatestProtocolVersion}`,W=i=>{var r,t;return{hash:i.hash.toString("hex"),alias:((r=i.alias)==null?void 0:r.toString())||null,trust:i.trust.toString(),creator:i.creator.toString(),createdAt:i.created_at.toNumber(),brand:((t=i.brand)==null?void 0:t.toString())||null}};var R=class{config;pendingBrand={protocol:w(o.ProtocolNameBrand),name:"",description:"",logo:"",website:""};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("BrandBuilder: user address not set");let t=new D({chainID:m(this.config.env)}),e=new $({config:t}),a=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:P.newFromBech32(this.config.userAddress),receiver:P.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(a).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){return await this.createFromRaw(r.data.toString(),t)}async createFromTransactionHash(r){let t=new V(this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env));try{let e=await t.getTransaction(r);return this.createFromTransaction(e)}catch(e){return console.error("BrandBuilder: Error creating from transaction hash",e),null}}setName(r){return this.pendingBrand.name=r,this}setDescription(r){return this.pendingBrand.description=r,this}setLogo(r){return this.pendingBrand.logo=r,this}setWebsite(r){return this.pendingBrand.website=r,this}setColors(r){return this.pendingBrand.colors=r,this}setCta(r){return this.pendingBrand.cta=r,this}async build(){return this.ensure(this.pendingBrand.name,"name is required"),this.ensure(this.pendingBrand.description,"description is required"),this.ensure(this.pendingBrand.logo,"logo is required"),await this.ensureValidSchema(this.pendingBrand),this.pendingBrand}ensure(r,t){if(!r)throw new Error(`Warp: ${t}`)}async ensureValidSchema(r){let t=this.config.brandSchemaUrl||o.LatestBrandSchemaUrl,a=await(await fetch(t)).json(),n=new O,s=n.compile(a);if(!s(r))throw new Error(`BrandBuilder: schema validation failed: ${n.errorsText(s.errors)}`)}};import{Address as b,AddressValue as _,BigUIntValue as k,BooleanValue as Q,BytesValue as F,SmartContractTransactionsFactory as H,TransactionsFactoryConfig as j,U16Value as K,U32Value as G,U64Value as J,U8Value as z}from"@multiversx/sdk-core/out";var x=class{config;url;constructor(r,t){this.config=r,this.url=new URL(t)}createTransactionForExecute(r){if(!this.config.userAddress)throw new Error("WarpActionExecutor: user address not set");let t=new j({chainID:m(this.config.env)}),e=new H({config:t}),a=this.getTypedArgsWithInputs(r),n=this.getPositionValueFromUrl(r,"value"),s=BigInt(n||r.value||0);return e.createTransactionForExecute({sender:b.newFromBech32(this.config.userAddress),contract:b.newFromBech32(r.address),function:r.func||"",gasLimit:BigInt(r.gasLimit),arguments:a,nativeTransferAmount:s})}getPositionValueFromUrl(r,t){var s,p;let e=new URLSearchParams(this.url.search),a=(s=r.inputs)==null?void 0:s.filter(d=>d.source==="query"),n=(p=a==null?void 0:a.find(d=>d.position===t))==null?void 0:p.name;return n?e.get(n):null}getTypedArgsWithInputs(r){return r.args.map(t=>{let[e,a]=t.split(":");return this.toTypedArg(a,e)})}toTypedArg(r,t){if(t==="string")return F.fromUTF8(r);if(t==="uint8")return new z(Number(r));if(t==="uint16")return new K(Number(r));if(t==="uint32")return new G(Number(r));if(t==="uint64")return new J(BigInt(r));if(t==="biguint")return new k(BigInt(r));if(t==="boolean")return new Q(r==="true");if(t==="address")return new _(b.newFromBech32(r));if(t==="hex")return F.fromHex(r);throw new Error(`WarpActionExecutor: Unsupported input type: ${t}`)}};import{Address as U,ApiNetworkProvider as M,TransactionsFactoryConfig as Z,TransferTransactionsFactory as X}from"@multiversx/sdk-core";import Y from"ajv";var y={Warp:i=>`warp:${i}`,WarpInfo:i=>`warp-info:${i}`,Brand:i=>`brand:${i}`},f=class{cache=new Map;set(r,t,e){let a=Date.now()+e*1e3;this.cache.set(r,{value:t,expiresAt:a})}get(r){let t=this.cache.get(r);return t?Date.now()>t.expiresAt?(this.cache.delete(r),null):t.value:null}clear(){this.cache.clear()}};var B=class{config;cache=new f;pendingWarp={protocol:w(o.ProtocolNameWarp),name:"",title:"",description:null,preview:"",actions:[]};constructor(r){this.config=r}createInscriptionTransaction(r){if(!this.config.userAddress)throw new Error("WarpBuilder: user address not set");let t=new Z({chainID:m(this.config.env)}),e=new X({config:t}),a=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:U.newFromBech32(this.config.userAddress),receiver:U.newFromBech32(this.config.userAddress),nativeAmount:BigInt(0),data:Buffer.from(a).valueOf()})}async createFromRaw(r,t=!0){let e=JSON.parse(r);return t&&await this.ensureValidSchema(e),e}async createFromTransaction(r,t=!1){let e=await this.createFromRaw(r.data.toString(),t);return e.meta={hash:r.hash,creator:r.sender.bech32(),createdAt:new Date(r.timestamp).toISOString()},e}async createFromTransactionHash(r,t){let e=y.Warp(r);if(t){let n=this.cache.get(e);if(n)return console.log(`WarpBuilder (createFromTransactionHash): Warp found in cache: ${r}`),n}let a=new M(this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env));try{let n=await a.getTransaction(r),s=await this.createFromTransaction(n);return t&&t.ttl&&s&&this.cache.set(e,s,t.ttl),s}catch(n){return console.error("WarpBuilder: Error creating from transaction hash",n),null}}setName(r){return this.pendingWarp.name=r,this}setTitle(r){return this.pendingWarp.title=r,this}setDescription(r){return this.pendingWarp.description=r,this}setPreview(r){return this.pendingWarp.preview=r,this}setActions(r){return this.pendingWarp.actions=r,this}addAction(r){return this.pendingWarp.actions.push(r),this}async build(){return this.ensure(this.pendingWarp.protocol,"protocol is required"),this.ensure(this.pendingWarp.name,"name is required"),this.ensure(this.pendingWarp.title,"title is required"),this.ensure(this.pendingWarp.actions.length>0,"actions are required"),await this.ensureValidSchema(this.pendingWarp),this.pendingWarp}ensure(r,t){if(!r)throw new Error(`WarpBuilder: ${t}`)}async ensureValidSchema(r){let t=this.config.warpSchemaUrl||o.LatestWarpSchemaUrl,a=await(await fetch(t)).json(),n=new Y,s=n.compile(a);if(!s(r))throw new Error(`WarpBuilder: schema validation failed: ${n.errorsText(s.errors)}`)}};import ir from"qr-code-styling";import{AbiRegistry as q,Address as g,AddressValue as S,ApiNetworkProvider as E,BytesValue as l,QueryRunnerAdapter as tr,SmartContractQueriesController as er,SmartContractTransactionsFactory as nr,TransactionsFactoryConfig as ar}from"@multiversx/sdk-core/out";var A={buildInfo:{rustc:{version:"1.80.0-nightly",commitHash:"791adf759cc065316f054961875052d5bc03e16c",commitDate:"2024-05-21",channel:"Nightly",short:"rustc 1.80.0-nightly (791adf759 2024-05-21)"},contractCrate:{name:"registry",version:"0.0.1"},framework:{name:"multiversx-sc",version:"0.50.6"}},name:"RegistryContract",constructor:{inputs:[{name:"unit_price",type:"BigUint"}],outputs:[]},upgradeConstructor:{inputs:[],outputs:[]},endpoints:[{name:"registerWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"},{name:"alias_opt",type:"optional<bytes>",multi_arg:!0},{name:"brand_opt",type:"optional<bytes>",multi_arg:!0}],outputs:[],allow_multiple_var_args:!0},{name:"publishWarp",mutability:"mutable",inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"assignAlias",mutability:"mutable",inputs:[{name:"alias",type:"bytes"}],outputs:[]},{name:"getUserWarps",mutability:"readonly",inputs:[{name:"address",type:"Address"}],outputs:[{type:"variadic<WarpInfoView>",multi_result:!0}]},{name:"getInfoByAlias",mutability:"readonly",inputs:[{name:"alias",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"getInfoByHash",mutability:"readonly",inputs:[{name:"hash",type:"bytes"}],outputs:[{type:"WarpInfoView"}]},{name:"setUnitPrice",onlyOwner:!0,mutability:"mutable",inputs:[{name:"amount",type:"BigUint"}],outputs:[]},{name:"getConfig",mutability:"readonly",inputs:[],outputs:[{type:"BigUint"}]},{name:"registerBrand",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"hash",type:"bytes"}],outputs:[]},{name:"brandWarp",mutability:"mutable",payableInTokens:["EGLD"],inputs:[{name:"warp",type:"bytes"},{name:"brand",type:"bytes"}],outputs:[]},{name:"getUserBrands",mutability:"readonly",inputs:[{name:"user",type:"Address"}],outputs:[{type:"variadic<bytes>",multi_result:!0}]}],events:[{identifier:"warpRegistered",inputs:[{name:"hash",type:"bytes",indexed:!0},{name:"alias",type:"bytes",indexed:!0}]}],esdtAttributes:[],hasCallback:!1,types:{WarpInfoView:{type:"struct",fields:[{name:"hash",type:"bytes"},{name:"alias",type:"Option<bytes>"},{name:"trust",type:"bytes"},{name:"creator",type:"Address"},{name:"created_at",type:"u64"}]}}};var v=class{config;unitPrice;cache=new f;constructor(r){this.config=r,this.unitPrice=BigInt(0)}async init(){await this.loadRegistryConfigs()}createRegisterWarpTransaction(r,t){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");let e=t?this.unitPrice*BigInt(2):this.unitPrice;return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"registerWarp",gasLimit:BigInt(1e7),nativeTransferAmount:e,arguments:t?[l.fromHex(r),l.fromUTF8(t)]:[l.fromHex(r)]})}createRegisterBrandTransaction(r){if(this.unitPrice===BigInt(0))throw new Error("WarpRegistry: config not loaded. forgot to call init()?");if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"registerBrand",gasLimit:BigInt(1e7),nativeTransferAmount:this.unitPrice,arguments:[l.fromHex(r)]})}createAliasAssignTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"assignAlias",gasLimit:BigInt(1e7),arguments:[l.fromHex(r),l.fromUTF8(t)]})}createPublishWarpTransaction(r){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"publishWarp",gasLimit:BigInt(1e7),arguments:[l.fromHex(r)]})}createBrandWarpTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:g.newFromBech32(this.config.userAddress),contract:g.newFromBech32(o.Registry.Contract(this.config.env)),function:"brandWarp",gasLimit:BigInt(1e7),nativeTransferAmount:this.unitPrice,arguments:[l.fromHex(r),l.fromHex(t)]})}async getInfoByAlias(r,t){let e=y.WarpInfo(r);if(t){let u=this.cache.get(e);if(u)return console.log(`WarpRegistry (getInfoByAlias): WarpInfo found in cache: ${r}`),u}let a=o.Registry.Contract(this.config.env),n=this.getController(),s=n.createQuery({contract:a,function:"getInfoByAlias",arguments:[l.fromUTF8(r)]}),p=await n.runQuery(s),[d]=n.parseQueryResponse(p),c=d?W(d):null,h=c!=null&&c.brand?await this.fetchBrand(c.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:c,brand:h},t.ttl),{warp:c,brand:h}}async getInfoByHash(r,t){let e=y.WarpInfo(r);if(t){let u=this.cache.get(e);if(u)return console.log(`WarpRegistry (getInfoByHash): WarpInfo found in cache: ${r}`),u}let a=o.Registry.Contract(this.config.env),n=this.getController(),s=n.createQuery({contract:a,function:"getInfoByHash",arguments:[l.fromUTF8(r)]}),p=await n.runQuery(s),[d]=n.parseQueryResponse(p),c=d?W(d):null,h=c!=null&&c.brand?await this.fetchBrand(c.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:c,brand:h},t.ttl),{warp:c,brand:h}}async getUserWarpInfos(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=o.Registry.Contract(this.config.env),a=this.getController(),n=a.createQuery({contract:e,function:"getUserWarps",arguments:[new S(new g(t))]}),s=await a.runQuery(n),[p]=a.parseQueryResponse(s);return p.map(W)}async getUserBrands(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=o.Registry.Contract(this.config.env),a=this.getController(),n=a.createQuery({contract:e,function:"getUserBrands",arguments:[new S(new g(t))]}),s=await a.runQuery(n),[p]=a.parseQueryResponse(s),d=p.map(u=>u.toString("hex")),c={ttl:365*24*60*60};return(await Promise.all(d.map(u=>this.fetchBrand(u,c)))).filter(u=>u!==null)}async fetchBrand(r,t){let e=y.Brand(r);if(t){let n=this.cache.get(e);if(n)return console.log(`WarpRegistry (fetchBrand): Brand found in cache: ${r}`),n}let a=new E(this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env));try{let n=await a.getTransaction(r),s=JSON.parse(n.data.toString());return s.meta={hash:n.hash,creator:n.sender.bech32(),createdAt:new Date(n.timestamp).toISOString()},t&&t.ttl&&this.cache.set(e,s,t.ttl),s}catch(n){return console.error("WarpRegistry: Error fetching brand from transaction hash",n),null}}async loadRegistryConfigs(){let r=o.Registry.Contract(this.config.env),t=this.getController(),e=t.createQuery({contract:r,function:"getConfig",arguments:[]}),a=await t.runQuery(e),[n]=t.parseQueryResponse(a),s=BigInt(n.toString());this.unitPrice=s}getFactory(){let r=new ar({chainID:m(this.config.env)}),t=q.create(A);return new nr({config:r,abi:t})}getController(){let r=this.config.chainApiUrl||o.Chain.ApiUrl(this.config.env),t=new E(r,{timeout:3e4}),e=new tr({networkProvider:t}),a=q.create(A);return new er({queryRunner:e,abi:a})}};var T="xwarp",C=":",L="alias",N=class{constructor(r){this.config=r;this.config=r}async detect(r){let a=new URL(r).searchParams.get(T);if(!a)return{match:!1,warp:null};let n=decodeURIComponent(a),s=n.includes(C)?n:`${L}${C}${n}`,[p,d]=s.split(C),c=new B(this.config),h=new v(this.config),u=null;if(p==="hash")u=await c.createFromTransactionHash(d);else if(p==="alias"){let{warp:I}=await h.getInfoByAlias(d);I&&(u=await c.createFromTransactionHash(I.hash))}return u?{match:!0,warp:u}:{match:!1,warp:null}}build(r,t){let e=this.config.clientUrl||o.DefaultClientUrl(this.config.env);return r===L?`${e}?${T}=${encodeURIComponent(t)}`:`${e}?${T}=${encodeURIComponent(r+C+t)}`}generateQrCode(r,t,e=512,a="white",n="black",s="#23F7DD"){let p=this.build(r,t);return new ir({type:"svg",width:e,height:e,data:String(p),margin:16,qrOptions:{typeNumber:0,mode:"Byte",errorCorrectionLevel:"Q"},backgroundOptions:{color:a},dotsOptions:{type:"extra-rounded",color:n},cornersSquareOptions:{type:"extra-rounded",color:n},cornersDotOptions:{type:"square",color:n},imageOptions:{hideBackgroundDots:!0,imageSize:.4,margin:8},image:`data:image/svg+xml;utf8,<svg width="16" height="16" viewBox="0 0 100 100" fill="${encodeURIComponent(s)}" xmlns="http://www.w3.org/2000/svg"><path d="M54.8383 50.0242L95 28.8232L88.2456 16L51.4717 30.6974C50.5241 31.0764 49.4759 31.0764 48.5283 30.6974L11.7544 16L5 28.8232L45.1616 50.0242L5 71.2255L11.7544 84.0488L48.5283 69.351C49.4759 68.9724 50.5241 68.9724 51.4717 69.351L88.2456 84.0488L95 71.2255L54.8383 50.0242Z"/></svg>`})}};export{R as BrandBuilder,o as Config,x as WarpActionExecutor,B as WarpBuilder,N as WarpLink,v as WarpRegistry};
|