@hiero-ledger/sdk
Version:
252 lines (231 loc) • 8.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _PrivateKey = _interopRequireDefault(require("./PrivateKey.cjs"));
var _AccountId = _interopRequireDefault(require("./account/AccountId.cjs"));
var _SignerSignature = _interopRequireDefault(require("./SignerSignature.cjs"));
var _AccountBalanceQuery = _interopRequireDefault(require("./account/AccountBalanceQuery.cjs"));
var _AccountInfoQuery = _interopRequireDefault(require("./account/AccountInfoQuery.cjs"));
var _AccountRecordsQuery = _interopRequireDefault(require("./account/AccountRecordsQuery.cjs"));
var _TransactionId = _interopRequireDefault(require("./transaction/TransactionId.cjs"));
var util = _interopRequireWildcard(require("./util.cjs"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// SPDX-License-Identifier: Apache-2.0
/**
* @template RequestT
* @template ResponseT
* @template OutputT
* @typedef {import("./Executable.js").default<RequestT, ResponseT, OutputT>} Executable<RequestT, ResponseT, OutputT>
*/
/**
* @typedef {import("./Signer.js").Signer} Signer
* @typedef {import("./Provider.js").Provider} Provider
* @typedef {import("./LedgerId.js").default} LedgerId
* @typedef {import("./Key.js").default} Key
* @typedef {import("./transaction/Transaction.js").default} Transaction
* @typedef {import("./transaction/TransactionResponse.js").default} TransactionResponse
* @typedef {import("./transaction/TransactionReceipt.js").default} TransactionReceipt
* @typedef {import("./transaction/TransactionRecord.js").default} TransactionRecord
* @typedef {import("./account/AccountBalance.js").default} AccountBalance
* @typedef {import("./account/AccountInfo.js").default} AccountInfo
*/
/**
* @template {any} O
* @typedef {import("./query/Query.js").default<O>} Query<O>
*/
/**
* @implements {Signer}
*/
class Wallet {
/**
* NOTE: When using string for private key, the string needs to contain DER headers
*
* @param {AccountId | string} accountId
* @param {PrivateKey | string} privateKey
* @param {Provider=} provider
*/
constructor(accountId, privateKey, provider) {
/*
TODO: deprecate on a major version
the following lines were added because we didnt have
a way to check the algorithm of a der encoded private key
We need to keep the old behavior for the transition period.
*/
let key;
if (typeof privateKey === "string" && _PrivateKey.default.isDerKey(privateKey)) {
key = _PrivateKey.default.fromStringDer(privateKey);
} else if (typeof privateKey === "string") {
// eslint-disable-next-line deprecation/deprecation
key = _PrivateKey.default.fromString(privateKey);
} else {
key = privateKey;
}
this.publicKey = key.publicKey;
/**
* @type {(message: Uint8Array) => Promise<Uint8Array>}
*/
this.signer = message => Promise.resolve(key.sign(message));
this.provider = provider;
this.accountId = typeof accountId === "string" ? _AccountId.default.fromString(accountId) : accountId;
}
/**
* @returns {Promise<Wallet>}
* @deprecated
*/
static createRandomED25519() {
const privateKey = _PrivateKey.default.generateED25519();
const publicKey = privateKey.publicKey;
const accountId = publicKey.toAccountId(0, 0);
return Promise.resolve(new Wallet(accountId, privateKey));
}
/**
* @returns {Promise<Wallet>}
* @deprecated
*/
static createRandomECDSA() {
const privateKey = _PrivateKey.default.generateECDSA();
const publicKey = privateKey.publicKey;
const accountId = publicKey.toAccountId(0, 0);
return Promise.resolve(new Wallet(accountId, privateKey));
}
/**
* @returns {Provider=}
*/
getProvider() {
return this.provider;
}
/**
* @abstract
* @returns {AccountId}
*/
getAccountId() {
return this.accountId;
}
/**
* @returns {Key}
*/
getAccountKey() {
return this.publicKey;
}
/**
* @returns {LedgerId?}
*/
getLedgerId() {
return this.provider == null ? null : this.provider.getLedgerId();
}
/**
* @abstract
* @returns {{[key: string]: (string | AccountId)}}
*/
getNetwork() {
return this.provider == null ? {} : this.provider.getNetwork();
}
/**
* @abstract
* @returns {string[]}
*/
getMirrorNetwork() {
return this.provider == null ? [] : this.provider.getMirrorNetwork();
}
/**
* @param {Uint8Array[]} messages
* @returns {Promise<SignerSignature[]>}
*/
async sign(messages) {
const sigantures = [];
for (const message of messages) {
sigantures.push(new _SignerSignature.default({
publicKey: this.publicKey,
signature: await this.signer(message),
accountId: this.accountId
}));
}
return sigantures;
}
/**
* @returns {Promise<AccountBalance>}
*/
getAccountBalance() {
return this.call(new _AccountBalanceQuery.default().setAccountId(this.accountId));
}
/**
* @abstract
* @returns {Promise<AccountInfo>}
*/
getAccountInfo() {
return this.call(new _AccountInfoQuery.default().setAccountId(this.accountId));
}
/**
* @abstract
* @returns {Promise<TransactionRecord[]>}
*/
getAccountRecords() {
return this.call(new _AccountRecordsQuery.default().setAccountId(this.accountId));
}
/**
* @template {Transaction} T
* @param {T} transaction
* @returns {Promise<T>}
*/
signTransaction(transaction) {
return transaction.signWith(this.publicKey, this.signer);
}
/**
* @template {Transaction} T
* @param {T} transaction
* @returns {Promise<T>}
*/
checkTransaction(transaction) {
const transactionId = transaction.transactionId;
if (transactionId != null && transactionId.accountId != null && transactionId.accountId.compare(this.accountId) != 0) {
throw new Error("transaction's ID constructed with a different account ID");
}
if (this.provider == null) {
return Promise.resolve(transaction);
}
const nodeAccountIds = (transaction.nodeAccountIds != null ? transaction.nodeAccountIds : []).map(nodeAccountId => nodeAccountId.toString());
const network = Object.values(this.provider.getNetwork()).map(nodeAccountId => nodeAccountId.toString());
if (!nodeAccountIds.reduce((previous, current) => previous && network.includes(current), true)) {
throw new Error("Transaction already set node account IDs to values not within the current network");
}
return Promise.resolve(transaction);
}
/**
* @template {Transaction} T
* @param {T} transaction
* @returns {Promise<T>}
*/
populateTransaction(transaction) {
transaction._freezeWithAccountId(this.accountId);
if (transaction.transactionId == null) {
transaction.setTransactionId(_TransactionId.default.generate(this.accountId));
}
if (transaction.nodeAccountIds != null && transaction.nodeAccountIds.length != 0) {
return Promise.resolve(transaction.freeze());
}
if (this.provider == null) {
return Promise.resolve(transaction);
}
const nodeAccountIds = Object.values(this.provider.getNetwork()).map(id => typeof id === "string" ? _AccountId.default.fromString(id) : id);
util.shuffle(nodeAccountIds);
transaction.setNodeAccountIds(nodeAccountIds.slice(0, (nodeAccountIds.length + 3 - 1) / 3));
return Promise.resolve(transaction.freeze());
}
/**
* @template RequestT
* @template ResponseT
* @template OutputT
* @param {Executable<RequestT, ResponseT, OutputT>} request
* @returns {Promise<OutputT>}
*/
call(request) {
if (this.provider == null) {
throw new Error("cannot send request with an wallet that doesn't contain a provider");
}
return this.provider.call(request._setOperatorWith(this.accountId, this.publicKey, this.signer));
}
}
exports.default = Wallet;