@vleap/warps 0.0.50 → 0.0.52
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 +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -156,12 +156,13 @@ declare class WarpActionExecutor {
|
|
|
156
156
|
|
|
157
157
|
declare class WarpBuilder {
|
|
158
158
|
private config;
|
|
159
|
+
private cache;
|
|
159
160
|
private pendingWarp;
|
|
160
161
|
constructor(config: WarpConfig);
|
|
161
162
|
createInscriptionTransaction(warp: Warp): Transaction;
|
|
162
163
|
createFromRaw(encoded: string, validateSchema?: boolean): Promise<Warp>;
|
|
163
164
|
createFromTransaction(tx: TransactionOnNetwork, validateSchema?: boolean): Promise<Warp>;
|
|
164
|
-
createFromTransactionHash(hash: string): Promise<Warp | null>;
|
|
165
|
+
createFromTransactionHash(hash: string, cache?: WarpCacheConfig): Promise<Warp | null>;
|
|
165
166
|
setName(name: string): WarpBuilder;
|
|
166
167
|
setTitle(title: string): WarpBuilder;
|
|
167
168
|
setDescription(description: string): WarpBuilder;
|
|
@@ -204,6 +205,7 @@ declare class WarpRegistry {
|
|
|
204
205
|
brand: Brand | null;
|
|
205
206
|
}>;
|
|
206
207
|
getUserWarpInfos(user?: string): Promise<WarpInfo[]>;
|
|
208
|
+
getUserBrands(user?: string): Promise<Brand[]>;
|
|
207
209
|
fetchBrand(hash: string, cache?: WarpCacheConfig): Promise<Brand | null>;
|
|
208
210
|
private loadRegistryConfigs;
|
|
209
211
|
private getFactory;
|
package/dist/index.d.ts
CHANGED
|
@@ -156,12 +156,13 @@ declare class WarpActionExecutor {
|
|
|
156
156
|
|
|
157
157
|
declare class WarpBuilder {
|
|
158
158
|
private config;
|
|
159
|
+
private cache;
|
|
159
160
|
private pendingWarp;
|
|
160
161
|
constructor(config: WarpConfig);
|
|
161
162
|
createInscriptionTransaction(warp: Warp): Transaction;
|
|
162
163
|
createFromRaw(encoded: string, validateSchema?: boolean): Promise<Warp>;
|
|
163
164
|
createFromTransaction(tx: TransactionOnNetwork, validateSchema?: boolean): Promise<Warp>;
|
|
164
|
-
createFromTransactionHash(hash: string): Promise<Warp | null>;
|
|
165
|
+
createFromTransactionHash(hash: string, cache?: WarpCacheConfig): Promise<Warp | null>;
|
|
165
166
|
setName(name: string): WarpBuilder;
|
|
166
167
|
setTitle(title: string): WarpBuilder;
|
|
167
168
|
setDescription(description: string): WarpBuilder;
|
|
@@ -204,6 +205,7 @@ declare class WarpRegistry {
|
|
|
204
205
|
brand: Brand | null;
|
|
205
206
|
}>;
|
|
206
207
|
getUserWarpInfos(user?: string): Promise<WarpInfo[]>;
|
|
208
|
+
getUserBrands(user?: string): Promise<Brand[]>;
|
|
207
209
|
fetchBrand(hash: string, cache?: WarpCacheConfig): Promise<Brand | null>;
|
|
208
210
|
private loadRegistryConfigs;
|
|
209
211
|
private getFactory;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var D=Object.create;var W=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var $=(n,r)=>{for(var t in r)W(n,t,{get:r[t],enumerable:!0})},S=(n,r,t,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of k(r))!Q.call(n,i)&&i!==t&&W(n,i,{get:()=>r[i],enumerable:!(e=O(r,i))||e.enumerable});return n};var I=(n,r,t)=>(t=n!=null?D(_(n)):{},S(r||!n||!n.__esModule?W(t,"default",{value:n,enumerable:!0}):t,n)),j=n=>S(W({},"__esModule",{value:!0}),n);var J={};$(J,{BrandBuilder:()=>P,Config:()=>u,WarpActionExecutor:()=>F,WarpBuilder:()=>w,WarpLink:()=>x,WarpRegistry:()=>v});module.exports=j(J);var h=require("@multiversx/sdk-core"),L=I(require("ajv"));var u={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:n=>n==="devnet"?"https://devnet.xwarp.me/to":n==="testnet"?"###Not implemented###":"https://xwarp.me/to",Chain:{ApiUrl:n=>n==="devnet"?"https://devnet-api.multiversx.com":n==="testnet"?"https://testnet-api.multiversx.com":"https://api.multiversx.com"},Registry:{Contract:n=>n==="devnet"?"erd1qqqqqqqqqqqqqpgqje2f99vr6r7sk54thg03c9suzcvwr4nfl3tsfkdl36":n==="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 f=n=>n==="devnet"?"D":n==="testnet"?"T":"1",B=n=>`${n}:${u.LatestProtocolVersion}`,C=n=>{var r,t;return{hash:n.hash.toString("hex"),alias:((r=n.alias)==null?void 0:r.toString())||null,trust:n.trust.toString(),creator:n.creator.toString(),createdAt:n.created_at.toNumber(),brand:((t=n.brand)==null?void 0:t.toString())||null}};var P=class{config;pendingBrand={protocol:B(u.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:f(this.config.env)}),e=new h.TransferTransactionsFactory({config:t}),i=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(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 h.ApiNetworkProvider(this.config.chainApiUrl||u.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||u.LatestBrandSchemaUrl,i=await(await fetch(t)).json(),a=new L.default,o=a.compile(i);if(!o(r))throw new Error(`BrandBuilder: schema validation failed: ${a.errorsText(o.errors)}`)}};var c=require("@multiversx/sdk-core/out");var F=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 c.TransactionsFactoryConfig({chainID:f(this.config.env)}),e=new c.SmartContractTransactionsFactory({config:t}),i=this.getTypedArgsWithInputs(r),a=this.getPositionValueFromUrl(r,"value"),o=BigInt(a||r.value||0);return e.createTransactionForExecute({sender:c.Address.newFromBech32(this.config.userAddress),contract:c.Address.newFromBech32(r.address),function:r.func||"",gasLimit:BigInt(r.gasLimit),arguments:i,nativeTransferAmount:o})}getPositionValueFromUrl(r,t){var o,d;let e=new URLSearchParams(this.url.search),i=(o=r.inputs)==null?void 0:o.filter(l=>l.source==="query"),a=(d=i==null?void 0:i.find(l=>l.position===t))==null?void 0:d.name;return a?e.get(a):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 c.BytesValue.fromUTF8(r);if(t==="uint8")return new c.U8Value(Number(r));if(t==="uint16")return new c.U16Value(Number(r));if(t==="uint32")return new c.U32Value(Number(r));if(t==="uint64")return new c.U64Value(BigInt(r));if(t==="biguint")return new c.BigUIntValue(BigInt(r));if(t==="boolean")return new c.BooleanValue(r==="true");if(t==="address")return new c.AddressValue(c.Address.newFromBech32(r));if(t==="hex")return c.BytesValue.fromHex(r);throw new Error(`WarpActionExecutor: Unsupported input type: ${t}`)}};var m=require("@multiversx/sdk-core"),E=I(require("ajv"));var w=class{config;pendingWarp={protocol:B(u.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:f(this.config.env)}),e=new m.TransferTransactionsFactory({config:t}),i=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(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){let t=new m.ApiNetworkProvider(this.config.chainApiUrl||u.Chain.ApiUrl(this.config.env));try{let e=await t.getTransaction(r);return this.createFromTransaction(e)}catch(e){return console.error("WarpBuilder: Error creating from transaction hash",e),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||u.LatestWarpSchemaUrl,i=await(await fetch(t)).json(),a=new E.default,o=a.compile(i);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 R={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:"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 A={WarpInfo:n=>`warp-info:${n}`,Brand:n=>`brand:${n}`},b=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 v=class{config;unitPrice;cache=new b;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(u.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(u.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(u.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(u.Registry.Contract(this.config.env)),function:"publishWarp",gasLimit:BigInt(1e7),arguments:[s.BytesValue.fromHex(r)]})}async getInfoByAlias(r,t){let e=A.WarpInfo(r);if(t){let g=this.cache.get(e);if(g)return g}let i=u.Registry.Contract(this.config.env),a=this.getController(),o=a.createQuery({contract:i,function:"getInfoByAlias",arguments:[s.BytesValue.fromUTF8(r)]}),d=await a.runQuery(o),[l]=a.parseQueryResponse(d),p=l?C(l):null,y=p!=null&&p.brand?await this.fetchBrand(p.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:p,brand:y},t.ttl),{warp:p,brand:y}}async getInfoByHash(r,t){let e=A.WarpInfo(r);if(t){let g=this.cache.get(e);if(g)return g}let i=u.Registry.Contract(this.config.env),a=this.getController(),o=a.createQuery({contract:i,function:"getInfoByHash",arguments:[s.BytesValue.fromUTF8(r)]}),d=await a.runQuery(o),[l]=a.parseQueryResponse(d),p=l?C(l):null,y=p!=null&&p.brand?await this.fetchBrand(p.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:p,brand:y},t.ttl),{warp:p,brand:y}}async getUserWarpInfos(r){let t=r||this.config.userAddress;if(!t)throw new Error("WarpRegistry: user address not set");let e=u.Registry.Contract(this.config.env),i=this.getController(),a=i.createQuery({contract:e,function:"getUserWarps",arguments:[new s.AddressValue(new s.Address(t))]}),o=await i.runQuery(a);return i.parseQueryResponse(o).map(C)}async fetchBrand(r,t){let e=A.Brand(r);if(t){let a=this.cache.get(e);if(a)return a}let i=new s.ApiNetworkProvider(this.config.chainApiUrl||u.Chain.ApiUrl(this.config.env));try{let a=await i.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=u.Registry.Contract(this.config.env),t=this.getController(),e=t.createQuery({contract:r,function:"getConfig",arguments:[]}),i=await t.runQuery(e),[a]=t.parseQueryResponse(i),o=BigInt(a.toString());this.unitPrice=o}getFactory(){let r=new s.TransactionsFactoryConfig({chainID:f(this.config.env)}),t=s.AbiRegistry.create(R);return new s.SmartContractTransactionsFactory({config:r,abi:t})}getController(){let r=this.config.chainApiUrl||u.Chain.ApiUrl(this.config.env),t=new s.ApiNetworkProvider(r,{timeout:3e4}),e=new s.QueryRunnerAdapter({networkProvider:t}),i=s.AbiRegistry.create(R);return new s.SmartContractQueriesController({queryRunner:e,abi:i})}};var U="xwarp",T=":",N="alias",x=class{constructor(r){this.config=r;this.config=r}async detect(r){let i=new URL(r).searchParams.get(U);if(!i)return{match:!1,warp:null};let a=decodeURIComponent(i),o=a.includes(T)?a:`${N}${T}${a}`,[d,l]=o.split(T),p=new w(this.config),y=new v(this.config),g=null;if(d==="hash")g=await p.createFromTransactionHash(l);else if(d==="alias"){let{warp:q}=await y.getInfoByAlias(l);q&&(g=await p.createFromTransactionHash(q.hash))}return g?{match:!0,warp:g}:{match:!1,warp:null}}build(r,t){let e=this.config.clientUrl||u.DefaultClientUrl(this.config.env);return r===N?`${e}?${U}=${encodeURIComponent(t)}`:`${e}?${U}=${encodeURIComponent(r+T+t)}`}generateQrCode(r,t,e=512,i="white",a="black",o="#23F7DD"){let d=this.build(r,t);return new V.default({type:"svg",width:e,height:e,data:String(d),margin:16,qrOptions:{typeNumber:0,mode:"Byte",errorCorrectionLevel:"Q"},backgroundOptions:{color:i},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 $=Object.create;var C=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var Q=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty;var _=(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 O(r))!k.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?$(Q(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={};_(K,{BrandBuilder:()=>P,Config:()=>c,WarpActionExecutor:()=>R,WarpBuilder:()=>B,WarpLink:()=>x,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:"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)]})}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 U="xwarp",T=":",N="alias",x=class{constructor(r){this.config=r;this.config=r}async detect(r){let n=new URL(r).searchParams.get(U);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}?${U}=${encodeURIComponent(t)}`:`${e}?${U}=${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});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Address as P,ApiNetworkProvider as N,TransactionsFactoryConfig as V,TransferTransactionsFactory as D}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: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 h=a=>a==="devnet"?"D":a==="testnet"?"T":"1",f=a=>`${a}:${o.LatestProtocolVersion}`,y=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 F=class{config;pendingBrand={protocol:f(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 V({chainID:h(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 N(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 O,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 _,BooleanValue as Q,BytesValue as R,SmartContractTransactionsFactory as $,TransactionsFactoryConfig as j,U16Value as H,U32Value as J,U64Value as K,U8Value as z}from"@multiversx/sdk-core/out";var U=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:h(this.config.env)}),e=new $({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,u;let e=new URLSearchParams(this.url.search),i=(s=r.inputs)==null?void 0:s.filter(p=>p.source==="query"),n=(u=i==null?void 0:i.find(p=>p.position===t))==null?void 0:u.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 R.fromUTF8(r);if(t==="uint8")return new z(Number(r));if(t==="uint16")return new H(Number(r));if(t==="uint32")return new J(Number(r));if(t==="uint64")return new K(BigInt(r));if(t==="biguint")return new _(BigInt(r));if(t==="boolean")return new Q(r==="true");if(t==="address")return new k(b.newFromBech32(r));if(t==="hex")return R.fromHex(r);throw new Error(`WarpActionExecutor: Unsupported input type: ${t}`)}};import{Address as x,ApiNetworkProvider as G,TransactionsFactoryConfig as M,TransferTransactionsFactory as Z}from"@multiversx/sdk-core";import X from"ajv";var w=class{config;pendingWarp={protocol:f(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 M({chainID:h(this.config.env)}),e=new Z({config:t}),i=JSON.stringify(r);return e.createTransactionForNativeTokenTransfer({sender:x.newFromBech32(this.config.userAddress),receiver:x.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){let t=new G(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("WarpBuilder: Error creating from transaction hash",e),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 X,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 l,AddressValue as rr,ApiNetworkProvider as S,BytesValue as g,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:"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 W={WarpInfo:a=>`warp-info:${a}`,Brand:a=>`brand:${a}`},v=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;unitPrice;cache=new v;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:l.newFromBech32(this.config.userAddress),contract:l.newFromBech32(o.Registry.Contract(this.config.env)),function:"registerWarp",gasLimit:BigInt(1e7),nativeTransferAmount:e,arguments:t?[g.fromHex(r),g.fromUTF8(t)]:[g.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:l.newFromBech32(this.config.userAddress),contract:l.newFromBech32(o.Registry.Contract(this.config.env)),function:"registerBrand",gasLimit:BigInt(1e7),nativeTransferAmount:this.unitPrice,arguments:[g.fromHex(r)]})}createAliasAssignTransaction(r,t){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:l.newFromBech32(this.config.userAddress),contract:l.newFromBech32(o.Registry.Contract(this.config.env)),function:"assignAlias",gasLimit:BigInt(1e7),arguments:[g.fromHex(r),g.fromUTF8(t)]})}createPublishWarpTransaction(r){if(!this.config.userAddress)throw new Error("WarpRegistry: user address not set");return this.getFactory().createTransactionForExecute({sender:l.newFromBech32(this.config.userAddress),contract:l.newFromBech32(o.Registry.Contract(this.config.env)),function:"publishWarp",gasLimit:BigInt(1e7),arguments:[g.fromHex(r)]})}async getInfoByAlias(r,t){let e=W.WarpInfo(r);if(t){let d=this.cache.get(e);if(d)return d}let i=o.Registry.Contract(this.config.env),n=this.getController(),s=n.createQuery({contract:i,function:"getInfoByAlias",arguments:[g.fromUTF8(r)]}),u=await n.runQuery(s),[p]=n.parseQueryResponse(u),c=p?y(p):null,m=c!=null&&c.brand?await this.fetchBrand(c.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:c,brand:m},t.ttl),{warp:c,brand:m}}async getInfoByHash(r,t){let e=W.WarpInfo(r);if(t){let d=this.cache.get(e);if(d)return d}let i=o.Registry.Contract(this.config.env),n=this.getController(),s=n.createQuery({contract:i,function:"getInfoByHash",arguments:[g.fromUTF8(r)]}),u=await n.runQuery(s),[p]=n.parseQueryResponse(u),c=p?y(p):null,m=c!=null&&c.brand?await this.fetchBrand(c.brand):null;return t&&t.ttl&&this.cache.set(e,{warp:c,brand:m},t.ttl),{warp:c,brand:m}}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 rr(new l(t))]}),s=await i.runQuery(n);return i.parseQueryResponse(s).map(y)}async fetchBrand(r,t){let e=W.Brand(r);if(t){let n=this.cache.get(e);if(n)return n}let i=new S(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:h(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 S(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",E=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}`,[u,p]=s.split(C),c=new w(this.config),m=new B(this.config),d=null;if(u==="hash")d=await c.createFromTransactionHash(p);else if(u==="alias"){let{warp:I}=await m.getInfoByAlias(p);I&&(d=await c.createFromTransactionHash(I.hash))}return d?{match:!0,warp:d}:{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 u=this.build(r,t);return new ar({type:"svg",width:e,height:e,data:String(u),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{F as BrandBuilder,o as Config,U as WarpActionExecutor,w as WarpBuilder,E as WarpLink,B as WarpRegistry};
|
|
1
|
+
import{Address as P,ApiNetworkProvider as V,TransactionsFactoryConfig as $,TransferTransactionsFactory as D}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: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 O,s=n.compile(i);if(!s(r))throw new Error(`BrandBuilder: schema validation failed: ${n.errorsText(s.errors)}`)}};import{Address as b,AddressValue as Q,BigUIntValue as k,BooleanValue as _,BytesValue as F,SmartContractTransactionsFactory as H,TransactionsFactoryConfig as j,U16Value as K,U32Value as J,U64Value as z,U8Value as G}from"@multiversx/sdk-core/out";var U=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 G(Number(r));if(t==="uint16")return new K(Number(r));if(t==="uint32")return new J(Number(r));if(t==="uint64")return new z(BigInt(r));if(t==="biguint")return new k(BigInt(r));if(t==="boolean")return new _(r==="true");if(t==="address")return new Q(b.newFromBech32(r));if(t==="hex")return F.fromHex(r);throw new Error(`WarpActionExecutor: Unsupported input type: ${t}`)}};import{Address as x,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:x.newFromBech32(this.config.userAddress),receiver:x.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:"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)]})}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,U as WarpActionExecutor,B as WarpBuilder,N as WarpLink,v as WarpRegistry};
|