@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
262 lines (238 loc) • 8.16 kB
JavaScript
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
/*
* 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.
*/
/**
* AccountBase module
* @module @aeternity/aepp-sdk/es/account/base
* @export AccountBase
* @export isAccountBase
*/
import stampit from '@stamp/it';
import { required } from '@stamp/required';
import { hash, personalMessageToBinary, decodeBase64Check, assertedType, verifyPersonalMessage } from '../utils/crypto';
import { buildTx, buildTxHash } from '../tx/builder';
import { decode } from '../tx/builder/helpers';
import { TX_TYPE } from '../tx/builder/schema';
import { getNetworkId } from '../node';
/**
* Check is provided object looks like an instance of AccountBase
* @rtype (Object) => Boolean
* @param {Object} acc - Object to check
* @return {Boolean}
*/
export var isAccountBase = function isAccountBase(acc) {
var _context;
return !_findInstanceProperty(_context = ['sign', 'address']).call(_context, function (f) {
return typeof acc[f] !== 'function';
});
};
/**
* Sign encoded transaction
* @instance
* @category async
* @rtype (tx: String) => tx: Promise[String], throws: Error
* @param {String} tx - Transaction to sign
* @param {Object} opt - Options
* @return {String} Signed transaction
*/
function signTransaction(_x) {
return _signTransaction.apply(this, arguments);
}
/**
* Get network Id
* @instance
* @function getNetworkId
* @category async
* @rtype () => networkId: String
* @return {String} Network Id
*/
/**
* Sign message
* @instance
* @category async
* @rtype (msg: String) => signature: Promise[String], throws: Error
* @param {String} message - Message to sign
* @param {Object} opt - Options
* @return {String} Signature
*/
function _signTransaction() {
_signTransaction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(tx) {
var opt,
networkId,
rlpBinaryTx,
txWithNetworkId,
signatures,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
opt = _args.length > 1 && _args[1] !== undefined ? _args[1] : {
signHash: true
};
networkId = this.getNetworkId(opt);
rlpBinaryTx = decodeBase64Check(assertedType(tx, 'tx')); // Prepend `NETWORK_ID` to begin of data binary
txWithNetworkId = _concatInstanceProperty(Buffer).call(Buffer, [Buffer.from(networkId), opt.signHash ? buildTxHash(rlpBinaryTx, {
raw: true
}) : rlpBinaryTx]);
_context2.next = 6;
return this.sign(txWithNetworkId, opt);
case 6:
_context2.t0 = _context2.sent;
signatures = [_context2.t0];
return _context2.abrupt("return", buildTx({
encodedTx: rlpBinaryTx,
signatures: signatures
}, TX_TYPE.signed).tx);
case 9:
case "end":
return _context2.stop();
}
}
}, _callee, this);
}));
return _signTransaction.apply(this, arguments);
}
function signMessage(_x2) {
return _signMessage.apply(this, arguments);
}
/**
* Verify message
* @instance
* @category async
* @rtype (msg: String, signature: String, publicKey: String) => signature: Promise[String], throws: Error
* @param {String} message - Message to verify
* @param {String} signature - Signature
* @param {Object} opt - Options
* @return {Boolean}
*/
function _signMessage() {
_signMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(message) {
var opt,
sig,
_args2 = arguments;
return _regeneratorRuntime.wrap(function _callee2$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
opt = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {
returnHex: false
};
_context3.next = 3;
return this.sign(hash(personalMessageToBinary(message)), opt);
case 3:
sig = _context3.sent;
return _context3.abrupt("return", opt.returnHex ? Buffer.from(sig).toString('hex') : sig);
case 5:
case "end":
return _context3.stop();
}
}
}, _callee2, this);
}));
return _signMessage.apply(this, arguments);
}
function verifyMessage(_x3, _x4) {
return _verifyMessage.apply(this, arguments);
}
/**
* AccountBase Stamp
*
* Attempting to create instances from the Stamp without overwriting all
* abstract methods using composition will result in an exception.
*
* Account is one of the three basic building blocks of an
* {@link module:@aeternity/aepp-sdk/es/ae--Ae} client and provides access to a
* signing key pair.
* @function
* @alias module:@aeternity/aepp-sdk/es/account
* @rtype Stamp
* @param {Object} [options={}] - Initializer object
* @param {String} options.networkId - NETWORK_ID using for signing transaction's
* @return {Object} Account instance
*/
function _verifyMessage() {
_verifyMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(message, signature) {
var opt,
_args3 = arguments;
return _regeneratorRuntime.wrap(function _callee3$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
opt = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : {};
_context4.t0 = verifyPersonalMessage;
_context4.t1 = message;
_context4.t2 = typeof signature === 'string' ? Buffer.from(signature, 'hex') : signature;
_context4.t3 = decode;
_context4.next = 7;
return this.address(opt);
case 7:
_context4.t4 = _context4.sent;
_context4.t5 = (0, _context4.t3)(_context4.t4);
return _context4.abrupt("return", (0, _context4.t0)(_context4.t1, _context4.t2, _context4.t5));
case 10:
case "end":
return _context4.stop();
}
}
}, _callee3, this);
}));
return _verifyMessage.apply(this, arguments);
}
export default stampit({
init: function init(_ref) {
var networkId = _ref.networkId;
// NETWORK_ID using for signing transaction's
if (!this.networkId && networkId) {
this.networkId = networkId;
}
},
methods: {
signTransaction: signTransaction,
getNetworkId: getNetworkId,
signMessage: signMessage,
verifyMessage: verifyMessage
}
}, required({
methods: {
sign: required,
address: required
}
}));
/**
* Sign data blob
* @function sign
* @instance
* @abstract
* @category async
* @rtype (data: String) => data: Promise[String]
* @param {String} data - Data blob to sign
* @return {String} Signed data blob
*/
/**
* Obtain account address
* @function address
* @instance
* @abstract
* @category async
* @rtype () => address: Promise[String]
* @return {String} Public account address
*/
//# sourceMappingURL=base.js.map