@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
149 lines (133 loc) • 5.22 kB
JavaScript
import _typeof from "@babel/runtime-corejs3/helpers/typeof";
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
import _WeakMap from "@babel/runtime-corejs3/core-js-stable/weak-map";
import _indexOfInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/index-of";
import _Promise from "@babel/runtime-corejs3/core-js-stable/promise";
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* Memory Account module
* @module @aeternity/aepp-sdk/es/account/memory
* @export MemoryAccount
* @example import { MemoryAccount } from '@aeternity/aepp-sdk'
*/
import AccountBase from './base';
import * as Crypto from '../utils/crypto';
import { isHex } from '../utils/string';
import { decode } from '../tx/builder/helpers';
var secrets = new _WeakMap();
function sign(_x) {
return _sign.apply(this, arguments);
}
function _sign() {
_sign = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(data) {
return _regeneratorRuntime.wrap(function _callee$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (!this.isGa) {
_context2.next = 2;
break;
}
throw new Error('You are trying to sign data using GA account without keypair');
case 2:
return _context2.abrupt("return", _Promise.resolve(Crypto.sign(data, secrets.get(this).secretKey)));
case 3:
case "end":
return _context2.stop();
}
}
}, _callee, this);
}));
return _sign.apply(this, arguments);
}
function address() {
return _address.apply(this, arguments);
}
function _address() {
_address = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var opt,
_args2 = arguments;
return _regeneratorRuntime.wrap(function _callee2$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
opt = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {
format: Crypto.ADDRESS_FORMAT.api
};
return _context3.abrupt("return", _Promise.resolve(Crypto.formatAddress(opt.format, secrets.get(this).publicKey)));
case 2:
case "end":
return _context3.stop();
}
}
}, _callee2, this);
}));
return _address.apply(this, arguments);
}
function setSecret(keyPair) {
secrets.set(this, {
secretKey: Buffer.isBuffer(keyPair.secretKey) ? keyPair.secretKey : Buffer.from(keyPair.secretKey, 'hex'),
publicKey: keyPair.publicKey
});
}
function validateKeyPair(keyPair) {
var _context;
if (!keyPair || _typeof(keyPair) !== 'object') throw new Error('KeyPair must be an object');
if (!keyPair.secretKey || !keyPair.publicKey) throw new Error('KeyPair must must have "secretKey", "publicKey" properties');
if (typeof keyPair.publicKey !== 'string' || _indexOfInstanceProperty(_context = keyPair.publicKey).call(_context, 'ak_') === -1) throw new Error('Public Key must be a base58c string with "ak_" prefix');
if (!Buffer.isBuffer(keyPair.secretKey) && typeof keyPair.secretKey === 'string' && !isHex(keyPair.secretKey)) throw new Error('Secret key must be hex string or Buffer');
var pubBuffer = Buffer.from(decode(keyPair.publicKey, 'ak'));
if (!Crypto.isValidKeypair(Buffer.from(keyPair.secretKey, 'hex'), pubBuffer)) throw new Error('Invalid Key Pair');
}
/**
* In-memory account stamp
* @function
* @alias module:@aeternity/aepp-sdk/es/account/memory
* @rtype Stamp
* @param {Object} [options={}] - Initializer object
* @param {Object} options.keypair - Key pair to use
* @param {String} options.keypair.publicKey - Public key
* @param {String} options.keypair.secretKey - Private key
* @return {Account}
*/
export default AccountBase.compose({
init: function init(_ref) {
var keypair = _ref.keypair,
gaId = _ref.gaId;
this.isGa = !!gaId;
if (gaId) {
if (!Crypto.isAddressValid(gaId)) throw new Error('Invalid GA address');
secrets.set(this, {
publicKey: gaId
});
} else {
validateKeyPair(keypair);
this.setSecret(keypair);
}
},
props: {
isGa: false
},
methods: {
sign: sign,
address: address,
setSecret: setSecret
}
});
//# sourceMappingURL=memory.js.map