@avalanche-sdk/client
Version:
A TypeScript SDK for interacting with the Avalanche network through JSON-RPC APIs. This SDK provides a comprehensive set of tools to interact with all Avalanche chains (P-Chain, X-Chain, C-Chain) and various APIs, including wallet functionality for transa
33 lines (30 loc) • 928 B
text/typescript
import { utils, Utxo } from "@avalabs/avalanchejs";
/**
* Get a Utxo from a buffer or hex string
* @param utxoBytesOrHex - The buffer or hex string to get the Utxo from `string` or `Uint8Array`
* @param chainAlias - The chain alias to get the Utxo from `"P" | "X" | "C"`
* @returns The Utxo {@link Utxo}
*
* @example
* ```ts
* import { getUtxoFromBytes } from "@avalanche-sdk/client/utils";
*
* const utxo = getUtxoFromBytes("0x1234567890abcdef", "P");
* ```
*/
export function getUtxoFromBytes(
utxoBytesOrHex: string | Uint8Array,
chainAlias: "P" | "X" | "C"
): Utxo {
let utxoBytes;
if (typeof utxoBytesOrHex === "string") {
utxoBytes = utils.hexToBuffer(utxoBytesOrHex);
} else {
utxoBytes = utxoBytesOrHex;
}
const manager = utils.getManagerForVM(
chainAlias === "P" ? "PVM" : chainAlias === "X" ? "AVM" : "EVM"
);
const utxo = manager.unpack(utxoBytes, Utxo);
return utxo;
}