UNPKG

ae-channel-manager

Version:

A javascript library for managing AEternity payment channels for Pay-per-API call protocol

1 lines 7.13 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["ae-channel-manager"]=t():e["ae-channel-manager"]=t()}(global,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=2)}([function(e,t,n){const{Node:r,MemoryAccount:i,Universal:a,Crypto:s,ChainNode:o,AmountFormatter:c}=n(1),{format:l,toAettos:u,AE_AMOUNT_FORMATS:d,toAe:h,formatAmount:p}=(n(4),c);e.exports={Account:function(e={},t){this.keyPair=e,e||(this.keyPair=this.create()),this.network=t,this.wallet=()=>new Promise(async(e,t)=>{const{url:n,internalUrl:s,networkId:o}=this.network;n||t("Netowrk url is not defined"),s||t("Netowrk internalUrl is not defined"),o||t("Netowrk networkId is not defined");const{secretKey:c,publicKey:l}=this.keyPair;c||t("publicKeyl is not defined"),l||t("publicKey is not defined");const u=await r({url:n,internalUrl:s}),d=i({keypair:{secretKey:c,publicKey:l}}),h=[{name:o,instance:u}];e(await a({nodes:h,accounts:[d]}))}),this.sign=async e=>{console.log("Receieved transaction for signing: ",e);const t=await this.wallet();return await t.signTransaction(e)},this.fund=async e=>{const t=await this.wallet();return await t.spend(5,e,{denomination:d.AE}),Promise.resolve(await this.balance(e))},this.balance=async e=>{const t=await r({url:this.network.url,internalUrl:this.network.internalUrl}),n=await o({nodes:[{name:"test",instance:t}]});return Promise.resolve(h(await n.balance(e)))},this.toAettos=u,this.toAe=h,this.create=()=>s.generateKeyPair(),this.format=l}}},function(e,t){e.exports=require("imaginea-aepp-sdk")},function(e,t,n){e.exports=n(3)},function(e,t,n){const{Account:r}=n(0),{ChannelManager:i}=n(5);e.exports={Account:r,ChannelManager:i}},function(e,t){e.exports=require("node-fetch")},function(e,t,n){const{Channel:r,TxBuilder:i}=n(1),{Account:a}=n(0);e.exports={ChannelManager:function(e){if(!e)throw new Error("please pass network and keypair in constructor");this.network=e;const t=e=>new a(e,this.network).wallet(),n=e=>(n,r)=>t(e).then(e=>e.signTransaction(r)),s={},o=e=>t=>new Promise((n,r)=>{e.on("statusChanged",r=>{console.log(`current status: '${r}' waiting for: '${t}'`),r===t&&n(e)}),e.on("error",r)}),c=async e=>({channelId:e.id(),round:e.round(),status:e.status(),state:await e.state(),fsmId:e.fsmId()}),l=(e,t)=>s.hasOwnProperty(e)?null==t?s[e][Object.keys(s[e])[0]]:s[e][t]:Promise.reject("no channel found with id: "+e);this.Role=Object.freeze({INITIATOR:"initiator",RESPONDER:"responder"}),this.balance=async e=>{const{channel:t,params:n}=l(e),r=await t.balances([n.initiatorId,n.responderId]),i=new a;return r[n.initiatorId]=i.toAe(r[n.initiatorId]),r[n.responderId]=i.toAe(r[n.responderId]),r},this.status=async e=>{if(!e)return Promise.reject("please send the channelId");const{channel:t}=l(e);return{state:await c(t),balances:await this.balance(e)}},this.setListener=(e,t,n)=>{if(!e)return Promise.reject("please send the channelId");const r=l(e);if(!(n&&n instanceof Function))return new Promise(e=>r.on(t,e));r.on(t,n)},this.getChannelIds=()=>Object.keys(s),this.add=t=>{const{initiatorId:i,responderId:l,initiatorDeposit:u,responderDeposit:d,host:h,port:p,role:f,keypair:y}=t;if(!(i&&l&&u&&h&&p&&f))return Promise.reject(`please send all the config parameters {network: (${JSON.stringify(e)}), initiatorId: (${JSON.stringify(i)}), responderId: (${JSON.stringify(l)}), initiatorDeposit: (${JSON.stringify(u)}),host: (${JSON.stringify(h)}), port: (${JSON.stringify(p)}), role: (${JSON.stringify(f)}), keypair: ${JSON.stringify(y)}}`);const w=new a,m={url:this.network.channelUrl,initiatorId:i,responderId:l,initiatorAmount:w.toAettos(u),responderAmount:w.toAettos(d),pushAmount:0,channelReserve:0,ttl:1e3,host:h,port:p,lockPeriod:10,minimumDepth:0,role:f};return r({...m,sign:n(y)}).then(e=>o(e)("open")).then(e=>{const t=e.id();return console.log("channelId: ",t),null==s[t]&&(s[t]={}),s[t][y.publicKey]={params:m,channel:e},c(e)})},this.update=async e=>{const{channelId:t,keypair:n,senderPublicKey:r,receiverPublicKey:i,amount:s,memo:o}=e;if(!(t&&r&&i&&s))return Promise.reject("either of these parameters are empty: channelId, senderPublicKey, receiverPublicKey, amount");const{channel:c}=l(t,n.publicKey),u=new a(n,this.network),d=await u.wallet();return c.update(r,i,u.toAettos(s),async e=>d.signTransaction(e),[o]).then(async e=>({result:e,status:await this.status(t)}))},this.update_old=async e=>{const{channelId:t,keypair:n,from:r,to:i,amount:s,signedTx:o,block_hash:c,memo:u}=e;if(!(t&&r&&i&&s&&o))return Promise.reject("either of these parameters are empty: channelId, updates, signedTx");const{channel:d}=l(t,n.publicKey),h=new a(n,this.network),p=await h.wallet();return d.update_old(r,i,s,o,c,async e=>p.signTransaction(e),[u]).then(async e=>({result:e,status:await this.status(t)}))},this.update_full=async e=>{const{channelId:t,keypair:n,signedTx:r,memo:i}=e;if(!t||!r)return Promise.reject("either of these parameters are empty: channelId, signedTx");const{channel:s}=l(t,n.publicKey),o=new a(n,this.network),c=await o.wallet();return s.update_full(r,async e=>c.signTransaction(e),[i]).then(async e=>({result:e,status:await this.status(t)}))},this.leave=async(e,t)=>{if(!e)return Promise.reject("please send the channelId");if(!t)return Promise.reject("please send the publicKey");const{channel:n}=l(e,t),r=await n.leave();return s[e][t].offchainTx=r.signedTx,r},this.reconnect=async(e,t)=>{if(!e)return Promise.reject("please send the channelId");const{params:n,channel:i,offchainTx:c}=l(e,t.publicKey);if(!c)return Promise.reject("can not reconnect before leaving the channel");const u=i.round(),d=i.fsmId(),h=new a(t,this.network),p=await h.wallet();return r({url:n.url,host:n.host,port:n.port,role:n.role,round:u,existingChannelId:e,offchainTx:c,existingFsmId:d,sign:async e=>p.signTransaction(e)}).then(e=>o(e)("open")).then(async n=>(s[e][t.publicKey].channel=n,!0))},this.close=async(e,t)=>{if(!e||!t)return Promise.reject("please send the channelId and keypair");const{channel:n}=l(e,t.publicKey),r=new a(t,this.network),o=await r.wallet(),c=await n.shutdown(async e=>await o.signTransaction(e));delete s[e];const{txType:u,tx:d}=i.unpackTx(c);return{result:c,txType:u,tx:d}}}}}])}));