@koralabs/kora-labs-common 6.15.2 → 6.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koralabs/kora-labs-common",
3
- "version": "6.15.2",
3
+ "version": "6.16.0",
4
4
  "description": "Kora Labs Common Utilities",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -77,7 +77,7 @@
77
77
  "@cardano-sdk/core": "^0.46.12 || ^0.47.0",
78
78
  "@cardano-sdk/crypto": "^0.4.5",
79
79
  "@emurgo/cardano-message-signing-nodejs": "^1.1.0",
80
- "bip39": "^3.1.0",
80
+ "bip39": "^3.0.4",
81
81
  "cassandra-driver": "^4.9.0",
82
82
  "scalus": "^0.15.0"
83
83
  },
@@ -0,0 +1,56 @@
1
+ /** The CIP-30 API a dApp gets from `window.cardano[key].enable()` (plus the CIP-95/CIP-103 extensions). */
2
+ export interface WalletApi {
3
+ getUtxos: () => Promise<string[] | null | undefined>;
4
+ getCollateral?: () => Promise<string[] | null>;
5
+ getExtensions?: () => Promise<{
6
+ cip: number;
7
+ }[]>;
8
+ getBalance: () => Promise<string>;
9
+ getNetworkId: () => Promise<number>;
10
+ getRewardAddresses: () => Promise<string[]>;
11
+ getChangeAddress: () => Promise<string>;
12
+ getUsedAddresses: () => Promise<string[]>;
13
+ getUnusedAddresses: () => Promise<string[]>;
14
+ signTx: (tx: string, partialSign?: boolean) => Promise<string>;
15
+ submitTx: (tx: string) => Promise<string>;
16
+ cip95?: {
17
+ getPubDRepKey: () => Promise<string>;
18
+ };
19
+ cip103?: {
20
+ signTxs: (txs: {
21
+ cbor: string;
22
+ partialSign?: boolean;
23
+ }[]) => Promise<string[]>;
24
+ submitTxs: (txs: string[]) => Promise<(string | {
25
+ error: unknown;
26
+ })[]>;
27
+ };
28
+ }
29
+ export interface WalletUtxoAsset {
30
+ policyId: string;
31
+ /** Asset name, hex (CIP-68 label included). */
32
+ hex: string;
33
+ count: number;
34
+ }
35
+ export interface ParsedUtxo {
36
+ /** The wallet's CBOR, untouched (ship this to a backend, never a re-encoding). */
37
+ cbor: string;
38
+ /** `<txHash>#<index>` */
39
+ id: string;
40
+ lovelace: bigint;
41
+ assets: WalletUtxoAsset[];
42
+ }
43
+ /**
44
+ * Parse one CIP-30 `getUtxos()` entry (`[input, output]` CBOR). Handles legacy array-form and
45
+ * Babbage map-form outputs (inline datum, reference script). Returns null for anything that is not
46
+ * a valid TransactionUnspentOutput, so one malformed entry never hides the rest of the wallet.
47
+ */
48
+ export declare const parseUtxo: (cborHex: string) => ParsedUtxo | null;
49
+ /** Lovelace in a CIP-30 `getBalance()` result (a CBOR `Value`: bare coin or `[coin, multiasset]`). Throws on malformed input. */
50
+ export declare const lovelaceFromBalance: (balanceCborHex: string) => bigint;
51
+ /**
52
+ * CIP-30 returns addresses as hex bytes. Shelley addresses (base, enterprise, pointer, reward) are
53
+ * returned in bech32 with the network's prefix; Byron addresses in base58. '' for empty input;
54
+ * throws on bytes that are not an address.
55
+ */
56
+ export declare const addressHexToBech32: (addressHex: string) => string;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addressHexToBech32 = exports.lovelaceFromBalance = exports.parseUtxo = void 0;
4
+ // Subpath module: `@koralabs/kora-labs-common/wallet`. Isomorphic (browser + Node), no network.
5
+ // Reads what a CIP-30 wallet returns (UTxO CBOR, balance CBOR, address hex) with
6
+ // @cardano-sdk/core Serialization — never a hand-rolled or generic CBOR decoder.
7
+ const core_1 = require("@cardano-sdk/core");
8
+ const POLICY_ID_HEX_LENGTH = 56;
9
+ const HexBlob = (hex) => hex;
10
+ /**
11
+ * Parse one CIP-30 `getUtxos()` entry (`[input, output]` CBOR). Handles legacy array-form and
12
+ * Babbage map-form outputs (inline datum, reference script). Returns null for anything that is not
13
+ * a valid TransactionUnspentOutput, so one malformed entry never hides the rest of the wallet.
14
+ */
15
+ const parseUtxo = (cborHex) => {
16
+ var _a;
17
+ const cbor = cborHex === null || cborHex === void 0 ? void 0 : cborHex.trim();
18
+ if (!cbor)
19
+ return null;
20
+ try {
21
+ const utxo = core_1.Serialization.TransactionUnspentOutput.fromCbor(HexBlob(cbor));
22
+ const input = utxo.input();
23
+ const value = utxo.output().amount();
24
+ const assets = [];
25
+ for (const [assetId, quantity] of (_a = value.multiasset()) !== null && _a !== void 0 ? _a : new Map()) {
26
+ assets.push({
27
+ policyId: assetId.slice(0, POLICY_ID_HEX_LENGTH),
28
+ hex: assetId.slice(POLICY_ID_HEX_LENGTH),
29
+ count: Number(quantity)
30
+ });
31
+ }
32
+ return { cbor, id: `${input.transactionId()}#${input.index()}`, lovelace: value.coin(), assets };
33
+ }
34
+ catch (_b) {
35
+ return null;
36
+ }
37
+ };
38
+ exports.parseUtxo = parseUtxo;
39
+ /** Lovelace in a CIP-30 `getBalance()` result (a CBOR `Value`: bare coin or `[coin, multiasset]`). Throws on malformed input. */
40
+ const lovelaceFromBalance = (balanceCborHex) => core_1.Serialization.Value.fromCbor(HexBlob(balanceCborHex)).coin();
41
+ exports.lovelaceFromBalance = lovelaceFromBalance;
42
+ /**
43
+ * CIP-30 returns addresses as hex bytes. Shelley addresses (base, enterprise, pointer, reward) are
44
+ * returned in bech32 with the network's prefix; Byron addresses in base58. '' for empty input;
45
+ * throws on bytes that are not an address.
46
+ */
47
+ const addressHexToBech32 = (addressHex) => {
48
+ const hex = (addressHex !== null && addressHex !== void 0 ? addressHex : '').replace(/^0x/i, '');
49
+ if (!hex)
50
+ return '';
51
+ const address = core_1.Cardano.Address.fromBytes(HexBlob(hex));
52
+ return address.getType() === core_1.Cardano.AddressType.Byron ? address.toBase58() : address.toBech32();
53
+ };
54
+ exports.addressHexToBech32 = addressHexToBech32;