UNPKG

apostille-library

Version:

A novel & holistic blockchain notarization and timestamping with transferable, updatable, branded, and conjointly owned notarizations.

111 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const nem2_sdk_1 = require("nem2-sdk"); const Errors_1 = require("../types/Errors"); var initiatorAccountType; (function (initiatorAccountType) { initiatorAccountType[initiatorAccountType["ACCOUNT"] = 0] = "ACCOUNT"; initiatorAccountType[initiatorAccountType["MULTISIG_ACCOUNT"] = 1] = "MULTISIG_ACCOUNT"; initiatorAccountType[initiatorAccountType["HARDWARE_WALLET"] = 2] = "HARDWARE_WALLET"; })(initiatorAccountType = exports.initiatorAccountType || (exports.initiatorAccountType = {})); class Initiator { constructor(account, accountType = initiatorAccountType.ACCOUNT, multiSigAccount) { this.account = account; this.accountType = accountType; this.multiSigAccount = multiSigAccount; if (accountType === initiatorAccountType.ACCOUNT) { if (account instanceof nem2_sdk_1.PublicAccount) { throw Error(Errors_1.Errors[Errors_1.Errors.INITIATOR_TYPE_ACCOUNT_REQUIRE_ACCOUNT]); } } if (accountType === initiatorAccountType.MULTISIG_ACCOUNT) { if (multiSigAccount === undefined) { throw Error(Errors_1.Errors[Errors_1.Errors.INITIATOR_TYPE_MULTISIG_REQUIRE_MULTISIG_INITIATOR]); } if (multiSigAccount.cosignatories.length < 1) { throw Error(Errors_1.Errors[Errors_1.Errors.INITIATOR_TYPE_MULTISIG_REQUIRE_AT_LEAST_ONE_COSIGNER]); } } } get complete() { switch (this.accountType) { case initiatorAccountType.ACCOUNT: return this._isAccountComplete(); case initiatorAccountType.HARDWARE_WALLET: return false; case initiatorAccountType.MULTISIG_ACCOUNT: return this._isMultiSigAccountComplete(); } } get publicAccount() { if (this.account instanceof nem2_sdk_1.Account) { return this.account.publicAccount; } return this.account; } sign(transaction, generationHash) { if (this.account.address.networkType !== transaction.networkType) { throw Error(Errors_1.Errors[Errors_1.Errors.NETWORK_TYPE_MISMATCHED]); } if (this.accountType === initiatorAccountType.ACCOUNT) { if (this.account instanceof nem2_sdk_1.Account) { if (!(transaction instanceof nem2_sdk_1.AggregateTransaction)) { return this.account.sign(transaction, generationHash); } } } else if (this.accountType === initiatorAccountType.MULTISIG_ACCOUNT && this.account instanceof nem2_sdk_1.PublicAccount) { const [firstCosigner, ...cosigners] = this.multiSigAccount.cosignatories; let aggregateTransaction; if (transaction instanceof nem2_sdk_1.TransferTransaction || transaction instanceof nem2_sdk_1.ModifyMultisigAccountTransaction) { const refreshedTransaction = transaction.reapplyGiven(nem2_sdk_1.Deadline.create()); if (this.complete) { aggregateTransaction = nem2_sdk_1.AggregateTransaction.createComplete(nem2_sdk_1.Deadline.create(), [refreshedTransaction.toAggregate(this.account)], this.account.address.networkType, []); } else { aggregateTransaction = nem2_sdk_1.AggregateTransaction.createBonded(nem2_sdk_1.Deadline.create(), [refreshedTransaction.toAggregate(this.account)], this.account.address.networkType, []); } } else if (transaction instanceof nem2_sdk_1.AggregateTransaction) { aggregateTransaction = transaction; } else if (transaction instanceof nem2_sdk_1.LockFundsTransaction) { return firstCosigner.sign(transaction, generationHash); } if (aggregateTransaction !== undefined) { return firstCosigner.signTransactionWithCosignatories(aggregateTransaction, cosigners, generationHash); } } throw Error(Errors_1.Errors[Errors_1.Errors.INITIATOR_UNABLE_TO_SIGN]); } canSign() { switch (this.accountType) { case initiatorAccountType.ACCOUNT: return this._isAccountComplete(); case initiatorAccountType.HARDWARE_WALLET: return false; case initiatorAccountType.MULTISIG_ACCOUNT: return this.multiSigAccount.cosignatories.length > 0; } } signFileHash(apostille, data, hashFunction) { if (hashFunction && this.account instanceof nem2_sdk_1.Account) { const hashedData = hashFunction.signedHashing(data, this.account.privateKey, this.account.address.networkType); return { initiator: this, transaction: apostille.update(hashedData), }; } throw Errors_1.Errors[Errors_1.Errors.REQUIRE_INITIATOR_TYPE_ACCOUNT]; } _isAccountComplete() { return this.account instanceof nem2_sdk_1.Account; } _isMultiSigAccountComplete() { return this.multiSigAccount.isComplete; } } exports.Initiator = Initiator; //# sourceMappingURL=Initiator.js.map