@gorbchain-xyz/chaindecode
Version:
GorbchainSDK V1.3+ - Complete Solana development toolkit with advanced cryptography, messaging, and collaboration features. Build secure applications with blockchain, DeFi, and end-to-end encryption.
121 lines (120 loc) • 5.07 kB
JavaScript
/**
* Decoder registry setup utilities for the GorbchainSDK
*
* This module contains functions for setting up and configuring
* the decoder registry with various blockchain program decoders.
*/
import { DecoderRegistry } from '../decoders/registry.js';
// Import all decoders
import { decodeSystemInstruction } from '../decoders/system.js';
import { decodeSPLTokenInstruction } from '../decoders/splToken.js';
import { decodeToken2022Instruction } from '../decoders/token2022.js';
import { decodeATAInstruction } from '../decoders/ata.js';
import { decodeNFTInstruction } from '../decoders/nft.js';
/**
* Create and configure a decoder registry with all supported program decoders
*/
export function createDecoderRegistry(config) {
var _a, _b, _c, _d;
const registry = new DecoderRegistry();
// Helper function to convert base64 string to Uint8Array
const base64ToUint8Array = (base64) => {
try {
const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
catch (_a) {
return new Uint8Array(0);
}
};
// Register System Program decoder
const systemProgramId = '11111111111111111111111111111111';
registry.register('system', systemProgramId, (instruction) => {
var _a;
const data = instruction.data ? (instruction.data instanceof Uint8Array ? instruction.data : new Uint8Array(instruction.data)) : new Uint8Array(0);
const decoded = decodeSystemInstruction(data);
return {
type: decoded.type,
programId: instruction.programId,
data: {
instruction: decoded.instruction,
lamports: decoded.lamports,
space: decoded.space,
seed: decoded.seed
},
accounts: (_a = instruction.accounts) !== null && _a !== void 0 ? _a : [],
raw: instruction
};
});
// Register SPL Token decoder
const splTokenProgramId = ((_a = config.programIds) === null || _a === void 0 ? void 0 : _a.splToken) || 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
registry.register('spl-token', splTokenProgramId, (instruction) => {
var _a;
const decoded = decodeSPLTokenInstruction(instruction);
return {
type: decoded.type,
programId: instruction.programId,
data: decoded.data,
accounts: (_a = instruction.accounts) !== null && _a !== void 0 ? _a : [],
raw: instruction
};
});
// Register Token-2022 decoder
const token2022ProgramId = ((_b = config.programIds) === null || _b === void 0 ? void 0 : _b.token2022) || 'FGyzDo6bhE7gFmSYymmFnJ3SZZu3xWGBA7sNHXR7QQsn';
registry.register('token-2022', token2022ProgramId, (instruction) => {
var _a;
const decoded = decodeToken2022Instruction(instruction);
return {
type: decoded.type,
programId: instruction.programId,
data: decoded.data,
accounts: (_a = instruction.accounts) !== null && _a !== void 0 ? _a : [],
raw: instruction
};
});
// Register ATA decoder
const ataProgramId = ((_c = config.programIds) === null || _c === void 0 ? void 0 : _c.ata) || '4YpYoLVTQ8bxcne9GneN85RUXeN7pqGTwgPcY71ZL5gX';
registry.register('ata', ataProgramId, (instruction) => {
var _a;
const decoded = decodeATAInstruction(instruction);
return {
type: decoded.type,
programId: instruction.programId,
data: decoded.data,
accounts: (_a = instruction.accounts) !== null && _a !== void 0 ? _a : [],
raw: instruction
};
});
// Register NFT/Metaplex decoder
const metaplexProgramId = ((_d = config.programIds) === null || _d === void 0 ? void 0 : _d.metaplex) || 'BvoSmPBF6mBRxBMY9FPguw1zUoUg3xrc5CaWf7y5ACkc';
registry.register('nft', metaplexProgramId, (instruction) => {
var _a;
const decoded = decodeNFTInstruction(instruction);
return {
type: decoded.type,
programId: instruction.programId,
data: decoded.data,
accounts: (_a = instruction.accounts) !== null && _a !== void 0 ? _a : [],
raw: instruction
};
});
return registry;
}
/**
* Get program name from program ID
*/
export function getProgramName(programId) {
const programNames = {
'11111111111111111111111111111111': 'System',
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA': 'SPL Token',
'FGyzDo6bhE7gFmSYymmFnJ3SZZu3xWGBA7sNHXR7QQsn': 'Token-2022',
'4YpYoLVTQ8bxcne9GneN85RUXeN7pqGTwgPcY71ZL5gX': 'ATA',
'BvoSmPBF6mBRxBMY9FPguw1zUoUg3xrc5CaWf7y5ACkc': 'Metaplex'
// Add more program mappings as needed
};
return programNames[programId] || 'Unknown Program';
}