diff --git a/dist/nearlib.js b/dist/nearlib.js index c4790857b..150764cf3 100644 --- a/dist/nearlib.js +++ b/dist/nearlib.js @@ -14858,7 +14858,7 @@ class AccountInfo { * @param {Object} json */ static fromJson(json) { - if (!json.public_key || !json.secret_key || !json.account_id || !json.network_id) { + if (!json.public_key || !json.secret_key || !json.account_id) { throw 'Invalid account info format. Please ensure it contains public_key, secret_key, and account_id".'; } return new AccountInfo(json.account_id, new KeyPair(json.public_key, json.secret_key), json.network_id); @@ -14940,6 +14940,16 @@ class BrowserLocalStorageKeystore { BrowserLocalStorageKeystore.storageKeyForSecretKey(accountId), key.getSecretKey()); } + /** + * Removes the key from local storage for a given id. Please be sure to store the key somewhere before doing this + * to prevent permanent key loss. + * @param {string} accountId + */ + async removeKey(accountId) { + this.localStorage.removeItem(BrowserLocalStorageKeystore.storageKeyForPublicKey(accountId)); + this.localStorage.removeItem(BrowserLocalStorageKeystore.storageKeyForSecretKey(accountId)); + } + async setKeyFromJson(json) { const accountInfo = AccountInfo.fromJson(json); if (this.networkId != accountInfo.networkId) { @@ -14991,6 +15001,12 @@ class InMemoryKeyStore { return this.keys[accountId + '_' + this.networkId]; } + async removeKey(accountId) { + if (this.getKey(accountId)) { + this.setKey(accountId, undefined); + } + } + async clear() { this.keys = {}; } @@ -15245,15 +15261,12 @@ module.exports = WalletAccessKey; * Wallet based account and signer that uses external wallet through the iframe to sign transactions. */ -const { sha256 } = require('js-sha256'); const { FunctionCallTransaction } = require('./protos'); -const EMBED_WALLET_URL_SUFFIX = '/embed/'; const LOGIN_WALLET_URL_SUFFIX = '/login/'; -const RANDOM_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; -const REQUEST_ID_LENGTH = 32; const LOCAL_STORAGE_KEY_SUFFIX = '_wallet_auth_key'; +const PENDING_ACCESS_KEY_PREFIX = 'pending_key'; // browser storage key for a pending access key (i.e. key has been generated but we are not sure it was added yet) /** * Wallet based account and signer that uses external wallet through the iframe to sign transactions. @@ -15267,16 +15280,15 @@ const LOCAL_STORAGE_KEY_SUFFIX = '_wallet_auth_key'; */ class WalletAccount { - constructor(appKeyPrefix, walletBaseUrl = 'https://wallet.nearprotocol.com') { - this._walletBaseUrl = walletBaseUrl; + constructor(appKeyPrefix, walletBaseUrl = 'https://wallet.nearprotocol.com', keyStore = new nearlib.BrowserLocalStorageKeystore()) { + this._walletBaseUrl = walletBaseUrl; this._authDataKey = appKeyPrefix + LOCAL_STORAGE_KEY_SUFFIX; + this._keyStore = keyStore; - this._initHtmlElements(); - this._signatureRequests = {}; this._authData = JSON.parse(window.localStorage.getItem(this._authDataKey) || '{}'); if (!this.isSignedIn()) { - this._tryInitFromUrl(); + this._completeSignInWithAccessKey(); } } @@ -15319,99 +15331,43 @@ class WalletAccount { newUrl.searchParams.set('success_url', success_url || currentUrl.href); newUrl.searchParams.set('failure_url', failure_url || currentUrl.href); newUrl.searchParams.set('app_url', currentUrl.origin); - window.location.replace(newUrl.toString()); + if (!this.getAccountId() || !this._keyStore.getKey(this.getAccountId())) { + const accessKey = nearlib.KeyPair.fromRandomSeed(); + newUrl.searchParams.set('public_key', accessKey.getPublicKey()); + this._keyStore.setKey(PENDING_ACCESS_KEY_PREFIX + accessKey.getPublicKey(), accessKey).then(window.location.replace(newUrl.toString())); + } } + /** - * Sign out from the current account - * @example - * walletAccount.signOut(); + * Complete sign in for a given account id and public key. To be invoked by the app when getting a callback from the wallet. */ - signOut() { - this._authData = {}; - window.localStorage.removeItem(this._authDataKey); - } - - _tryInitFromUrl() { + _completeSignInWithAccessKey() { let currentUrl = new URL(window.location.href); - let authToken = currentUrl.searchParams.get('auth_token') || ''; + let publicKey = currentUrl.searchParams.get('public_key') || ''; let accountId = currentUrl.searchParams.get('account_id') || ''; - if (!!authToken && !!accountId) { - this._authData = { - authToken, - accountId, - }; - window.localStorage.setItem(this._authDataKey, JSON.stringify(this._authData)); - } - } - - _initHtmlElements() { - // Wallet iframe - const iframe = document.createElement('iframe'); - iframe.style = 'display: none;'; - iframe.src = this._walletBaseUrl + EMBED_WALLET_URL_SUFFIX; - document.body.appendChild(iframe); - this._walletWindow = iframe.contentWindow; - - // Message Event - window.addEventListener('message', this.receiveMessage.bind(this), false); - } - - receiveMessage(event) { - if (!this._walletBaseUrl.startsWith(event.origin)) { - // Only processing wallet messages. - console.log('Wallet account ignoring message from ' + event.origin); - return; - } - let data; - try { - data = JSON.parse(event.data); - } catch (e) { - console.error('Can\'t parse the result', event.data, e); - return; - } - const request_id = data.request_id || ''; - if (!(request_id in this._signatureRequests)) { - console.error('Request ID' + request_id + ' was not found'); - return; - } - let signatureRequest = this._signatureRequests[request_id]; - delete this._signatureRequests[request_id]; - - if (data.success) { - signatureRequest.resolve(data.result); - } else { - signatureRequest.reject(data.error); + if (accountId && publicKey) { + this._moveKeyFromTempToPermanent(accountId, publicKey); } } - _randomRequestId() { - var result = ''; - - for (var i = 0; i < REQUEST_ID_LENGTH; i++) { - result += RANDOM_ALPHABET.charAt(Math.floor(Math.random() * RANDOM_ALPHABET.length)); - } - - return result; + async _moveKeyFromTempToPermanent(accountId, publicKey) { + let keyPair = await this._keyStore.getKey(PENDING_ACCESS_KEY_PREFIX + publicKey); + this._authData = { + accountId + }; + window.localStorage.setItem(this._authDataKey, JSON.stringify(this._authData)); + await this._keyStore.setKey(accountId, keyPair); + await this._keyStore.removeKey(PENDING_ACCESS_KEY_PREFIX + publicKey); } - _remoteSign(hash, methodName, args) { - // TODO(#482): Add timeout. - return new Promise((resolve, reject) => { - const request_id = this._randomRequestId(); - this._signatureRequests[request_id] = { - request_id, - resolve, - reject, - }; - this._walletWindow.postMessage(JSON.stringify({ - action: 'sign_transaction', - token: this._authData.authToken, - method_name: methodName, - args: args || {}, - hash, - request_id, - }), this._walletBaseUrl); - }); + /** + * Sign out from the current account + * @example + * walletAccount.signOut(); + */ + signOut() { + this._authData = {}; + window.localStorage.removeItem(this._authDataKey); } /** @@ -15427,10 +15383,10 @@ class WalletAccount { const body = FunctionCallTransaction.decode(buffer); let methodName = Buffer.from(body.methodName).toString(); let args = JSON.parse(Buffer.from(body.args).toString()); - let signature = await this._remoteSign(sha256.array(buffer), methodName, args); - return { - signature, - }; + const signer = new nearlib.SimpleKeyStoreSigner(this._keyStore); + let signature = await signer.signBuffer(buffer, originator); + console.log(signature); + return signature; } } @@ -15438,4 +15394,4 @@ class WalletAccount { module.exports = WalletAccount; }).call(this,require("buffer").Buffer) -},{"./protos":67,"buffer":21,"js-sha256":40}]},{},[2]); +},{"./protos":67,"buffer":21}]},{},[2]); diff --git a/dist/nearlib.min.js b/dist/nearlib.min.js index 2763235c4..43d48ea60 100644 --- a/dist/nearlib.min.js +++ b/dist/nearlib.min.js @@ -275,13 +275,13 @@ module.exports={cache:require("./cache"),eachCombination:require("./eachCombinat "use strict";var $protobuf=require("protobufjs/minimal"),$Reader=$protobuf.Reader,$Writer=$protobuf.Writer,$util=$protobuf.util,$root=$protobuf.roots.default||($protobuf.roots.default={});$root.CreateAccountTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.originator=e.string();break;case 3:o.newAccountId=e.string();break;case 4:o.amount=e.uint64();break;case 5:o.publicKey=e.bytes();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high))?"nonce: integer|Long expected":null!=e.originator&&e.hasOwnProperty("originator")&&!$util.isString(e.originator)?"originator: string expected":null!=e.newAccountId&&e.hasOwnProperty("newAccountId")&&!$util.isString(e.newAccountId)?"newAccountId: string expected":null!=e.amount&&e.hasOwnProperty("amount")&&!($util.isInteger(e.amount)||e.amount&&$util.isInteger(e.amount.low)&&$util.isInteger(e.amount.high))?"amount: integer|Long expected":null!=e.publicKey&&e.hasOwnProperty("publicKey")&&!(e.publicKey&&"number"==typeof e.publicKey.length||$util.isString(e.publicKey))?"publicKey: buffer expected":null},e.fromObject=function(e){if(e instanceof $root.CreateAccountTransaction)return e;var n=new $root.CreateAccountTransaction;return null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.originator&&(n.originator=String(e.originator)),null!=e.newAccountId&&(n.newAccountId=String(e.newAccountId)),null!=e.amount&&($util.Long?(n.amount=$util.Long.fromValue(e.amount)).unsigned=!0:"string"==typeof e.amount?n.amount=parseInt(e.amount,10):"number"==typeof e.amount?n.amount=e.amount:"object"==typeof e.amount&&(n.amount=new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0))),null!=e.publicKey&&("string"==typeof e.publicKey?$util.base64.decode(e.publicKey,n.publicKey=$util.newBuffer($util.base64.length(e.publicKey)),0):e.publicKey.length&&(n.publicKey=e.publicKey)),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;if(t.originator="",t.newAccountId="",$util.Long){o=new $util.Long(0,0,!0);t.amount=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.amount=n.longs===String?"0":0;n.bytes===String?t.publicKey="":(t.publicKey=[],n.bytes!==Array&&(t.publicKey=$util.newBuffer(t.publicKey)))}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.originator&&e.hasOwnProperty("originator")&&(t.originator=e.originator),null!=e.newAccountId&&e.hasOwnProperty("newAccountId")&&(t.newAccountId=e.newAccountId),null!=e.amount&&e.hasOwnProperty("amount")&&("number"==typeof e.amount?t.amount=n.longs===String?String(e.amount):e.amount:t.amount=n.longs===String?$util.Long.prototype.toString.call(e.amount):n.longs===Number?new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0):e.amount),null!=e.publicKey&&e.hasOwnProperty("publicKey")&&(t.publicKey=n.bytes===String?$util.base64.encode(e.publicKey,0,e.publicKey.length):n.bytes===Array?Array.prototype.slice.call(e.publicKey):e.publicKey),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.DeployContractTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.contractId=e.string();break;case 3:o.wasmByteArray=e.bytes();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high))?"nonce: integer|Long expected":null!=e.contractId&&e.hasOwnProperty("contractId")&&!$util.isString(e.contractId)?"contractId: string expected":null!=e.wasmByteArray&&e.hasOwnProperty("wasmByteArray")&&!(e.wasmByteArray&&"number"==typeof e.wasmByteArray.length||$util.isString(e.wasmByteArray))?"wasmByteArray: buffer expected":null},e.fromObject=function(e){if(e instanceof $root.DeployContractTransaction)return e;var n=new $root.DeployContractTransaction;return null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.contractId&&(n.contractId=String(e.contractId)),null!=e.wasmByteArray&&("string"==typeof e.wasmByteArray?$util.base64.decode(e.wasmByteArray,n.wasmByteArray=$util.newBuffer($util.base64.length(e.wasmByteArray)),0):e.wasmByteArray.length&&(n.wasmByteArray=e.wasmByteArray)),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;t.contractId="",n.bytes===String?t.wasmByteArray="":(t.wasmByteArray=[],n.bytes!==Array&&(t.wasmByteArray=$util.newBuffer(t.wasmByteArray)))}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.contractId&&e.hasOwnProperty("contractId")&&(t.contractId=e.contractId),null!=e.wasmByteArray&&e.hasOwnProperty("wasmByteArray")&&(t.wasmByteArray=n.bytes===String?$util.base64.encode(e.wasmByteArray,0,e.wasmByteArray.length):n.bytes===Array?Array.prototype.slice.call(e.wasmByteArray):e.wasmByteArray),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.FunctionCallTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.originator=e.string();break;case 3:o.contractId=e.string();break;case 4:o.methodName=e.bytes();break;case 5:o.args=e.bytes();break;case 6:o.amount=e.uint64();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high))?"nonce: integer|Long expected":null!=e.originator&&e.hasOwnProperty("originator")&&!$util.isString(e.originator)?"originator: string expected":null!=e.contractId&&e.hasOwnProperty("contractId")&&!$util.isString(e.contractId)?"contractId: string expected":null!=e.methodName&&e.hasOwnProperty("methodName")&&!(e.methodName&&"number"==typeof e.methodName.length||$util.isString(e.methodName))?"methodName: buffer expected":null!=e.args&&e.hasOwnProperty("args")&&!(e.args&&"number"==typeof e.args.length||$util.isString(e.args))?"args: buffer expected":null!=e.amount&&e.hasOwnProperty("amount")&&!($util.isInteger(e.amount)||e.amount&&$util.isInteger(e.amount.low)&&$util.isInteger(e.amount.high))?"amount: integer|Long expected":null},e.fromObject=function(e){if(e instanceof $root.FunctionCallTransaction)return e;var n=new $root.FunctionCallTransaction;return null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.originator&&(n.originator=String(e.originator)),null!=e.contractId&&(n.contractId=String(e.contractId)),null!=e.methodName&&("string"==typeof e.methodName?$util.base64.decode(e.methodName,n.methodName=$util.newBuffer($util.base64.length(e.methodName)),0):e.methodName.length&&(n.methodName=e.methodName)),null!=e.args&&("string"==typeof e.args?$util.base64.decode(e.args,n.args=$util.newBuffer($util.base64.length(e.args)),0):e.args.length&&(n.args=e.args)),null!=e.amount&&($util.Long?(n.amount=$util.Long.fromValue(e.amount)).unsigned=!0:"string"==typeof e.amount?n.amount=parseInt(e.amount,10):"number"==typeof e.amount?n.amount=e.amount:"object"==typeof e.amount&&(n.amount=new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0))),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;if(t.originator="",t.contractId="",n.bytes===String?t.methodName="":(t.methodName=[],n.bytes!==Array&&(t.methodName=$util.newBuffer(t.methodName))),n.bytes===String?t.args="":(t.args=[],n.bytes!==Array&&(t.args=$util.newBuffer(t.args))),$util.Long){o=new $util.Long(0,0,!0);t.amount=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.amount=n.longs===String?"0":0}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.originator&&e.hasOwnProperty("originator")&&(t.originator=e.originator),null!=e.contractId&&e.hasOwnProperty("contractId")&&(t.contractId=e.contractId),null!=e.methodName&&e.hasOwnProperty("methodName")&&(t.methodName=n.bytes===String?$util.base64.encode(e.methodName,0,e.methodName.length):n.bytes===Array?Array.prototype.slice.call(e.methodName):e.methodName),null!=e.args&&e.hasOwnProperty("args")&&(t.args=n.bytes===String?$util.base64.encode(e.args,0,e.args.length):n.bytes===Array?Array.prototype.slice.call(e.args):e.args),null!=e.amount&&e.hasOwnProperty("amount")&&("number"==typeof e.amount?t.amount=n.longs===String?String(e.amount):e.amount:t.amount=n.longs===String?$util.Long.prototype.toString.call(e.amount):n.longs===Number?new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0):e.amount),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.SendMoneyTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.originator=e.string();break;case 3:o.receiver=e.string();break;case 4:o.amount=e.uint64();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high))?"nonce: integer|Long expected":null!=e.originator&&e.hasOwnProperty("originator")&&!$util.isString(e.originator)?"originator: string expected":null!=e.receiver&&e.hasOwnProperty("receiver")&&!$util.isString(e.receiver)?"receiver: string expected":null!=e.amount&&e.hasOwnProperty("amount")&&!($util.isInteger(e.amount)||e.amount&&$util.isInteger(e.amount.low)&&$util.isInteger(e.amount.high))?"amount: integer|Long expected":null},e.fromObject=function(e){if(e instanceof $root.SendMoneyTransaction)return e;var n=new $root.SendMoneyTransaction;return null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.originator&&(n.originator=String(e.originator)),null!=e.receiver&&(n.receiver=String(e.receiver)),null!=e.amount&&($util.Long?(n.amount=$util.Long.fromValue(e.amount)).unsigned=!0:"string"==typeof e.amount?n.amount=parseInt(e.amount,10):"number"==typeof e.amount?n.amount=e.amount:"object"==typeof e.amount&&(n.amount=new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0))),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;if(t.originator="",t.receiver="",$util.Long){o=new $util.Long(0,0,!0);t.amount=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.amount=n.longs===String?"0":0}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.originator&&e.hasOwnProperty("originator")&&(t.originator=e.originator),null!=e.receiver&&e.hasOwnProperty("receiver")&&(t.receiver=e.receiver),null!=e.amount&&e.hasOwnProperty("amount")&&("number"==typeof e.amount?t.amount=n.longs===String?String(e.amount):e.amount:t.amount=n.longs===String?$util.Long.prototype.toString.call(e.amount):n.longs===Number?new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0):e.amount),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.StakeTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.originator=e.string();break;case 3:o.amount=e.uint64();break;case 4:o.publicKey=e.string();break;case 5:o.blsPublicKey=e.string();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high))?"nonce: integer|Long expected":null!=e.originator&&e.hasOwnProperty("originator")&&!$util.isString(e.originator)?"originator: string expected":null!=e.amount&&e.hasOwnProperty("amount")&&!($util.isInteger(e.amount)||e.amount&&$util.isInteger(e.amount.low)&&$util.isInteger(e.amount.high))?"amount: integer|Long expected":null!=e.publicKey&&e.hasOwnProperty("publicKey")&&!$util.isString(e.publicKey)?"publicKey: string expected":null!=e.blsPublicKey&&e.hasOwnProperty("blsPublicKey")&&!$util.isString(e.blsPublicKey)?"blsPublicKey: string expected":null},e.fromObject=function(e){if(e instanceof $root.StakeTransaction)return e;var n=new $root.StakeTransaction;return null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.originator&&(n.originator=String(e.originator)),null!=e.amount&&($util.Long?(n.amount=$util.Long.fromValue(e.amount)).unsigned=!0:"string"==typeof e.amount?n.amount=parseInt(e.amount,10):"number"==typeof e.amount?n.amount=e.amount:"object"==typeof e.amount&&(n.amount=new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0))),null!=e.publicKey&&(n.publicKey=String(e.publicKey)),null!=e.blsPublicKey&&(n.blsPublicKey=String(e.blsPublicKey)),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;if(t.originator="",$util.Long){o=new $util.Long(0,0,!0);t.amount=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.amount=n.longs===String?"0":0;t.publicKey="",t.blsPublicKey=""}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.originator&&e.hasOwnProperty("originator")&&(t.originator=e.originator),null!=e.amount&&e.hasOwnProperty("amount")&&("number"==typeof e.amount?t.amount=n.longs===String?String(e.amount):e.amount:t.amount=n.longs===String?$util.Long.prototype.toString.call(e.amount):n.longs===Number?new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0):e.amount),null!=e.publicKey&&e.hasOwnProperty("publicKey")&&(t.publicKey=e.publicKey),null!=e.blsPublicKey&&e.hasOwnProperty("blsPublicKey")&&(t.blsPublicKey=e.blsPublicKey),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.SwapKeyTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.originator=e.string();break;case 3:o.curKey=e.bytes();break;case 4:o.newKey=e.bytes();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high))?"nonce: integer|Long expected":null!=e.originator&&e.hasOwnProperty("originator")&&!$util.isString(e.originator)?"originator: string expected":null!=e.curKey&&e.hasOwnProperty("curKey")&&!(e.curKey&&"number"==typeof e.curKey.length||$util.isString(e.curKey))?"curKey: buffer expected":null!=e.newKey&&e.hasOwnProperty("newKey")&&!(e.newKey&&"number"==typeof e.newKey.length||$util.isString(e.newKey))?"newKey: buffer expected":null},e.fromObject=function(e){if(e instanceof $root.SwapKeyTransaction)return e;var n=new $root.SwapKeyTransaction;return null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.originator&&(n.originator=String(e.originator)),null!=e.curKey&&("string"==typeof e.curKey?$util.base64.decode(e.curKey,n.curKey=$util.newBuffer($util.base64.length(e.curKey)),0):e.curKey.length&&(n.curKey=e.curKey)),null!=e.newKey&&("string"==typeof e.newKey?$util.base64.decode(e.newKey,n.newKey=$util.newBuffer($util.base64.length(e.newKey)),0):e.newKey.length&&(n.newKey=e.newKey)),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;t.originator="",n.bytes===String?t.curKey="":(t.curKey=[],n.bytes!==Array&&(t.curKey=$util.newBuffer(t.curKey))),n.bytes===String?t.newKey="":(t.newKey=[],n.bytes!==Array&&(t.newKey=$util.newBuffer(t.newKey)))}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.originator&&e.hasOwnProperty("originator")&&(t.originator=e.originator),null!=e.curKey&&e.hasOwnProperty("curKey")&&(t.curKey=n.bytes===String?$util.base64.encode(e.curKey,0,e.curKey.length):n.bytes===Array?Array.prototype.slice.call(e.curKey):e.curKey),null!=e.newKey&&e.hasOwnProperty("newKey")&&(t.newKey=n.bytes===String?$util.base64.encode(e.newKey,0,e.newKey.length):n.bytes===Array?Array.prototype.slice.call(e.newKey):e.newKey),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.AddKeyTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.originator=e.string();break;case 3:o.newKey=e.bytes();break;case 4:o.accessKey=$root.AccessKey.decode(e,e.uint32());break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){if("object"!=typeof e||null===e)return"object expected";if(null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high)))return"nonce: integer|Long expected";if(null!=e.originator&&e.hasOwnProperty("originator")&&!$util.isString(e.originator))return"originator: string expected";if(null!=e.newKey&&e.hasOwnProperty("newKey")&&!(e.newKey&&"number"==typeof e.newKey.length||$util.isString(e.newKey)))return"newKey: buffer expected";if(null!=e.accessKey&&e.hasOwnProperty("accessKey")){var n=$root.AccessKey.verify(e.accessKey);if(n)return"accessKey."+n}return null},e.fromObject=function(e){if(e instanceof $root.AddKeyTransaction)return e;var n=new $root.AddKeyTransaction;if(null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.originator&&(n.originator=String(e.originator)),null!=e.newKey&&("string"==typeof e.newKey?$util.base64.decode(e.newKey,n.newKey=$util.newBuffer($util.base64.length(e.newKey)),0):e.newKey.length&&(n.newKey=e.newKey)),null!=e.accessKey){if("object"!=typeof e.accessKey)throw TypeError(".AddKeyTransaction.accessKey: object expected");n.accessKey=$root.AccessKey.fromObject(e.accessKey)}return n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;t.originator="",n.bytes===String?t.newKey="":(t.newKey=[],n.bytes!==Array&&(t.newKey=$util.newBuffer(t.newKey))),t.accessKey=null}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.originator&&e.hasOwnProperty("originator")&&(t.originator=e.originator),null!=e.newKey&&e.hasOwnProperty("newKey")&&(t.newKey=n.bytes===String?$util.base64.encode(e.newKey,0,e.newKey.length):n.bytes===Array?Array.prototype.slice.call(e.newKey):e.newKey),null!=e.accessKey&&e.hasOwnProperty("accessKey")&&(t.accessKey=$root.AccessKey.toObject(e.accessKey,n)),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.DeleteKeyTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.nonce=e.uint64();break;case 2:o.originator=e.string();break;case 3:o.curKey=e.bytes();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.nonce&&e.hasOwnProperty("nonce")&&!($util.isInteger(e.nonce)||e.nonce&&$util.isInteger(e.nonce.low)&&$util.isInteger(e.nonce.high))?"nonce: integer|Long expected":null!=e.originator&&e.hasOwnProperty("originator")&&!$util.isString(e.originator)?"originator: string expected":null!=e.curKey&&e.hasOwnProperty("curKey")&&!(e.curKey&&"number"==typeof e.curKey.length||$util.isString(e.curKey))?"curKey: buffer expected":null},e.fromObject=function(e){if(e instanceof $root.DeleteKeyTransaction)return e;var n=new $root.DeleteKeyTransaction;return null!=e.nonce&&($util.Long?(n.nonce=$util.Long.fromValue(e.nonce)).unsigned=!0:"string"==typeof e.nonce?n.nonce=parseInt(e.nonce,10):"number"==typeof e.nonce?n.nonce=e.nonce:"object"==typeof e.nonce&&(n.nonce=new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0))),null!=e.originator&&(n.originator=String(e.originator)),null!=e.curKey&&("string"==typeof e.curKey?$util.base64.decode(e.curKey,n.curKey=$util.newBuffer($util.base64.length(e.curKey)),0):e.curKey.length&&(n.curKey=e.curKey)),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.nonce=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.nonce=n.longs===String?"0":0;t.originator="",n.bytes===String?t.curKey="":(t.curKey=[],n.bytes!==Array&&(t.curKey=$util.newBuffer(t.curKey)))}return null!=e.nonce&&e.hasOwnProperty("nonce")&&("number"==typeof e.nonce?t.nonce=n.longs===String?String(e.nonce):e.nonce:t.nonce=n.longs===String?$util.Long.prototype.toString.call(e.nonce):n.longs===Number?new $util.LongBits(e.nonce.low>>>0,e.nonce.high>>>0).toNumber(!0):e.nonce),null!=e.originator&&e.hasOwnProperty("originator")&&(t.originator=e.originator),null!=e.curKey&&e.hasOwnProperty("curKey")&&(t.curKey=n.bytes===String?$util.base64.encode(e.curKey,0,e.curKey.length):n.bytes===Array?Array.prototype.slice.call(e.curKey):e.curKey),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.SignedTransaction=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.signature=e.bytes();break;case 10:o.publicKey=$root.google.protobuf.BytesValue.decode(e,e.uint32());break;case 2:o.createAccount=$root.CreateAccountTransaction.decode(e,e.uint32());break;case 3:o.deployContract=$root.DeployContractTransaction.decode(e,e.uint32());break;case 4:o.functionCall=$root.FunctionCallTransaction.decode(e,e.uint32());break;case 5:o.sendMoney=$root.SendMoneyTransaction.decode(e,e.uint32());break;case 6:o.stake=$root.StakeTransaction.decode(e,e.uint32());break;case 7:o.swapKey=$root.SwapKeyTransaction.decode(e,e.uint32());break;case 8:o.addKey=$root.AddKeyTransaction.decode(e,e.uint32());break;case 9:o.deleteKey=$root.DeleteKeyTransaction.decode(e,e.uint32());break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){if("object"!=typeof e||null===e)return"object expected";var n={};if(null!=e.signature&&e.hasOwnProperty("signature")&&!(e.signature&&"number"==typeof e.signature.length||$util.isString(e.signature)))return"signature: buffer expected";if(null!=e.publicKey&&e.hasOwnProperty("publicKey")&&(t=$root.google.protobuf.BytesValue.verify(e.publicKey)))return"publicKey."+t;if(null!=e.createAccount&&e.hasOwnProperty("createAccount")&&(n.body=1,t=$root.CreateAccountTransaction.verify(e.createAccount)))return"createAccount."+t;if(null!=e.deployContract&&e.hasOwnProperty("deployContract")){if(1===n.body)return"body: multiple values";if(n.body=1,t=$root.DeployContractTransaction.verify(e.deployContract))return"deployContract."+t}if(null!=e.functionCall&&e.hasOwnProperty("functionCall")){if(1===n.body)return"body: multiple values";if(n.body=1,t=$root.FunctionCallTransaction.verify(e.functionCall))return"functionCall."+t}if(null!=e.sendMoney&&e.hasOwnProperty("sendMoney")){if(1===n.body)return"body: multiple values";if(n.body=1,t=$root.SendMoneyTransaction.verify(e.sendMoney))return"sendMoney."+t}if(null!=e.stake&&e.hasOwnProperty("stake")){if(1===n.body)return"body: multiple values";if(n.body=1,t=$root.StakeTransaction.verify(e.stake))return"stake."+t}if(null!=e.swapKey&&e.hasOwnProperty("swapKey")){if(1===n.body)return"body: multiple values";if(n.body=1,t=$root.SwapKeyTransaction.verify(e.swapKey))return"swapKey."+t}if(null!=e.addKey&&e.hasOwnProperty("addKey")){if(1===n.body)return"body: multiple values";if(n.body=1,t=$root.AddKeyTransaction.verify(e.addKey))return"addKey."+t}if(null!=e.deleteKey&&e.hasOwnProperty("deleteKey")){if(1===n.body)return"body: multiple values";var t;if(n.body=1,t=$root.DeleteKeyTransaction.verify(e.deleteKey))return"deleteKey."+t}return null},e.fromObject=function(e){if(e instanceof $root.SignedTransaction)return e;var n=new $root.SignedTransaction;if(null!=e.signature&&("string"==typeof e.signature?$util.base64.decode(e.signature,n.signature=$util.newBuffer($util.base64.length(e.signature)),0):e.signature.length&&(n.signature=e.signature)),null!=e.publicKey){if("object"!=typeof e.publicKey)throw TypeError(".SignedTransaction.publicKey: object expected");n.publicKey=$root.google.protobuf.BytesValue.fromObject(e.publicKey)}if(null!=e.createAccount){if("object"!=typeof e.createAccount)throw TypeError(".SignedTransaction.createAccount: object expected");n.createAccount=$root.CreateAccountTransaction.fromObject(e.createAccount)}if(null!=e.deployContract){if("object"!=typeof e.deployContract)throw TypeError(".SignedTransaction.deployContract: object expected");n.deployContract=$root.DeployContractTransaction.fromObject(e.deployContract)}if(null!=e.functionCall){if("object"!=typeof e.functionCall)throw TypeError(".SignedTransaction.functionCall: object expected");n.functionCall=$root.FunctionCallTransaction.fromObject(e.functionCall)}if(null!=e.sendMoney){if("object"!=typeof e.sendMoney)throw TypeError(".SignedTransaction.sendMoney: object expected");n.sendMoney=$root.SendMoneyTransaction.fromObject(e.sendMoney)}if(null!=e.stake){if("object"!=typeof e.stake)throw TypeError(".SignedTransaction.stake: object expected");n.stake=$root.StakeTransaction.fromObject(e.stake)}if(null!=e.swapKey){if("object"!=typeof e.swapKey)throw TypeError(".SignedTransaction.swapKey: object expected");n.swapKey=$root.SwapKeyTransaction.fromObject(e.swapKey)}if(null!=e.addKey){if("object"!=typeof e.addKey)throw TypeError(".SignedTransaction.addKey: object expected");n.addKey=$root.AddKeyTransaction.fromObject(e.addKey)}if(null!=e.deleteKey){if("object"!=typeof e.deleteKey)throw TypeError(".SignedTransaction.deleteKey: object expected");n.deleteKey=$root.DeleteKeyTransaction.fromObject(e.deleteKey)}return n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(n.bytes===String?t.signature="":(t.signature=[],n.bytes!==Array&&(t.signature=$util.newBuffer(t.signature))),t.publicKey=null),null!=e.signature&&e.hasOwnProperty("signature")&&(t.signature=n.bytes===String?$util.base64.encode(e.signature,0,e.signature.length):n.bytes===Array?Array.prototype.slice.call(e.signature):e.signature),null!=e.createAccount&&e.hasOwnProperty("createAccount")&&(t.createAccount=$root.CreateAccountTransaction.toObject(e.createAccount,n),n.oneofs&&(t.body="createAccount")),null!=e.deployContract&&e.hasOwnProperty("deployContract")&&(t.deployContract=$root.DeployContractTransaction.toObject(e.deployContract,n),n.oneofs&&(t.body="deployContract")),null!=e.functionCall&&e.hasOwnProperty("functionCall")&&(t.functionCall=$root.FunctionCallTransaction.toObject(e.functionCall,n),n.oneofs&&(t.body="functionCall")),null!=e.sendMoney&&e.hasOwnProperty("sendMoney")&&(t.sendMoney=$root.SendMoneyTransaction.toObject(e.sendMoney,n),n.oneofs&&(t.body="sendMoney")),null!=e.stake&&e.hasOwnProperty("stake")&&(t.stake=$root.StakeTransaction.toObject(e.stake,n),n.oneofs&&(t.body="stake")),null!=e.swapKey&&e.hasOwnProperty("swapKey")&&(t.swapKey=$root.SwapKeyTransaction.toObject(e.swapKey,n),n.oneofs&&(t.body="swapKey")),null!=e.addKey&&e.hasOwnProperty("addKey")&&(t.addKey=$root.AddKeyTransaction.toObject(e.addKey,n),n.oneofs&&(t.body="addKey")),null!=e.deleteKey&&e.hasOwnProperty("deleteKey")&&(t.deleteKey=$root.DeleteKeyTransaction.toObject(e.deleteKey,n),n.oneofs&&(t.body="deleteKey")),null!=e.publicKey&&e.hasOwnProperty("publicKey")&&(t.publicKey=$root.google.protobuf.BytesValue.toObject(e.publicKey,n)),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),$root.google=function(){var e,n={};return n.protobuf=((e={}).DoubleValue=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.double();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&"number"!=typeof e.value?"value: number expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.DoubleValue)return e;var n=new $root.google.protobuf.DoubleValue;return null!=e.value&&(n.value=Number(e.value)),n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(t.value=0),null!=e.value&&e.hasOwnProperty("value")&&(t.value=n.json&&!isFinite(e.value)?String(e.value):e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.FloatValue=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.float();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&"number"!=typeof e.value?"value: number expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.FloatValue)return e;var n=new $root.google.protobuf.FloatValue;return null!=e.value&&(n.value=Number(e.value)),n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(t.value=0),null!=e.value&&e.hasOwnProperty("value")&&(t.value=n.json&&!isFinite(e.value)?String(e.value):e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.Int64Value=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.int64();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&!($util.isInteger(e.value)||e.value&&$util.isInteger(e.value.low)&&$util.isInteger(e.value.high))?"value: integer|Long expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.Int64Value)return e;var n=new $root.google.protobuf.Int64Value;return null!=e.value&&($util.Long?(n.value=$util.Long.fromValue(e.value)).unsigned=!1:"string"==typeof e.value?n.value=parseInt(e.value,10):"number"==typeof e.value?n.value=e.value:"object"==typeof e.value&&(n.value=new $util.LongBits(e.value.low>>>0,e.value.high>>>0).toNumber())),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults)if($util.Long){var o=new $util.Long(0,0,!1);t.value=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.value=n.longs===String?"0":0;return null!=e.value&&e.hasOwnProperty("value")&&("number"==typeof e.value?t.value=n.longs===String?String(e.value):e.value:t.value=n.longs===String?$util.Long.prototype.toString.call(e.value):n.longs===Number?new $util.LongBits(e.value.low>>>0,e.value.high>>>0).toNumber():e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.UInt64Value=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.uint64();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&!($util.isInteger(e.value)||e.value&&$util.isInteger(e.value.low)&&$util.isInteger(e.value.high))?"value: integer|Long expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.UInt64Value)return e;var n=new $root.google.protobuf.UInt64Value;return null!=e.value&&($util.Long?(n.value=$util.Long.fromValue(e.value)).unsigned=!0:"string"==typeof e.value?n.value=parseInt(e.value,10):"number"==typeof e.value?n.value=e.value:"object"==typeof e.value&&(n.value=new $util.LongBits(e.value.low>>>0,e.value.high>>>0).toNumber(!0))),n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults)if($util.Long){var o=new $util.Long(0,0,!0);t.value=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.value=n.longs===String?"0":0;return null!=e.value&&e.hasOwnProperty("value")&&("number"==typeof e.value?t.value=n.longs===String?String(e.value):e.value:t.value=n.longs===String?$util.Long.prototype.toString.call(e.value):n.longs===Number?new $util.LongBits(e.value.low>>>0,e.value.high>>>0).toNumber(!0):e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.Int32Value=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.int32();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&!$util.isInteger(e.value)?"value: integer expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.Int32Value)return e;var n=new $root.google.protobuf.Int32Value;return null!=e.value&&(n.value=0|e.value),n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(t.value=0),null!=e.value&&e.hasOwnProperty("value")&&(t.value=e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.UInt32Value=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.uint32();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&!$util.isInteger(e.value)?"value: integer expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.UInt32Value)return e;var n=new $root.google.protobuf.UInt32Value;return null!=e.value&&(n.value=e.value>>>0),n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(t.value=0),null!=e.value&&e.hasOwnProperty("value")&&(t.value=e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.BoolValue=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.bool();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&"boolean"!=typeof e.value?"value: boolean expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.BoolValue)return e;var n=new $root.google.protobuf.BoolValue;return null!=e.value&&(n.value=Boolean(e.value)),n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(t.value=!1),null!=e.value&&e.hasOwnProperty("value")&&(t.value=e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.StringValue=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.string();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&!$util.isString(e.value)?"value: string expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.StringValue)return e;var n=new $root.google.protobuf.StringValue;return null!=e.value&&(n.value=String(e.value)),n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(t.value=""),null!=e.value&&e.hasOwnProperty("value")&&(t.value=e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e.BytesValue=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.value=e.bytes();break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){return"object"!=typeof e||null===e?"object expected":null!=e.value&&e.hasOwnProperty("value")&&!(e.value&&"number"==typeof e.value.length||$util.isString(e.value))?"value: buffer expected":null},e.fromObject=function(e){if(e instanceof $root.google.protobuf.BytesValue)return e;var n=new $root.google.protobuf.BytesValue;return null!=e.value&&("string"==typeof e.value?$util.base64.decode(e.value,n.value=$util.newBuffer($util.base64.length(e.value)),0):e.value.length&&(n.value=e.value)),n},e.toObject=function(e,n){n||(n={});var t={};return n.defaults&&(n.bytes===String?t.value="":(t.value=[],n.bytes!==Array&&(t.value=$util.newBuffer(t.value)))),null!=e.value&&e.hasOwnProperty("value")&&(t.value=n.bytes===String?$util.base64.encode(e.value,0,e.value.length):n.bytes===Array?Array.prototype.slice.call(e.value):e.value),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),e),n}(),$root.AccessKey=function(){function e(e){if(e)for(var n=Object.keys(e),t=0;t>>3){case 1:o.amount=e.uint64();break;case 2:o.balanceOwner=$root.google.protobuf.StringValue.decode(e,e.uint32());break;case 3:o.contractId=$root.google.protobuf.StringValue.decode(e,e.uint32());break;case 4:o.methodName=$root.google.protobuf.BytesValue.decode(e,e.uint32());break;default:e.skipType(7&r)}}return o},e.decodeDelimited=function(e){return e instanceof $Reader||(e=new $Reader(e)),this.decode(e,e.uint32())},e.verify=function(e){if("object"!=typeof e||null===e)return"object expected";if(null!=e.amount&&e.hasOwnProperty("amount")&&!($util.isInteger(e.amount)||e.amount&&$util.isInteger(e.amount.low)&&$util.isInteger(e.amount.high)))return"amount: integer|Long expected";var n;if(null!=e.balanceOwner&&e.hasOwnProperty("balanceOwner")&&(n=$root.google.protobuf.StringValue.verify(e.balanceOwner)))return"balanceOwner."+n;if(null!=e.contractId&&e.hasOwnProperty("contractId")&&(n=$root.google.protobuf.StringValue.verify(e.contractId)))return"contractId."+n;if(null!=e.methodName&&e.hasOwnProperty("methodName")&&(n=$root.google.protobuf.BytesValue.verify(e.methodName)))return"methodName."+n;return null},e.fromObject=function(e){if(e instanceof $root.AccessKey)return e;var n=new $root.AccessKey;if(null!=e.amount&&($util.Long?(n.amount=$util.Long.fromValue(e.amount)).unsigned=!0:"string"==typeof e.amount?n.amount=parseInt(e.amount,10):"number"==typeof e.amount?n.amount=e.amount:"object"==typeof e.amount&&(n.amount=new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0))),null!=e.balanceOwner){if("object"!=typeof e.balanceOwner)throw TypeError(".AccessKey.balanceOwner: object expected");n.balanceOwner=$root.google.protobuf.StringValue.fromObject(e.balanceOwner)}if(null!=e.contractId){if("object"!=typeof e.contractId)throw TypeError(".AccessKey.contractId: object expected");n.contractId=$root.google.protobuf.StringValue.fromObject(e.contractId)}if(null!=e.methodName){if("object"!=typeof e.methodName)throw TypeError(".AccessKey.methodName: object expected");n.methodName=$root.google.protobuf.BytesValue.fromObject(e.methodName)}return n},e.toObject=function(e,n){n||(n={});var t={};if(n.defaults){if($util.Long){var o=new $util.Long(0,0,!0);t.amount=n.longs===String?o.toString():n.longs===Number?o.toNumber():o}else t.amount=n.longs===String?"0":0;t.balanceOwner=null,t.contractId=null,t.methodName=null}return null!=e.amount&&e.hasOwnProperty("amount")&&("number"==typeof e.amount?t.amount=n.longs===String?String(e.amount):e.amount:t.amount=n.longs===String?$util.Long.prototype.toString.call(e.amount):n.longs===Number?new $util.LongBits(e.amount.low>>>0,e.amount.high>>>0).toNumber(!0):e.amount),null!=e.balanceOwner&&e.hasOwnProperty("balanceOwner")&&(t.balanceOwner=$root.google.protobuf.StringValue.toObject(e.balanceOwner,n)),null!=e.contractId&&e.hasOwnProperty("contractId")&&(t.contractId=$root.google.protobuf.StringValue.toObject(e.contractId,n)),null!=e.methodName&&e.hasOwnProperty("methodName")&&(t.methodName=$root.google.protobuf.BytesValue.toObject(e.methodName,n)),t},e.prototype.toJSON=function(){return this.constructor.toObject(this,$protobuf.util.toJSONOptions)},e}(),module.exports=$root; },{"protobufjs/minimal":46}],68:[function(require,module,exports){ -const KeyPair=require("./key_pair");class AccountInfo{constructor(e,t,n){this.accountId=e,this.keyPair=t,this.networkId=n}static fromJson(e){if(!(e.public_key&&e.secret_key&&e.account_id&&e.network_id))throw'Invalid account info format. Please ensure it contains public_key, secret_key, and account_id".';return new AccountInfo(e.account_id,new KeyPair(e.public_key,e.secret_key),e.network_id)}toJSON(){return{account_id:this.accountId,public_key:this.keyPair.getPublicKey(),secret_key:this.keyPair.getSecretKey(),network_id:this.networkId}}downloadAsFile(){const e=this.keyFileName,t=JSON.stringify(this.toJSON());var n=document.createElement("a");n.setAttribute("href","data:text/plain;charset=utf-8,"+encodeURIComponent(t)),n.setAttribute("download",e),n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n)}get keyFileName(){return this.networkId+"_"+this.accountId}}module.exports=AccountInfo; +const KeyPair=require("./key_pair");class AccountInfo{constructor(e,t,n){this.accountId=e,this.keyPair=t,this.networkId=n}static fromJson(e){if(!e.public_key||!e.secret_key||!e.account_id)throw'Invalid account info format. Please ensure it contains public_key, secret_key, and account_id".';return new AccountInfo(e.account_id,new KeyPair(e.public_key,e.secret_key),e.network_id)}toJSON(){return{account_id:this.accountId,public_key:this.keyPair.getPublicKey(),secret_key:this.keyPair.getSecretKey(),network_id:this.networkId}}downloadAsFile(){const e=this.keyFileName,t=JSON.stringify(this.toJSON());var n=document.createElement("a");n.setAttribute("href","data:text/plain;charset=utf-8,"+encodeURIComponent(t)),n.setAttribute("download",e),n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n)}get keyFileName(){return this.networkId+"_"+this.accountId}}module.exports=AccountInfo; },{"./key_pair":71}],69:[function(require,module,exports){ -const KeyPair=require("./key_pair"),AccountInfo=require("./account_info"),LOCAL_STORAGE_SECRET_KEY_SUFFIX="_secretkey",LOCAL_STORAGE_PUBLIC_KEY_SUFFIX="_publickey";class BrowserLocalStorageKeystore{constructor(e="unknown",t=window.localStorage){this.networkId=e,this.localStorage=t}static storageKeyForPublicKey(e){return e+"_"+this.networkId+LOCAL_STORAGE_PUBLIC_KEY_SUFFIX}static storageKeyForSecretKey(e){return e+"_"+this.networkId+LOCAL_STORAGE_SECRET_KEY_SUFFIX}async setKey(e,t){this.localStorage.setItem(BrowserLocalStorageKeystore.storageKeyForPublicKey(e),t.getPublicKey()),this.localStorage.setItem(BrowserLocalStorageKeystore.storageKeyForSecretKey(e),t.getSecretKey())}async setKeyFromJson(e){const t=AccountInfo.fromJson(e);if(this.networkId!=t.networkId)throw new Error("Setting key for a wrong network");this.setKey(t.accountId,t.keyPair)}async getKey(e){const t=this.localStorage.getItem(BrowserLocalStorageKeystore.storageKeyForPublicKey(e)),r=this.localStorage.getItem(BrowserLocalStorageKeystore.storageKeyForSecretKey(e));return t&&r?new KeyPair(t,r):null}static getAccounts(){return Object.keys(this.localStorage).map(function(e){if(e.endsWith("_public"))return e.substr(0,e.length()-7)})}}module.exports=BrowserLocalStorageKeystore; +const KeyPair=require("./key_pair"),AccountInfo=require("./account_info"),LOCAL_STORAGE_SECRET_KEY_SUFFIX="_secretkey",LOCAL_STORAGE_PUBLIC_KEY_SUFFIX="_publickey";class BrowserLocalStorageKeystore{constructor(e="unknown",t=window.localStorage){this.networkId=e,this.localStorage=t}static storageKeyForPublicKey(e){return e+"_"+this.networkId+LOCAL_STORAGE_PUBLIC_KEY_SUFFIX}static storageKeyForSecretKey(e){return e+"_"+this.networkId+LOCAL_STORAGE_SECRET_KEY_SUFFIX}async setKey(e,t){this.localStorage.setItem(BrowserLocalStorageKeystore.storageKeyForPublicKey(e),t.getPublicKey()),this.localStorage.setItem(BrowserLocalStorageKeystore.storageKeyForSecretKey(e),t.getSecretKey())}async removeKey(e){this.localStorage.removeItem(BrowserLocalStorageKeystore.storageKeyForPublicKey(e)),this.localStorage.removeItem(BrowserLocalStorageKeystore.storageKeyForSecretKey(e))}async setKeyFromJson(e){const t=AccountInfo.fromJson(e);if(this.networkId!=t.networkId)throw new Error("Setting key for a wrong network");this.setKey(t.accountId,t.keyPair)}async getKey(e){const t=this.localStorage.getItem(BrowserLocalStorageKeystore.storageKeyForPublicKey(e)),r=this.localStorage.getItem(BrowserLocalStorageKeystore.storageKeyForSecretKey(e));return t&&r?new KeyPair(t,r):null}static getAccounts(){return Object.keys(this.localStorage).map(function(e){if(e.endsWith("_public"))return e.substr(0,e.length()-7)})}}module.exports=BrowserLocalStorageKeystore; },{"./account_info":68,"./key_pair":71}],70:[function(require,module,exports){ -class InMemoryKeyStore{constructor(e){this.networkId=e,this.keys={}}async setKey(e,s){this.keys[e+"_"+this.networkId]=s}async getKey(e){return this.keys[e+"_"+this.networkId]}async clear(){this.keys={}}}module.exports=InMemoryKeyStore; +class InMemoryKeyStore{constructor(e){this.networkId=e,this.keys={}}async setKey(e,s){this.keys[e+"_"+this.networkId]=s}async getKey(e){return this.keys[e+"_"+this.networkId]}async removeKey(e){this.getKey(e)&&this.setKey(e,void 0)}async clear(){this.keys={}}}module.exports=InMemoryKeyStore; },{}],71:[function(require,module,exports){ (function (Buffer){ @@ -296,7 +296,7 @@ const KeyPair=require("./signing/key_pair"),BrowserLocalStorageKeystore=require( },{"./signing/browser_local_storage_key_store":69,"./signing/key_pair":71,"./signing/simple_key_store_signer":72}],74:[function(require,module,exports){ (function (Buffer){ -const{sha256:sha256}=require("js-sha256"),{FunctionCallTransaction:FunctionCallTransaction}=require("./protos"),EMBED_WALLET_URL_SUFFIX="/embed/",LOGIN_WALLET_URL_SUFFIX="/login/",RANDOM_ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",REQUEST_ID_LENGTH=32,LOCAL_STORAGE_KEY_SUFFIX="_wallet_auth_key";class WalletAccount{constructor(t,e="https://wallet.nearprotocol.com"){this._walletBaseUrl=e,this._authDataKey=t+LOCAL_STORAGE_KEY_SUFFIX,this._initHtmlElements(),this._signatureRequests={},this._authData=JSON.parse(window.localStorage.getItem(this._authDataKey)||"{}"),this.isSignedIn()||this._tryInitFromUrl()}isSignedIn(){return!!this._authData.accountId}getAccountId(){return this._authData.accountId||""}requestSignIn(t,e,a,s){const r=new URL(window.location.href);let n=new URL(this._walletBaseUrl+LOGIN_WALLET_URL_SUFFIX);n.searchParams.set("title",e),n.searchParams.set("contract_id",t),n.searchParams.set("success_url",a||r.href),n.searchParams.set("failure_url",s||r.href),n.searchParams.set("app_url",r.origin),window.location.replace(n.toString())}signOut(){this._authData={},window.localStorage.removeItem(this._authDataKey)}_tryInitFromUrl(){let t=new URL(window.location.href),e=t.searchParams.get("auth_token")||"",a=t.searchParams.get("account_id")||"";e&&a&&(this._authData={authToken:e,accountId:a},window.localStorage.setItem(this._authDataKey,JSON.stringify(this._authData)))}_initHtmlElements(){const t=document.createElement("iframe");t.style="display: none;",t.src=this._walletBaseUrl+EMBED_WALLET_URL_SUFFIX,document.body.appendChild(t),this._walletWindow=t.contentWindow,window.addEventListener("message",this.receiveMessage.bind(this),!1)}receiveMessage(t){if(!this._walletBaseUrl.startsWith(t.origin))return void console.log("Wallet account ignoring message from "+t.origin);let e;try{e=JSON.parse(t.data)}catch(e){return void console.error("Can't parse the result",t.data,e)}const a=e.request_id||"";if(!(a in this._signatureRequests))return void console.error("Request ID"+a+" was not found");let s=this._signatureRequests[a];delete this._signatureRequests[a],e.success?s.resolve(e.result):s.reject(e.error)}_randomRequestId(){for(var t="",e=0;e{const n=this._randomRequestId();this._signatureRequests[n]={request_id:n,resolve:s,reject:r},this._walletWindow.postMessage(JSON.stringify({action:"sign_transaction",token:this._authData.authToken,method_name:e,args:a||{},hash:t,request_id:n}),this._walletBaseUrl)})}async signBuffer(t,e){if(!this.isSignedIn()||e!==this.getAccountId())throw"Unauthorized account_id "+e;const a=FunctionCallTransaction.decode(t);let s=Buffer.from(a.methodName).toString(),r=JSON.parse(Buffer.from(a.args).toString());return{signature:await this._remoteSign(sha256.array(t),s,r)}}}module.exports=WalletAccount; +const{FunctionCallTransaction:FunctionCallTransaction}=require("./protos"),LOGIN_WALLET_URL_SUFFIX="/login/",LOCAL_STORAGE_KEY_SUFFIX="_wallet_auth_key",PENDING_ACCESS_KEY_PREFIX="pending_key";class WalletAccount{constructor(t,e="https://wallet.nearprotocol.com",a=new nearlib.BrowserLocalStorageKeystore){this._walletBaseUrl=e,this._authDataKey=t+LOCAL_STORAGE_KEY_SUFFIX,this._keyStore=a,this._authData=JSON.parse(window.localStorage.getItem(this._authDataKey)||"{}"),this.isSignedIn()||this._completeSignInWithAccessKey()}isSignedIn(){return!!this._authData.accountId}getAccountId(){return this._authData.accountId||""}requestSignIn(t,e,a,s){const o=new URL(window.location.href);let r=new URL(this._walletBaseUrl+LOGIN_WALLET_URL_SUFFIX);if(r.searchParams.set("title",e),r.searchParams.set("contract_id",t),r.searchParams.set("success_url",a||o.href),r.searchParams.set("failure_url",s||o.href),r.searchParams.set("app_url",o.origin),!this.getAccountId()||!this._keyStore.getKey(this.getAccountId())){const t=nearlib.KeyPair.fromRandomSeed();r.searchParams.set("public_key",t.getPublicKey()),this._keyStore.setKey(PENDING_ACCESS_KEY_PREFIX+t.getPublicKey(),t).then(window.location.replace(r.toString()))}}_completeSignInWithAccessKey(){let t=new URL(window.location.href),e=t.searchParams.get("public_key")||"",a=t.searchParams.get("account_id")||"";a&&e&&this._moveKeyFromTempToPermanent(a,e)}async _moveKeyFromTempToPermanent(t,e){let a=await this._keyStore.getKey(PENDING_ACCESS_KEY_PREFIX+e);this._authData={accountId:t},window.localStorage.setItem(this._authDataKey,JSON.stringify(this._authData)),await this._keyStore.setKey(t,a),await this._keyStore.removeKey(PENDING_ACCESS_KEY_PREFIX+e)}signOut(){this._authData={},window.localStorage.removeItem(this._authDataKey)}async signBuffer(t,e){if(!this.isSignedIn()||e!==this.getAccountId())throw"Unauthorized account_id "+e;const a=FunctionCallTransaction.decode(t);Buffer.from(a.methodName).toString(),JSON.parse(Buffer.from(a.args).toString());const s=new nearlib.SimpleKeyStoreSigner(this._keyStore);let o=await s.signBuffer(t,e);return console.log(o),o}}module.exports=WalletAccount; }).call(this,require("buffer").Buffer) -},{"./protos":67,"buffer":21,"js-sha256":40}]},{},[2]); +},{"./protos":67,"buffer":21}]},{},[2]); diff --git a/signing/browser_local_storage_key_store.js b/signing/browser_local_storage_key_store.js index d784fb440..1074efbd9 100644 --- a/signing/browser_local_storage_key_store.js +++ b/signing/browser_local_storage_key_store.js @@ -35,6 +35,16 @@ class BrowserLocalStorageKeystore { BrowserLocalStorageKeystore.storageKeyForSecretKey(accountId), key.getSecretKey()); } + /** + * Removes the key from local storage for a given id. Please be sure to store the key somewhere before doing this + * to prevent permanent key loss. + * @param {string} accountId + */ + async removeKey(accountId) { + this.localStorage.removeItem(BrowserLocalStorageKeystore.storageKeyForPublicKey(accountId)); + this.localStorage.removeItem(BrowserLocalStorageKeystore.storageKeyForSecretKey(accountId)); + } + async setKeyFromJson(json) { const accountInfo = AccountInfo.fromJson(json); if (this.networkId != accountInfo.networkId) { diff --git a/signing/in_memory_key_store.js b/signing/in_memory_key_store.js index 1f3f42862..a04da44e1 100644 --- a/signing/in_memory_key_store.js +++ b/signing/in_memory_key_store.js @@ -15,6 +15,12 @@ class InMemoryKeyStore { return this.keys[accountId + '_' + this.networkId]; } + async removeKey(accountId) { + if (this.getKey(accountId)) { + this.setKey(accountId, undefined); + } + } + async clear() { this.keys = {}; } diff --git a/signing/unencrypted_file_system_keystore.js b/signing/unencrypted_file_system_keystore.js index 637546dde..ff9d4a164 100644 --- a/signing/unencrypted_file_system_keystore.js +++ b/signing/unencrypted_file_system_keystore.js @@ -39,6 +39,12 @@ class UnencryptedFileSystemKeyStore { return AccountInfo.fromJson(json).keyPair; } + async removeKey(accountId) { + if (await promisify(fs.exists)(this.getKeyFilePath(accountId))) { + fs.unlink(this.getKeyFilePath(accountId)); + } + } + /** * Returns all account ids for a particular network. */ diff --git a/wallet-account.js b/wallet-account.js index d6b031df0..e4e549369 100644 --- a/wallet-account.js +++ b/wallet-account.js @@ -2,15 +2,12 @@ * Wallet based account and signer that uses external wallet through the iframe to sign transactions. */ -const { sha256 } = require('js-sha256'); const { FunctionCallTransaction } = require('./protos'); -const EMBED_WALLET_URL_SUFFIX = '/embed/'; const LOGIN_WALLET_URL_SUFFIX = '/login/'; -const RANDOM_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; -const REQUEST_ID_LENGTH = 32; const LOCAL_STORAGE_KEY_SUFFIX = '_wallet_auth_key'; +const PENDING_ACCESS_KEY_PREFIX = 'pending_key'; // browser storage key for a pending access key (i.e. key has been generated but we are not sure it was added yet) /** * Wallet based account and signer that uses external wallet through the iframe to sign transactions. @@ -24,16 +21,15 @@ const LOCAL_STORAGE_KEY_SUFFIX = '_wallet_auth_key'; */ class WalletAccount { - constructor(appKeyPrefix, walletBaseUrl = 'https://wallet.nearprotocol.com') { - this._walletBaseUrl = walletBaseUrl; + constructor(appKeyPrefix, walletBaseUrl = 'https://wallet.nearprotocol.com', keyStore = new nearlib.BrowserLocalStorageKeystore()) { + this._walletBaseUrl = walletBaseUrl; this._authDataKey = appKeyPrefix + LOCAL_STORAGE_KEY_SUFFIX; + this._keyStore = keyStore; - this._initHtmlElements(); - this._signatureRequests = {}; this._authData = JSON.parse(window.localStorage.getItem(this._authDataKey) || '{}'); if (!this.isSignedIn()) { - this._tryInitFromUrl(); + this._completeSignInWithAccessKey(); } } @@ -76,99 +72,43 @@ class WalletAccount { newUrl.searchParams.set('success_url', success_url || currentUrl.href); newUrl.searchParams.set('failure_url', failure_url || currentUrl.href); newUrl.searchParams.set('app_url', currentUrl.origin); - window.location.replace(newUrl.toString()); + if (!this.getAccountId() || !this._keyStore.getKey(this.getAccountId())) { + const accessKey = nearlib.KeyPair.fromRandomSeed(); + newUrl.searchParams.set('public_key', accessKey.getPublicKey()); + this._keyStore.setKey(PENDING_ACCESS_KEY_PREFIX + accessKey.getPublicKey(), accessKey).then(window.location.replace(newUrl.toString())); + } } + /** - * Sign out from the current account - * @example - * walletAccount.signOut(); + * Complete sign in for a given account id and public key. To be invoked by the app when getting a callback from the wallet. */ - signOut() { - this._authData = {}; - window.localStorage.removeItem(this._authDataKey); - } - - _tryInitFromUrl() { + _completeSignInWithAccessKey() { let currentUrl = new URL(window.location.href); - let authToken = currentUrl.searchParams.get('auth_token') || ''; + let publicKey = currentUrl.searchParams.get('public_key') || ''; let accountId = currentUrl.searchParams.get('account_id') || ''; - if (!!authToken && !!accountId) { - this._authData = { - authToken, - accountId, - }; - window.localStorage.setItem(this._authDataKey, JSON.stringify(this._authData)); + if (accountId && publicKey) { + this._moveKeyFromTempToPermanent(accountId, publicKey); } } - _initHtmlElements() { - // Wallet iframe - const iframe = document.createElement('iframe'); - iframe.style = 'display: none;'; - iframe.src = this._walletBaseUrl + EMBED_WALLET_URL_SUFFIX; - document.body.appendChild(iframe); - this._walletWindow = iframe.contentWindow; - - // Message Event - window.addEventListener('message', this.receiveMessage.bind(this), false); - } - - receiveMessage(event) { - if (!this._walletBaseUrl.startsWith(event.origin)) { - // Only processing wallet messages. - console.log('Wallet account ignoring message from ' + event.origin); - return; - } - let data; - try { - data = JSON.parse(event.data); - } catch (e) { - console.error('Can\'t parse the result', event.data, e); - return; - } - const request_id = data.request_id || ''; - if (!(request_id in this._signatureRequests)) { - console.error('Request ID' + request_id + ' was not found'); - return; - } - let signatureRequest = this._signatureRequests[request_id]; - delete this._signatureRequests[request_id]; - - if (data.success) { - signatureRequest.resolve(data.result); - } else { - signatureRequest.reject(data.error); - } - } - - _randomRequestId() { - var result = ''; - - for (var i = 0; i < REQUEST_ID_LENGTH; i++) { - result += RANDOM_ALPHABET.charAt(Math.floor(Math.random() * RANDOM_ALPHABET.length)); - } - - return result; + async _moveKeyFromTempToPermanent(accountId, publicKey) { + let keyPair = await this._keyStore.getKey(PENDING_ACCESS_KEY_PREFIX + publicKey); + this._authData = { + accountId + }; + window.localStorage.setItem(this._authDataKey, JSON.stringify(this._authData)); + await this._keyStore.setKey(accountId, keyPair); + await this._keyStore.removeKey(PENDING_ACCESS_KEY_PREFIX + publicKey); } - _remoteSign(hash, methodName, args) { - // TODO(#482): Add timeout. - return new Promise((resolve, reject) => { - const request_id = this._randomRequestId(); - this._signatureRequests[request_id] = { - request_id, - resolve, - reject, - }; - this._walletWindow.postMessage(JSON.stringify({ - action: 'sign_transaction', - token: this._authData.authToken, - method_name: methodName, - args: args || {}, - hash, - request_id, - }), this._walletBaseUrl); - }); + /** + * Sign out from the current account + * @example + * walletAccount.signOut(); + */ + signOut() { + this._authData = {}; + window.localStorage.removeItem(this._authDataKey); } /** @@ -184,10 +124,10 @@ class WalletAccount { const body = FunctionCallTransaction.decode(buffer); let methodName = Buffer.from(body.methodName).toString(); let args = JSON.parse(Buffer.from(body.args).toString()); - let signature = await this._remoteSign(sha256.array(buffer), methodName, args); - return { - signature, - }; + const signer = new nearlib.SimpleKeyStoreSigner(this._keyStore); + let signature = await signer.signBuffer(buffer, originator); + console.log(signature); + return signature; } }