PHP WebShell
Текущая директория: /opt/BitGoJS/node_modules/@hashgraph/sdk/lib/account
Просмотр файла: AccountId.cjs
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _long = _interopRequireDefault(require("long"));
var entity_id = _interopRequireWildcard(require("../EntityIdHelper.cjs"));
var HashgraphProto = _interopRequireWildcard(require("@hashgraph/proto"));
var _Key = _interopRequireDefault(require("../Key.cjs"));
var _PublicKey = _interopRequireDefault(require("../PublicKey.cjs"));
var _Cache = _interopRequireDefault(require("../Cache.cjs"));
var _EvmAddress = _interopRequireDefault(require("../EvmAddress.cjs"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*-
*
* Hedera JavaScript SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* @typedef {import("../client/Client.js").default<*, *>} Client
*/
/**
* The ID for a crypto-currency account on Hedera.
*/
class AccountId {
/**
* @param {number | Long | import("../EntityIdHelper").IEntityId} props
* @param {(number | Long)=} realm
* @param {(number | Long)=} num
* @param {(PublicKey)=} aliasKey
* @param {(EvmAddress)=} evmAddress
*/
constructor(props, realm, num, aliasKey, evmAddress) {
const result = entity_id.constructor(props, realm, num);
this.shard = result.shard;
this.realm = result.realm;
this.num = result.num;
this.aliasKey = aliasKey != null ? aliasKey : null;
this.evmAddress = evmAddress != null ? evmAddress : null;
/**
* @type {string | null}
*/
this._checksum = null;
}
/**
* @description Accepts the following formats as string:
* - as stand alone nubmers
* - as shard.realm.num
* - as shard.realm.hex (wo 0x prefix)
* - hex (w/wo 0x prefix)
* @param {string} text
* @returns {AccountId}
*/
static fromString(text) {
let shard = _long.default.ZERO;
let realm = _long.default.ZERO;
let num = _long.default.ZERO;
let aliasKey = undefined;
let evmAddress = undefined;
if (text.startsWith("0x") && text.length == 42 || text.length == 40) {
evmAddress = _EvmAddress.default.fromString(text);
} else {
const result = entity_id.fromStringSplitter(text);
if (Number.isNaN(result.shard) || Number.isNaN(result.realm)) {
throw new Error("invalid format for entity ID");
}
if (result.shard != null) shard = _long.default.fromString(result.shard);
if (result.realm != null) realm = _long.default.fromString(result.realm);
if (result.numOrHex.length < 20) {
num = _long.default.fromString(result.numOrHex);
} else if (result.numOrHex.length == 40) {
evmAddress = _EvmAddress.default.fromString(result.numOrHex);
} else {
aliasKey = _PublicKey.default.fromString(result.numOrHex);
}
}
return new AccountId(shard, realm, num, aliasKey, evmAddress);
}
/**
* @param {Long | number} shard
* @param {Long | number} realm
* @param {EvmAddress | string} evmAddress
* @returns {AccountId}
*/
static fromEvmAddress(shard, realm, evmAddress) {
return new AccountId(shard, realm, 0, undefined, typeof evmAddress === "string" ? _EvmAddress.default.fromString(evmAddress) : evmAddress);
}
/**
* @summary Accepts an evm address only as `EvmAddress` type
* @param {EvmAddress} evmAddress
* @returns {AccountId}
*/
static fromEvmPublicAddress(evmAddress) {
return new AccountId(0, 0, 0, undefined, evmAddress);
}
/**
* @internal
* @param {HashgraphProto.proto.IAccountID} id
* @returns {AccountId}
*/
static _fromProtobuf(id) {
let aliasKey = undefined;
let evmAddress = undefined;
if (id.alias != null) {
if (id.alias.length === 20) {
evmAddress = _EvmAddress.default.fromBytes(id.alias);
} else {
aliasKey = _Key.default._fromProtobufKey(HashgraphProto.proto.Key.decode(id.alias));
}
}
if (!(aliasKey instanceof _PublicKey.default)) {
aliasKey = undefined;
}
return new AccountId(id.shardNum != null ? id.shardNum : 0, id.realmNum != null ? id.realmNum : 0, id.accountNum != null ? id.accountNum : 0, aliasKey, evmAddress);
}
/**
* @returns {string | null}
*/
get checksum() {
return this._checksum;
}
/**
* @returns {?EvmAddress}
*/
getEvmAddress() {
return this.evmAddress;
}
/**
* @deprecated - Use `validateChecksum` instead
* @param {Client} client
*/
validate(client) {
console.warn("Deprecated: Use `validateChecksum` instead");
this.validateChecksum(client);
}
/**
* @param {Client} client
*/
validateChecksum(client) {
if (this.aliasKey != null) {
throw new Error("cannot calculate checksum with an account ID that has a aliasKey");
}
entity_id.validateChecksum(this.shard, this.realm, this.num, this._checksum, client);
}
/**
* @param {Uint8Array} bytes
* @returns {AccountId}
*/
static fromBytes(bytes) {
return AccountId._fromProtobuf(HashgraphProto.proto.AccountID.decode(bytes));
}
/**
* @param {string} address
* @returns {AccountId}
*/
static fromSolidityAddress(address) {
return new AccountId(...entity_id.fromSolidityAddress(address));
}
/**
* @returns {string}
*/
toSolidityAddress() {
return entity_id.toSolidityAddress([this.shard, this.realm, this.num]);
}
//TODO remove the comments after we get to HIP-631
/**
* @internal
* @returns {HashgraphProto.proto.IAccountID}
*/
_toProtobuf() {
let alias = null;
//let evmAddress = null;
if (this.aliasKey != null) {
alias = HashgraphProto.proto.Key.encode(this.aliasKey._toProtobufKey()).finish();
} else if (this.evmAddress != null) {
alias = this.evmAddress._bytes;
}
/* if (this.evmAddress != null) {
evmAddress = this.evmAddress._bytes;
} */
return {
alias,
accountNum: this.aliasKey != null ? null : this.num,
shardNum: this.shard,
realmNum: this.realm
//evmAddress,
};
}
/**
* @returns {Uint8Array}
*/
toBytes() {
return HashgraphProto.proto.AccountID.encode(this._toProtobuf()).finish();
}
/**
* @returns {string}
*/
toString() {
let account = this.num.toString();
if (this.aliasKey != null) {
account = this.aliasKey.toString();
} else if (this.evmAddress != null) {
account = this.evmAddress.toString();
}
return `${this.shard.toString()}.${this.realm.toString()}.${account}`;
}
/**
* @param {Client} client
* @returns {string}
*/
toStringWithChecksum(client) {
if (this.aliasKey != null) {
throw new Error("cannot calculate checksum with an account ID that has a aliasKey");
}
return entity_id.toStringWithChecksum(this.toString(), client);
}
/**
* @param {this} other
* @returns {boolean}
*/
equals(other) {
let account = false;
if (this.aliasKey != null && other.aliasKey != null) {
account = this.aliasKey.equals(other.aliasKey);
} else if (this.evmAddress != null && other.evmAddress != null) {
account = this.evmAddress.equals(other.evmAddress);
} else if (this.aliasKey == null && other.aliasKey == null && this.evmAddress == null && other.evmAddress == null) {
account = this.num.eq(other.num);
}
return this.shard.eq(other.shard) && this.realm.eq(other.realm) && account;
}
/**
* @returns {AccountId}
*/
clone() {
const id = new AccountId(this);
id._checksum = this._checksum;
id.aliasKey = this.aliasKey;
id.evmAddress = this.evmAddress;
return id;
}
/**
* @param {AccountId} other
* @returns {number}
*/
compare(other) {
let comparison = this.shard.compare(other.shard);
if (comparison != 0) {
return comparison;
}
comparison = this.realm.compare(other.realm);
if (comparison != 0) {
return comparison;
}
if (this.aliasKey != null && other.aliasKey != null) {
const t = this.aliasKey.toString();
const o = other.aliasKey.toString();
if (t > o) {
return 1;
} else if (t < o) {
return -1;
} else {
return 0;
}
} else if (this.evmAddress != null && other.evmAddress != null) {
const t = this.evmAddress.toString();
const o = other.evmAddress.toString();
if (t > o) {
return 1;
} else if (t < o) {
return -1;
} else {
return 0;
}
} else if (this.aliasKey == null && other.aliasKey == null && this.evmAddress == null && other.evmAddress == null) {
return this.num.compare(other.num);
} else {
return 1;
}
}
}
exports.default = AccountId;
_Cache.default.setAccountIdConstructor((shard, realm, key) => new AccountId(shard, realm, _long.default.ZERO, key));Выполнить команду
Для локальной разработки. Не используйте в интернете!