hw-app-iota
Version:
Ledger Hardware Wallet IOTA Application API
3 lines (2 loc) • 13.5 kB
JavaScript
import e from"struct";import r from"iota.lib.js/lib/crypto/bundle/bundle.js";import{addChecksum as t,noChecksum as n,transactionTrytes as i}from"iota.lib.js/lib/utils/utils.js";import a from"bip32-path";import s from"semver";import o from"joi";function u(){return(u=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var d=o.number().integer(),c=d.min(1).max(3),f=d.min(0).max(4294967295),l=d.min(0).max(0x9dff7d32d5dc1),h=d.min(1).max(0x9dff7d32d5dc1),p=o.string().regex(/^[A-Z9]+$/),v=p.allow("").max(27),m=o.alternatives().try(p.length(81),p.length(90)),g=o.array().items(o.object({address:m.required(),tag:v.required(),value:l.required()}).unknown()).min(1),_=o.array().items(o.object({address:m.required(),balance:h.required(),keyIndex:f.required(),tags:o.array().items(v).optional()}).unknown()).min(1),y=o.object({address:m.required(),keyIndex:f.required(),tag:v.optional()}).unknown();function w(e,r,t){if(!e.s){if(t instanceof b){if(!t.s)return void(t.o=w.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(w.bind(null,e,r),w.bind(null,e,2));e.s=r,e.v=t;var n=e.o;n&&n(e)}}var b=function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var a=1&i?r:t;if(a){try{w(n,1,a(this.v))}catch(e){w(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?w(n,1,r?r(i):i):t?w(n,1,t(i)):w(n,2,i)}catch(e){w(n,2,e)}},n},e}();function x(e){return e instanceof b&&1&e.s}function P(e,r,t){for(var n;;){var i=e();if(x(i)&&(i=i.v),!i)return a;if(i.then){n=0;break}var a=t();if(a&&a.then){if(!x(a)){n=1;break}a=a.s}if(r){var s=r();if(s&&s.then&&!x(s)){n=2;break}}}var o=new b,u=w.bind(null,o,2);return(0===n?i.then(c):1===n?a.then(d):s.then(f)).then(void 0,u),o;function d(n){a=n;do{if(r&&(s=r())&&s.then&&!x(s))return void s.then(f).then(void 0,u);if(!(i=e())||x(i)&&!i.v)return void w(o,1,a);if(i.then)return void i.then(c).then(void 0,u);x(a=t())&&(a=a.v)}while(!a||!a.then);a.then(d).then(void 0,u)}function c(e){e?(a=t())&&a.then?a.then(d).then(void 0,u):d(a):w(o,1,a)}function f(){(i=e())?i.then?i.then(c).then(void 0,u):c(i):w(o,1,a)}}var I="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator",S="9".repeat(27),A=function(){function d(e){e.decorateAppAPIMethods(this,["setActiveSeed","getAddress","prepareTransfers","getAppVersion","getAppMaxBundleSize"],"IOT"),this.transport=e,this.config=void 0,this.security=0,this.pathArray=void 0}var l=d.prototype;return l.setActiveSeed=function(e,r){void 0===r&&(r=2);try{var t=this,n=function(e){t.config=e;var r=s.satisfies(t.config.app_version,"<0.5")?(t._createPubkeyInput=t._createPubkeyInputLegacy,t._createTxInput=t._createTxInputLegacy,Promise.resolve(t._setSeed()).then(function(){})):Promise.resolve(t._reset(!0)).then(function(){});if(r&&r.then)return r.then(function(){})};return o.assert(e,o.string().required()),o.assert(r,c.required()),t.pathArray=d._validatePath(e),t.security=r,Promise.resolve(t.config?n(t.config):Promise.resolve(t._getAppConfig()).then(n))}catch(e){return Promise.reject(e)}},l.getAddress=function(e,r){void 0===r&&(r={});try{this._assertInitialized(),o.assert(e,f.required());var n=r.checksum||!1;return Promise.resolve(this._publicKey(e,r.display||!1)).then(function(e){return n?t(e):e})}catch(e){return Promise.reject(e)}},l.prepareTransfers=function(e,r,t,n){void 0===n&&(n=function(){return Date.now()});try{var i=this;if(i._assertInitialized(),o.assert(e,g.required()),o.assert(r,_.required()),o.assert(t,y.optional()),o.assert(n,o.func().arity(0).required()),1!=e.length)throw new Error("unsupported number of transfers");return t=d._validateRemainder(e,r,t),Promise.resolve(i._prepareTransfers(e,r,t,n)).then(function(e){return Promise.resolve(i._reset(!0)).then(function(){return e})})}catch(e){return Promise.reject(e)}},l.getAppVersion=function(){try{var e=this;return Promise.resolve(e._getAppConfig()).then(function(r){return e.config=r,r.app_version})}catch(e){return Promise.reject(e)}},l.getAppMaxBundleSize=function(){try{var e=this;return Promise.resolve(e._getAppConfig()).then(function(r){return e.config=r,r.app_max_bundle_size?r.app_max_bundle_size:8})}catch(e){return Promise.reject(e)}},d._validatePath=function(e){var r;try{r=a.fromString(e).toPathArray()}catch(e){throw new Error('"path" invalid: '+e.message)}if(!r||r.length<2||r.length>5)throw new Error('"path" invalid: Invalid path length');return r},l._assertInitialized=function(){if(!this.security)throw new Error("seed not yet initialized")},l._addSeedFields=function(e){return e.word8("security").word32Ule("pathLength").array("pathArray",this.pathArray.length,"word32Ule")},l._initSeedFields=function(e){var r=e.fields;r.security=this.security,r.pathLength=this.pathArray.length,r.pathArray=this.pathArray},l._setSeed=function(){try{var r=this,t=new e;return r._addSeedFields(t),t.allocate(),r._initSeedFields(t),Promise.resolve(r._sendCommand(1,0,0,t.buffer(),1e4)).then(function(){})}catch(e){return Promise.reject(e)}},l._createPubkeyInputLegacy=function(r){var t=new e;return(t=t.word32Ule("index")).allocate(),t.fields.index=r,t},l._createPubkeyInput=function(r){var t=new e;return this._addSeedFields(t),(t=t.word32Ule("index")).allocate(),this._initSeedFields(t),t.fields.index=r,t},l._publicKey=function(r,t){try{var n=this._createPubkeyInput(r);return Promise.resolve(this._sendCommand(2,t?1:0,0,n.buffer(),1e4)).then(function(r){var t=(new e).chars("address",81);return t.setBuffer(r),t.fields.address})}catch(e){return Promise.reject(e)}},d._validateRemainder=function(e,r,t){var n=r.reduce(function(e,r){return e+r.balance},0),i=e.reduce(function(e,r){return e+r.value},0);if(n<i)throw new Error("insufficient balance");if(n>i){if(!t)throw new Error('"remainder" is required');return{address:t.address,value:n-i,keyIndex:t.keyIndex}}},l._sign=function(r,t){try{var n=(new e).word32Ule("index");return n.allocate(),n.fields.index=r,Promise.resolve(this._sendCommand(4,0,0,n.buffer(),1e4)).then(function(r){var n=(new e).chars("signature",t).word8Sle("fragmentsRemaining");return n.setBuffer(r),{signature:n.fields.signature,fragmentsRemaining:n.fields.fragmentsRemaining}})}catch(e){return Promise.reject(e)}},l._createTxInputLegacy=function(r,t,n,i,a,s,o){var u=new e;(u=u.chars("address",81).word32Ule("address_idx").word64Sle("value").chars("tag",27).word32Ule("tx_idx").word32Ule("tx_len").word32Ule("time")).allocate();var d=u.fields;return d.address=r,d.address_idx=t,d.value=n,d.tag=i,d.tx_idx=a,d.tx_len=s,d.time=o,u},l._createTxInput=function(r,t,n,i,a,s,o){var u=new e;0==a&&this._addSeedFields(u),(u=u.chars("address",81).word32Ule("address_idx").word64Sle("value").chars("tag",27).word32Ule("tx_idx").word32Ule("tx_len").word32Ule("time")).allocate(),0==a&&this._initSeedFields(u);var d=u.fields;return d.address=r,d.address_idx=t,d.value=n,d.tag=i,d.tx_idx=a,d.tx_len=s,d.time=o,u},l._transaction=function(r,t,n,i,a,s,o){try{var u=this._createTxInput(r,t,n,i,a,s,o),d=1e4;return a==s&&(d=15e4),Promise.resolve(this._sendCommand(3,0==a?0:128,0,u.buffer(),d)).then(function(r){var t=(new e).word8("finalized").chars("bundleHash",81);return t.setBuffer(r),{finalized:t.fields.finalized,bundleHash:t.fields.bundleHash}})}catch(e){return Promise.reject(e)}},l._getSignatureFragments=function(e,r){try{var t=this,n=function(e){return a.match(/.{2187}/g)},i=2187*t.security/r,a="",s=1,o=P(function(){return s<=i},function(){return s++},function(){return Promise.resolve(t._sign(e,r)).then(function(e){if(a+=e.signature,s===i!=(0===e.fragmentsRemaining))throw new Error("wrong signture length")})});return Promise.resolve(o&&o.then?o.then(n):n())}catch(e){return Promise.reject(e)}},l._addSignatureFragmentsToBundle=function(e){try{var r,t=this,n=0;return Promise.resolve(P(function(){return!r&&n<e.bundle.length},function(){return n++},function(){var i=e.bundle[n];if(!(i.value>=0))return Promise.resolve(t._getSignatureFragments(n,243)).then(function(a){i.signatureMessageFragment=a.shift();for(var s=i.address,o=1;o<t.security;o++){if(++n>=e.bundle.length)return void(r=1);var u=e.bundle[n];u.address===s&&0===u.value&&(u.signatureMessageFragment=a.shift())}})}))}catch(e){return Promise.reject(e)}},l._signBundle=function(e,r){try{var t=this,n=function(){if(!i)throw new Error("bundle not finalized");if(a!==e.bundle[0].bundle)throw new Error("wrong bundle hash");return Promise.resolve(t._addSignatureFragmentsToBundle(e)).then(function(){})},i=!1,a="",s=function(e,r,t){if("function"==typeof e[I]){var n,i,a,s=e[I]();if(function e(t){try{for(;!(n=s.next()).done;)if((t=r(n.value))&&t.then){if(!x(t))return void t.then(e,a||(a=w.bind(null,i=new b,2)));t=t.v}i?w(i,1,t):i=t}catch(e){w(i||(i=new b),2,e)}}(),s.return){var o=function(e){try{n.done||s.return()}catch(e){}return e};if(i&&i.then)return i.then(o,function(e){throw o(e)});o()}return i}if(!("length"in e))throw new TypeError("Object is not iterable");for(var u=[],d=0;d<e.length;d++)u.push(e[d]);return function(e,r,t){var n,i,a=-1;return function t(s){try{for(;++a<e.length;)if((s=r(a))&&s.then){if(!x(s))return void s.then(t,i||(i=w.bind(null,n=new b,2)));s=s.v}n?w(n,1,s):n=s}catch(e){w(n||(n=new b),2,e)}}(),n}(u,function(e){return r(u[e])})}(e.bundle,function(e){return Promise.resolve(t._transaction(e.address,r[e.address]?r[e.address]:0,e.value,e.obsoleteTag,e.currentIndex,e.lastIndex,e.timestamp)).then(function(e){i=e.finalized,a=e.bundleHash})});return Promise.resolve(s&&s.then?s.then(n):n())}catch(e){return Promise.reject(e)}},l._hasDuplicateAddresses=function(e,r,t){var n=new Set;return e.forEach(function(e){return n.add(e.address)}),r.forEach(function(e){return n.add(e.address)}),!(!t||!n.has(t.address))||n.length===e.length+r.length},l._prepareTransfers=function(e,t,a,s){try{var o=this;if(e=e.map(function(e){return u({},e,{address:n(e.address),tag:e.tag?e.tag.padEnd(27,"9"):S})}),t=t.map(function(e){return u({},e,{address:n(e.address),tags:e.tags?e.tags.map(function(e){return e.padEnd(27,"9")}):null})}),a&&(a=u({},a,{address:n(a.address),tag:a.tag?a.tag.padEnd(27,"9"):S})),o._hasDuplicateAddresses(e,t,a))throw new Error("transaction must not contain duplicate addresses");var d=Math.floor(s()/1e3),c=new r;e.forEach(function(e){return c.addEntry(1,e.address,e.value,e.tag,d,-1)}),t.forEach(function(e){for(var r=0;r<o.security;r++)c.addEntry(1,e.address,0==r?-e.balance:0,e.tags?e.tags[r]:S,d,e.keyIndex)}),a&&c.addEntry(1,a.address,a.value,a.tag,d,a.keyIndex),c.addTrytes([]),c.finalize();var f={};return t.forEach(function(e){return f[e.address]=e.keyIndex}),a&&(f[a.address]=a.keyIndex),Promise.resolve(o._signBundle(c,f)).then(function(){var e=[];return c.bundle.forEach(function(r){return e.push(i(r))}),e.reverse()})}catch(e){return Promise.reject(e)}},l._createAppConfigOutputLegacy=function(){return(new e).word8("app_flags").word8("app_version_major").word8("app_version_minor").word8("app_version_patch")},l._createAppConfigOutput=function(){return(new e).word8("app_version_major").word8("app_version_minor").word8("app_version_patch").word8("app_max_bundle_size").word8("app_flags")},l._getAppConfig=function(){try{var e=this;return Promise.resolve(e._sendCommand(16,0,0,void 0,1e4)).then(function(r){var t=e._createAppConfigOutput();r.length<t.length()+2&&(t=e._createAppConfigOutputLegacy()),t.setBuffer(r);var n=t.fields;return{app_max_bundle_size:n.app_max_bundle_size,app_flags:n.app_flags,app_version:n.app_version_major+"."+n.app_version_minor+"."+n.app_version_patch}})}catch(e){return Promise.reject(e)}},l._reset=function(e){void 0===e&&(e=!1);try{return Promise.resolve(this._sendCommand(255,e?1:0,0,void 0,1e4)).then(function(){})}catch(e){return Promise.reject(e)}},l._sendCommand=function(e,r,t,n,i){try{var a=this.transport;return Promise.resolve(function(s,o){try{var u=(a.setExchangeTimeout(i),Promise.resolve(a.send(122,e,r,t,n)))}catch(e){return o(e)}return u&&u.then?u.then(void 0,o):u}(0,function(e){throw e.statusCode&&(e.message=function(e){var r=function(e){switch(e){case 36864:return"Success";case 26368:return"Incorrect input length";case 27264:return"Incorrect data";case 27392:return"Incorrect command parameter";case 27648:return"Incorrect length specified in header";case 27904:return"Invalid INS command";case 28160:return"Incorrect CLA (Wrong application opened)";case 26880:return"Command not allowed (Command out of order)";case 27010:return"Security not satisfied (Device locked)";case 27013:return"Condition of use not satisfied (Denied by the user)";case 25601:return"Security not satisfied (Timeout exceeded)";case 27041:return"Bundle error (Insecure hash)";case 27042:return"Bundle error (Non zero balance)";case 27043:return"Bundle error (Invalid meta transaction)";case 27044:return"Bundle error (Invalid input address/index pair(s))";case 27045:return"Bundle error (Address reused)";case 27012:return"Invalid input data";case 27014:return"App has not been initialized by user";case 27025:return"Invalid transaction index";case 27026:return"Invalid transaction order (Output, Inputs, Change)";case 27027:return"Invalid meta transaction";case 27028:return"Invalid output transaction (Output must come first)"}if(28416<=e&&e<=28671)return"Internal error, please report"}(e);if(r)return"Ledger device: "+r+" (0x"+e.toString(16)+")"}(e.statusCode)||e.message),e}))}catch(e){return Promise.reject(e)}},d}();export default A;
//# sourceMappingURL=iota.module.js.map