@solana/web3.js
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -1,1 +0,1 @@ | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var _classCallCheck=_interopDefault(require("babel-runtime/helpers/classCallCheck")),_createClass=_interopDefault(require("babel-runtime/helpers/createClass")),BN=_interopDefault(require("bn.js")),bs58=_interopDefault(require("bs58")),nacl=_interopDefault(require("tweetnacl")),_getIterator=_interopDefault(require("babel-runtime/core-js/get-iterator")),_Object$assign=_interopDefault(require("babel-runtime/core-js/object/assign")),assert=_interopDefault(require("assert")),_toConsumableArray=_interopDefault(require("babel-runtime/helpers/toConsumableArray")),_Promise=_interopDefault(require("babel-runtime/core-js/promise")),_regeneratorRuntime=_interopDefault(require("babel-runtime/regenerator")),_asyncToGenerator=_interopDefault(require("babel-runtime/helpers/asyncToGenerator")),fetch=_interopDefault(require("node-fetch")),jayson=_interopDefault(require("jayson/lib/client/browser")),superstruct=require("superstruct"),PublicKey=function(){function e(r){_classCallCheck(this,e);if(this._bn="string"==typeof r&&r.startsWith("0x")?new BN(r.substring(2),16):new BN(r,10),this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var r=new Buffer(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");var r=Buffer.alloc(2048),t=0;r.writeUInt32LE(this.keys.length,t),t+=8;var n=!0,a=!1,s=void 0;try{for(var u,o=_getIterator(this.keys);!(n=(u=o.next()).done);n=!0){u.value.toBuffer().copy(r,t),t+=32}}catch(e){a=!0,s=e}finally{try{!n&&o.return&&o.return()}finally{if(a)throw s}}this.programId&&this.programId.toBuffer().copy(r,t);t+=32;var i=Buffer.from(bs58.decode(e));return assert(32===i.length),i.copy(r,t),r.writeUInt32LE(this.fee,t+=32),t+=8,this.userdata.length>0&&(r.writeUInt32LE(this.userdata.length,t),this.userdata.copy(r,t+=8),t+=this.userdata.length),r.slice(0,t)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.length),n;case"signature":var a=e.from.toBuffer(),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),s;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),s=0;a.writeUInt32LE(0,s),a.writeUInt32LE(n,s+=4),s+=8;for(var u=arguments.length,o=Array(u>4?u-4:0),i=4;u>i;i++)o[i-4]=arguments[i];switch(o.length){case 0:a.writeUInt32LE(0,s),s+=4;var c=serializePayment({amount:n,to:t});return c.copy(a,s),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,s+=c.length)});case 1:a.writeUInt32LE(1,s),s+=4;var l=serializeCondition(o[0]);l.copy(a,s),s+=l.length;var f=serializePayment({amount:n,to:t});return f.copy(a,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s+=f.length)});case 2:a.writeUInt32LE(2,s),s+=4;var p=!0,g=!1,h=void 0;try{for(var y,m=_getIterator(o);!(p=(y=m.next()).done);p=!0){var d=serializeCondition(y.value);d.copy(a,s),s+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,s),s+=v.length}}catch(e){g=!0,h=e}finally{try{!p&&m.return&&m.return()}finally{if(g)throw h}}return new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s)});default:throw Error("A maximum of two conditions are support: "+o.length+" provided")}}},{key:"payOnBoth",value:function(e,r,t,n,a,s){var u=Buffer.alloc(1024),o=0;u.writeUInt32LE(0,o),u.writeUInt32LE(n,o+=4),u.writeUInt32LE(3,o+=8),o+=4;for(var i=[a,s],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(u,o),o+=l.length}var f=serializePayment({amount:n,to:t});return f.copy(u,o),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:u.slice(0,o+=f.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:s})}},{key:"applySignature",value:function(e,r,t){var n=Buffer.alloc(4);return n.writeUInt32LE(2,0),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var s,u;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return s={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,s);case 4:return u=r.sent,r.next=7,u.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=superstruct.struct({jsonrpc:superstruct.struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=superstruct.struct.literal("2.0");return superstruct.struct.union([superstruct.struct({jsonrpc:r,id:"string",error:"any"}),superstruct.struct({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(superstruct.struct.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,n,a,s){var u=Buffer.alloc(53),o=0;return u.writeUInt32LE(0,o),u.writeUInt32LE(n,o+=4),u.writeUInt32LE(a,o+=8),o+=8,s.toBuffer().copy(u,o),assert(u.length>=(o+=32)),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:u})}},{key:"move",value:function(r,t,n){var a=Buffer.alloc(12),s=0;return a.writeUInt32LE(2,s),a.writeUInt32LE(n,s+=4),assert((s+=8)===a.length),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:a})}},{key:"assign",value:function(r,t){var n=Buffer.alloc(36),a=0;n.writeUInt32LE(1,a),a+=4;var s=t.toBuffer();return s.copy(n,a),assert((a+=s.length)===n.length),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();exports.Account=Account,exports.BudgetProgram=BudgetProgram,exports.Connection=Connection,exports.PublicKey=PublicKey,exports.SystemProgram=SystemProgram,exports.Transaction=Transaction; | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var _classCallCheck=_interopDefault(require("babel-runtime/helpers/classCallCheck")),_createClass=_interopDefault(require("babel-runtime/helpers/createClass")),BN=_interopDefault(require("bn.js")),bs58=_interopDefault(require("bs58")),nacl=_interopDefault(require("tweetnacl")),_getIterator=_interopDefault(require("babel-runtime/core-js/get-iterator")),_Object$assign=_interopDefault(require("babel-runtime/core-js/object/assign")),assert=_interopDefault(require("assert")),_toConsumableArray=_interopDefault(require("babel-runtime/helpers/toConsumableArray")),_Promise=_interopDefault(require("babel-runtime/core-js/promise")),_regeneratorRuntime=_interopDefault(require("babel-runtime/regenerator")),_asyncToGenerator=_interopDefault(require("babel-runtime/helpers/asyncToGenerator")),fetch=_interopDefault(require("node-fetch")),jayson=_interopDefault(require("jayson/lib/client/browser")),superstruct=require("superstruct"),PublicKey=function(){function e(r){for(_classCallCheck(this,e);;){if("string"==typeof r){if(/^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(r)){this._bn=new BN(bs58.decode(r));break}if(r.startsWith("0x")){this._bn=new BN(r.substring(2),16);break}}this._bn=new BN(r);break}if(this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var r=new Buffer(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");var r=Buffer.alloc(2048),t=0;r.writeUInt32LE(this.keys.length,t),t+=8;var n=!0,a=!1,s=void 0;try{for(var u,o=_getIterator(this.keys);!(n=(u=o.next()).done);n=!0){u.value.toBuffer().copy(r,t),t+=32}}catch(e){a=!0,s=e}finally{try{!n&&o.return&&o.return()}finally{if(a)throw s}}this.programId&&this.programId.toBuffer().copy(r,t);t+=32;var i=Buffer.from(bs58.decode(e));return assert(32===i.length),i.copy(r,t),r.writeUInt32LE(this.fee,t+=32),t+=8,this.userdata.length>0&&(r.writeUInt32LE(this.userdata.length,t),this.userdata.copy(r,t+=8),t+=this.userdata.length),r.slice(0,t)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.length),n;case"signature":var a=e.from.toBuffer(),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),s;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),s=0;a.writeUInt32LE(0,s),a.writeUInt32LE(n,s+=4),s+=8;for(var u=arguments.length,o=Array(u>4?u-4:0),i=4;u>i;i++)o[i-4]=arguments[i];switch(o.length){case 0:a.writeUInt32LE(0,s),s+=4;var c=serializePayment({amount:n,to:t});return c.copy(a,s),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,s+=c.length)});case 1:a.writeUInt32LE(1,s),s+=4;var l=serializeCondition(o[0]);l.copy(a,s),s+=l.length;var f=serializePayment({amount:n,to:t});return f.copy(a,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s+=f.length)});case 2:a.writeUInt32LE(2,s),s+=4;var p=!0,g=!1,h=void 0;try{for(var y,m=_getIterator(o);!(p=(y=m.next()).done);p=!0){var d=serializeCondition(y.value);d.copy(a,s),s+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,s),s+=v.length}}catch(e){g=!0,h=e}finally{try{!p&&m.return&&m.return()}finally{if(g)throw h}}return new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s)});default:throw Error("A maximum of two conditions are support: "+o.length+" provided")}}},{key:"payOnBoth",value:function(e,r,t,n,a,s){var u=Buffer.alloc(1024),o=0;u.writeUInt32LE(0,o),u.writeUInt32LE(n,o+=4),u.writeUInt32LE(3,o+=8),o+=4;for(var i=[a,s],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(u,o),o+=l.length}var f=serializePayment({amount:n,to:t});return f.copy(u,o),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:u.slice(0,o+=f.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:s})}},{key:"applySignature",value:function(e,r,t){var n=Buffer.alloc(4);return n.writeUInt32LE(2,0),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var s,u;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return s={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,s);case 4:return u=r.sent,r.next=7,u.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=superstruct.struct({jsonrpc:superstruct.struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=superstruct.struct.literal("2.0");return superstruct.struct.union([superstruct.struct({jsonrpc:r,id:"string",error:"any"}),superstruct.struct({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(superstruct.struct.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,n,a,s){var u=Buffer.alloc(53),o=0;u.writeUInt32LE(0,o),u.writeUInt32LE(n,o+=4),u.writeUInt32LE(a,o+=8),o+=8;var i=s.toBuffer();return i.copy(u,o),assert(u.length>=(o+=i.length)),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:u})}},{key:"move",value:function(r,t,n){var a=Buffer.alloc(12),s=0;return a.writeUInt32LE(2,s),a.writeUInt32LE(n,s+=4),assert((s+=8)===a.length),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:a})}},{key:"assign",value:function(r,t){var n=Buffer.alloc(36),a=0;n.writeUInt32LE(1,a),a+=4;var s=t.toBuffer();return s.copy(n,a),assert((a+=s.length)===n.length),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();exports.Account=Account,exports.BudgetProgram=BudgetProgram,exports.Connection=Connection,exports.PublicKey=PublicKey,exports.SystemProgram=SystemProgram,exports.Transaction=Transaction; |
@@ -1,1 +0,1 @@ | ||
import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import BN from"bn.js";import bs58 from"bs58";import nacl from"tweetnacl";import _getIterator from"babel-runtime/core-js/get-iterator";import _Object$assign from"babel-runtime/core-js/object/assign";import assert from"assert";import _toConsumableArray from"babel-runtime/helpers/toConsumableArray";import _Promise from"babel-runtime/core-js/promise";import _regeneratorRuntime from"babel-runtime/regenerator";import _asyncToGenerator from"babel-runtime/helpers/asyncToGenerator";import fetch from"node-fetch";import jayson from"jayson/lib/client/browser";import{struct}from"superstruct";var PublicKey=function(){function e(r){_classCallCheck(this,e);if(this._bn="string"==typeof r&&r.startsWith("0x")?new BN(r.substring(2),16):new BN(r,10),this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var r=new Buffer(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");var r=Buffer.alloc(2048),t=0;r.writeUInt32LE(this.keys.length,t),t+=8;var n=!0,a=!1,s=void 0;try{for(var o,u=_getIterator(this.keys);!(n=(o=u.next()).done);n=!0){o.value.toBuffer().copy(r,t),t+=32}}catch(e){a=!0,s=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw s}}this.programId&&this.programId.toBuffer().copy(r,t);t+=32;var i=Buffer.from(bs58.decode(e));return assert(32===i.length),i.copy(r,t),r.writeUInt32LE(this.fee,t+=32),t+=8,this.userdata.length>0&&(r.writeUInt32LE(this.userdata.length,t),this.userdata.copy(r,t+=8),t+=this.userdata.length),r.slice(0,t)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.length),n;case"signature":var a=e.from.toBuffer(),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),s;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),s=0;a.writeUInt32LE(0,s),a.writeUInt32LE(n,s+=4),s+=8;for(var o=arguments.length,u=Array(o>4?o-4:0),i=4;o>i;i++)u[i-4]=arguments[i];switch(u.length){case 0:a.writeUInt32LE(0,s),s+=4;var c=serializePayment({amount:n,to:t});return c.copy(a,s),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,s+=c.length)});case 1:a.writeUInt32LE(1,s),s+=4;var l=serializeCondition(u[0]);l.copy(a,s),s+=l.length;var f=serializePayment({amount:n,to:t});return f.copy(a,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s+=f.length)});case 2:a.writeUInt32LE(2,s),s+=4;var p=!0,g=!1,h=void 0;try{for(var m,y=_getIterator(u);!(p=(m=y.next()).done);p=!0){var d=serializeCondition(m.value);d.copy(a,s),s+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,s),s+=v.length}}catch(e){g=!0,h=e}finally{try{!p&&y.return&&y.return()}finally{if(g)throw h}}return new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s)});default:throw Error("A maximum of two conditions are support: "+u.length+" provided")}}},{key:"payOnBoth",value:function(e,r,t,n,a,s){var o=Buffer.alloc(1024),u=0;o.writeUInt32LE(0,u),o.writeUInt32LE(n,u+=4),o.writeUInt32LE(3,u+=8),u+=4;for(var i=[a,s],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(o,u),u+=l.length}var f=serializePayment({amount:n,to:t});return f.copy(o,u),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:o.slice(0,u+=f.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:s})}},{key:"applySignature",value:function(e,r,t){var n=Buffer.alloc(4);return n.writeUInt32LE(2,0),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var s,o;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return s={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,s);case 4:return o=r.sent,r.next=7,o.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=struct({jsonrpc:struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=struct.literal("2.0");return struct.union([struct({jsonrpc:r,id:"string",error:"any"}),struct({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(struct.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,n,a,s){var o=Buffer.alloc(53),u=0;return o.writeUInt32LE(0,u),o.writeUInt32LE(n,u+=4),o.writeUInt32LE(a,u+=8),u+=8,s.toBuffer().copy(o,u),assert(o.length>=(u+=32)),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:o})}},{key:"move",value:function(r,t,n){var a=Buffer.alloc(12),s=0;return a.writeUInt32LE(2,s),a.writeUInt32LE(n,s+=4),assert((s+=8)===a.length),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:a})}},{key:"assign",value:function(r,t){var n=Buffer.alloc(36),a=0;n.writeUInt32LE(1,a),a+=4;var s=t.toBuffer();return s.copy(n,a),assert((a+=s.length)===n.length),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();export{Account,BudgetProgram,Connection,PublicKey,SystemProgram,Transaction}; | ||
import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import BN from"bn.js";import bs58 from"bs58";import nacl from"tweetnacl";import _getIterator from"babel-runtime/core-js/get-iterator";import _Object$assign from"babel-runtime/core-js/object/assign";import assert from"assert";import _toConsumableArray from"babel-runtime/helpers/toConsumableArray";import _Promise from"babel-runtime/core-js/promise";import _regeneratorRuntime from"babel-runtime/regenerator";import _asyncToGenerator from"babel-runtime/helpers/asyncToGenerator";import fetch from"node-fetch";import jayson from"jayson/lib/client/browser";import{struct}from"superstruct";var PublicKey=function(){function e(r){for(_classCallCheck(this,e);;){if("string"==typeof r){if(/^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(r)){this._bn=new BN(bs58.decode(r));break}if(r.startsWith("0x")){this._bn=new BN(r.substring(2),16);break}}this._bn=new BN(r);break}if(this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var r=new Buffer(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");var r=Buffer.alloc(2048),t=0;r.writeUInt32LE(this.keys.length,t),t+=8;var n=!0,a=!1,s=void 0;try{for(var o,u=_getIterator(this.keys);!(n=(o=u.next()).done);n=!0){o.value.toBuffer().copy(r,t),t+=32}}catch(e){a=!0,s=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw s}}this.programId&&this.programId.toBuffer().copy(r,t);t+=32;var i=Buffer.from(bs58.decode(e));return assert(32===i.length),i.copy(r,t),r.writeUInt32LE(this.fee,t+=32),t+=8,this.userdata.length>0&&(r.writeUInt32LE(this.userdata.length,t),this.userdata.copy(r,t+=8),t+=this.userdata.length),r.slice(0,t)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.length),n;case"signature":var a=e.from.toBuffer(),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),s;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),s=0;a.writeUInt32LE(0,s),a.writeUInt32LE(n,s+=4),s+=8;for(var o=arguments.length,u=Array(o>4?o-4:0),i=4;o>i;i++)u[i-4]=arguments[i];switch(u.length){case 0:a.writeUInt32LE(0,s),s+=4;var c=serializePayment({amount:n,to:t});return c.copy(a,s),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,s+=c.length)});case 1:a.writeUInt32LE(1,s),s+=4;var l=serializeCondition(u[0]);l.copy(a,s),s+=l.length;var f=serializePayment({amount:n,to:t});return f.copy(a,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s+=f.length)});case 2:a.writeUInt32LE(2,s),s+=4;var p=!0,g=!1,h=void 0;try{for(var m,y=_getIterator(u);!(p=(m=y.next()).done);p=!0){var d=serializeCondition(m.value);d.copy(a,s),s+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,s),s+=v.length}}catch(e){g=!0,h=e}finally{try{!p&&y.return&&y.return()}finally{if(g)throw h}}return new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s)});default:throw Error("A maximum of two conditions are support: "+u.length+" provided")}}},{key:"payOnBoth",value:function(e,r,t,n,a,s){var o=Buffer.alloc(1024),u=0;o.writeUInt32LE(0,u),o.writeUInt32LE(n,u+=4),o.writeUInt32LE(3,u+=8),u+=4;for(var i=[a,s],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(o,u),u+=l.length}var f=serializePayment({amount:n,to:t});return f.copy(o,u),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:o.slice(0,u+=f.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:s})}},{key:"applySignature",value:function(e,r,t){var n=Buffer.alloc(4);return n.writeUInt32LE(2,0),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var s,o;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return s={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,s);case 4:return o=r.sent,r.next=7,o.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=struct({jsonrpc:struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=struct.literal("2.0");return struct.union([struct({jsonrpc:r,id:"string",error:"any"}),struct({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(struct.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",+r.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,n,a,s){var o=Buffer.alloc(53),u=0;o.writeUInt32LE(0,u),o.writeUInt32LE(n,u+=4),o.writeUInt32LE(a,u+=8),u+=8;var i=s.toBuffer();return i.copy(o,u),assert(o.length>=(u+=i.length)),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:o})}},{key:"move",value:function(r,t,n){var a=Buffer.alloc(12),s=0;return a.writeUInt32LE(2,s),a.writeUInt32LE(n,s+=4),assert((s+=8)===a.length),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:a})}},{key:"assign",value:function(r,t){var n=Buffer.alloc(36),a=0;n.writeUInt32LE(1,a),a+=4;var s=t.toBuffer();return s.copy(n,a),assert((a+=s.length)===n.length),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();export{Account,BudgetProgram,Connection,PublicKey,SystemProgram,Transaction}; |
{ | ||
"name": "@solana/web3.js", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Solana Javascript API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4768055
165884