@pushchain/core
Version:
## Overview
161 lines • 8.13 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PushChain = void 0;
const tslib_1 = require("tslib");
const constants_1 = require("../constants");
const enums_1 = require("../constants/enums");
const chain_1 = require("../constants/chain");
const orchestrator_1 = require("../orchestrator/orchestrator");
const signer_1 = require("../universal/signer");
const utils_1 = require("../utils");
const anchor_1 = require("@coral-xyz/anchor");
const viem_1 = require("viem");
/**
* @class PushChain
*
* Entry point to interact with Push Chain in your application.
* Provides access to cross-chain execution, utilities, and signer abstraction.
*/
class PushChain {
/**
* Helper function to check if input is UniversalAccount (read-only) or UniversalSigner
*/
static isUniversalAccount(input) {
return !('signMessage' in input) && !('signAndSendTransaction' in input);
}
constructor(orchestrator, universalSigner, blockExplorers, isReadMode) {
this.orchestrator = orchestrator;
this.universalSigner = universalSigner;
this.blockExplorers = blockExplorers;
this.isReadMode = isReadMode;
/**
* @method reinitialize
* Reinitializes the PushChain SDK with a new universal signer and optional config.
*
* @param universalSigner
* @param options - Optional settings to configure the SDK instance.
* - network: PushChain network to target (e.g., TESTNET_DONUT, MAINNET).
* - rpcUrls: Custom RPC URLs mapped by chain IDs.
* - printTraces: Whether to print internal trace logs for debugging.
*
* @returns A new initialized instance of PushChain.
*/
this.reinitialize = (universalSigner, options) => tslib_1.__awaiter(this, void 0, void 0, function* () {
var _b, _c, _d, _e, _f;
const mergedOptions = {
network: (_b = options === null || options === void 0 ? void 0 : options.network) !== null && _b !== void 0 ? _b : this.orchestrator.getNetwork(),
rpcUrls: (_c = options === null || options === void 0 ? void 0 : options.rpcUrls) !== null && _c !== void 0 ? _c : this.orchestrator.getRpcUrls(),
blockExplorers: (_d = options === null || options === void 0 ? void 0 : options.blockExplorers) !== null && _d !== void 0 ? _d : this.blockExplorers,
printTraces: (_e = options === null || options === void 0 ? void 0 : options.printTraces) !== null && _e !== void 0 ? _e : this.orchestrator.getPrintTraces(),
progressHook: (_f = options === null || options === void 0 ? void 0 : options.progressHook) !== null && _f !== void 0 ? _f : this.orchestrator.getProgressHook(),
};
return _a.createInstance(universalSigner, mergedOptions);
});
this.orchestrator = orchestrator;
this.universal = {
get origin() {
return orchestrator.getUOA();
},
get account() {
return orchestrator.computeUEAOffchain();
},
sendTransaction: (...args) => {
if (this.isReadMode) {
throw new Error('Read only mode cannot call sendTransaction function');
}
return orchestrator.execute.bind(orchestrator)(...args);
},
signMessage: (data) => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.isReadMode) {
throw new Error('Read only mode cannot call signMessage function');
}
const sigBytes = yield universalSigner.signMessage(data);
const chain = universalSigner.account.chain;
if (chain_1.CHAIN_INFO[chain].vm === enums_1.VM.EVM) {
return (0, viem_1.bytesToHex)(sigBytes);
}
else if (chain_1.CHAIN_INFO[chain].vm === enums_1.VM.SVM) {
return anchor_1.utils.bytes.bs58.encode(sigBytes);
}
return (0, viem_1.bytesToHex)(sigBytes);
}),
signTypedData: (...args) => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (typeof universalSigner.signTypedData !== 'function') {
throw new Error('Typed data signing not supported');
}
const signBytes = yield universalSigner.signTypedData(...args);
return (0, viem_1.bytesToHex)(signBytes);
}),
};
this.explorer = {
getTransactionUrl: (txHash) => {
return `https://donut.push.network/tx/${txHash}`;
},
listUrls: () => {
var _b;
return (_b = blockExplorers[enums_1.CHAIN.PUSH_TESTNET_DONUT]) !== null && _b !== void 0 ? _b : [];
},
};
}
/**
* @private
* Internal method to create a PushChain instance with the given parameters.
* Used by both initialize and reinitialize methods to avoid code duplication.
*/
static createInstance(universalSigner, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _b, _c, _d, _e;
const isReadOnly = _a.isUniversalAccount(universalSigner);
// If it's a UniversalAccount (read-only), create a dummy signer for the orchestrator
const validatedUniversalSigner = isReadOnly
? (0, signer_1.createUniversalSigner)({
account: universalSigner,
signMessage: () => tslib_1.__awaiter(this, void 0, void 0, function* () {
throw new Error('Read only mode cannot call signMessage function');
}),
signAndSendTransaction: () => tslib_1.__awaiter(this, void 0, void 0, function* () {
throw new Error('Read only mode cannot call signAndSendTransaction function');
}),
})
: (0, signer_1.createUniversalSigner)(universalSigner);
const blockExplorers = (_b = options === null || options === void 0 ? void 0 : options.blockExplorers) !== null && _b !== void 0 ? _b : {
[enums_1.CHAIN.PUSH_TESTNET_DONUT]: ['https://donut.push.network'],
};
const orchestrator = new orchestrator_1.Orchestrator(
/**
* Ensures the signer conforms to the UniversalSigner interface.
*/
validatedUniversalSigner, (_c = options === null || options === void 0 ? void 0 : options.network) !== null && _c !== void 0 ? _c : enums_1.PUSH_NETWORK.TESTNET_DONUT, (_d = options === null || options === void 0 ? void 0 : options.rpcUrls) !== null && _d !== void 0 ? _d : {}, (_e = options === null || options === void 0 ? void 0 : options.printTraces) !== null && _e !== void 0 ? _e : false, options === null || options === void 0 ? void 0 : options.progressHook);
return new _a(orchestrator, validatedUniversalSigner, blockExplorers, isReadOnly);
});
}
}
exports.PushChain = PushChain;
_a = PushChain;
/**
* @static
* Constants for the PushChain SDK.
*/
PushChain.CONSTANTS = constants_1.CONSTANTS;
/**
* @static
* Utility functions for encoding, hashing, and data formatting.
*/
PushChain.utils = utils_1.Utils;
/**
* @method initialize
* Initializes the PushChain SDK with a universal signer and optional config.
*
* @param universalSigner
* @param options - Optional settings to configure the SDK instance.
* - network: PushChain network to target (e.g., TESTNET_DONUT, MAINNET).
* - rpcUrls: Custom RPC URLs mapped by chain IDs.
* - printTraces: Whether to print internal trace logs for debugging.
*
* @returns An initialized instance of PushChain.
*/
PushChain.initialize = (universalSigner, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
return _a.createInstance(universalSigner, options);
});
//# sourceMappingURL=push-chain.js.map