@meshsdk/midnight-setup
Version:
Midnight Network integration SDK for MeshSDK - https://meshjs.dev/midnight
201 lines (195 loc) • 6.94 kB
JavaScript
;
var rxjs = require('rxjs');
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/common-types.ts
var MidnightSetupPrivateStateId = "midnight-setup-private-state";
// src/utils.ts
var utils_exports = {};
__export(utils_exports, {
default: () => utils_default,
hexStringToUint8Array: () => hexStringToUint8Array,
randomNonceBytes: () => randomNonceBytes
});
var randomNonceBytes = (length, logger) => {
const newBytes = new Uint8Array(length);
crypto.getRandomValues(newBytes);
logger?.info("Random nonce bytes generated");
return newBytes;
};
function hexStringToUint8Array(hexStr) {
const hex = hexStr.replace(/^0x/, "");
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
}
return bytes;
}
var utils_default = { randomNonceBytes, hexStringToUint8Array };
// src/index.ts
var MidnightSetupAPI = class _MidnightSetupAPI {
constructor(providers, deployedContract, logger) {
this.providers = providers;
this.deployedContract = deployedContract;
this.logger = logger;
this.deployedContractAddress = deployedContract.deployTxData.public.contractAddress;
this.state = new rxjs.Observable((subscriber) => {
subscriber.next({
protocolTVL: [],
projects: []
});
});
}
deployedContractAddress;
state;
async getContractState() {
try {
this.logger?.info("Getting contract state...", {
address: this.deployedContractAddress
});
const contractState = await this.providers.publicDataProvider.queryContractState(
this.deployedContractAddress
);
if (contractState) {
this.logger?.info("Contract state retrieved successfully");
return {
address: this.deployedContractAddress,
data: contractState.data,
blockHeight: contractState.blockHeight?.toString(),
blockHash: typeof contractState.blockHash === "string" ? contractState.blockHash : contractState.blockHash?.toString()
};
} else {
this.logger?.warn("No contract state found");
return {
address: this.deployedContractAddress,
data: null,
message: "No contract state found at this address"
};
}
} catch (error) {
this.logger?.error("Failed to get contract state", {
error: error instanceof Error ? error.message : error
});
return {
address: this.deployedContractAddress,
data: null,
error: error instanceof Error ? error.message : "Failed to get contract state"
};
}
}
async getLedgerState() {
try {
this.logger?.info("Getting ledger state...", {
address: this.deployedContractAddress
});
const contractState = await this.getContractState();
if (contractState.data) {
try {
const ledgerState = contractState.data;
this.logger?.info("Ledger state parsed successfully");
let messageText = null;
if (ledgerState.message && ledgerState.message instanceof Uint8Array) {
messageText = new TextDecoder().decode(ledgerState.message);
}
return {
address: this.deployedContractAddress,
ledgerState: {
message: messageText
},
blockHeight: contractState.blockHeight,
blockHash: contractState.blockHash
};
} catch (parseError) {
this.logger?.warn("Failed to parse ledger state", {
error: parseError
});
return {
address: this.deployedContractAddress,
rawData: contractState.data,
parseError: parseError instanceof Error ? parseError.message : "Failed to parse ledger state"
};
}
} else {
return {
address: this.deployedContractAddress,
error: contractState.error || contractState.message
};
}
} catch (error) {
this.logger?.error("Failed to get ledger state", {
error: error instanceof Error ? error.message : error
});
return {
address: this.deployedContractAddress,
error: error instanceof Error ? error.message : "Failed to get ledger state"
};
}
}
static async deployContract(providers, contractInstance, logger) {
logger?.info("Deploying contract...");
try {
const initialPrivateState = await _MidnightSetupAPI.getPrivateState(providers);
const { deployContract } = await import('@midnight-ntwrk/midnight-js-contracts');
const deployedContract = await deployContract(providers, {
contract: contractInstance,
initialPrivateState,
privateStateId: MidnightSetupPrivateStateId
});
logger?.info("Contract deployed successfully", {
address: deployedContract.deployTxData.public.contractAddress
});
return new _MidnightSetupAPI(providers, deployedContract, logger);
} catch (error) {
logger?.error("Failed to deploy contract", {
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : void 0
});
throw error;
}
}
static async joinContract(providers, contractInstance, contractAddress, logger) {
logger?.info("Joining contract...", { contractAddress });
try {
const initialPrivateState = await _MidnightSetupAPI.getPrivateState(providers);
const { findDeployedContract } = await import('@midnight-ntwrk/midnight-js-contracts');
const existingContract = await findDeployedContract(providers, {
contract: contractInstance,
contractAddress,
privateStateId: MidnightSetupPrivateStateId,
initialPrivateState
});
logger?.info("Successfully joined contract", {
address: existingContract.deployTxData.public.contractAddress
});
return new _MidnightSetupAPI(providers, existingContract, logger);
} catch (error) {
logger?.error("Failed to join contract", {
error: error instanceof Error ? error.message : String(error),
contractAddress
});
throw error;
}
}
static async getPrivateState(providers) {
try {
const existingPrivateState = await providers.privateStateProvider.get(
MidnightSetupPrivateStateId
);
return existingPrivateState ?? {};
} catch (error) {
console.warn(
"Error getting private state, returning empty object:",
error
);
return {};
}
}
};
exports.MidnightSetupAPI = MidnightSetupAPI;
exports.MidnightSetupPrivateStateId = MidnightSetupPrivateStateId;
exports.utils = utils_exports;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map