@mistcash/sdk 0.1.0-beta

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.
@@ -0,0 +1,62 @@
1
+ import { ProviderInterface, AccountInterface } from 'starknet';
2
+ import { ChamberTypedContract } from '@mistcash/config';
3
+
4
+ /**
5
+ * devVal shortcut for empty string
6
+ * @uses devVal
7
+ */
8
+ declare const devStr: (val: string) => string;
9
+ /**
10
+ * Dev util to preset defaults if devVals is set in localStorage
11
+ * @param val value to set
12
+ * @param deflt default value when no dev vals is set
13
+ * @returns devVals ? val : deflt
14
+ */
15
+ declare const devVal: <T>(val: T, deflt?: T | undefined) => T | undefined;
16
+
17
+ interface Asset {
18
+ amount: bigint;
19
+ addr: string;
20
+ }
21
+
22
+ /**
23
+ * Returns Chamber starknet contract
24
+ * @param provider Starknet provider
25
+ * @returns Typed chamber contract
26
+ */
27
+ declare function getChamber(provider?: ProviderInterface | AccountInterface): ChamberTypedContract;
28
+ /**
29
+ * Fetch transaction assets from the chamber contract.
30
+ * ⚠️ Will show assets even if transaction is spent
31
+ * ⚠️ Contract has no way of knowing which transaction is spent
32
+ * @param contract chamber contract instance.
33
+ * @param valKey claiming key.
34
+ * @param valTo recipient.
35
+ * @returns The asset associated with the transaction.
36
+ */
37
+ declare function fetchTxAssets(contract: ChamberTypedContract, valKey: string, valTo: string): Promise<Asset>;
38
+ /**
39
+ * Checks if the transaction exists on the tree in the contract
40
+ * gets tx array from the contract and returns true if tx is found in the leaves
41
+ * ⚠️ Will show transaction exists even if it is spent
42
+ * ⚠️ Contract has no way of knowing which transaction is spent
43
+ * @param contract chamber contract instance.
44
+ * @param valKey claiming key.
45
+ * @param valTo recipient.
46
+ * @param tokenAddr token address.
47
+ * @param amount token amount.
48
+ * @returns True if the transaction exists, false otherwise.
49
+ */
50
+ declare function checkTxExists(contract: ChamberTypedContract, valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<boolean>;
51
+ /**
52
+ * Receives tx leaves array and returns true if tx is found on the tree
53
+ * @param contract chamber contract instance.
54
+ * @param valKey claiming key.
55
+ * @param valTo recipient.
56
+ * @param tokenAddr token address.
57
+ * @param amount token amount.
58
+ * @returns index of the transaction
59
+ */
60
+ declare function getTxIndexInTree(leaves: bigint[], valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<number>;
61
+
62
+ export { type Asset, checkTxExists, devStr, devVal, fetchTxAssets, getChamber, getTxIndexInTree };
@@ -0,0 +1,62 @@
1
+ import { ProviderInterface, AccountInterface } from 'starknet';
2
+ import { ChamberTypedContract } from '@mistcash/config';
3
+
4
+ /**
5
+ * devVal shortcut for empty string
6
+ * @uses devVal
7
+ */
8
+ declare const devStr: (val: string) => string;
9
+ /**
10
+ * Dev util to preset defaults if devVals is set in localStorage
11
+ * @param val value to set
12
+ * @param deflt default value when no dev vals is set
13
+ * @returns devVals ? val : deflt
14
+ */
15
+ declare const devVal: <T>(val: T, deflt?: T | undefined) => T | undefined;
16
+
17
+ interface Asset {
18
+ amount: bigint;
19
+ addr: string;
20
+ }
21
+
22
+ /**
23
+ * Returns Chamber starknet contract
24
+ * @param provider Starknet provider
25
+ * @returns Typed chamber contract
26
+ */
27
+ declare function getChamber(provider?: ProviderInterface | AccountInterface): ChamberTypedContract;
28
+ /**
29
+ * Fetch transaction assets from the chamber contract.
30
+ * ⚠️ Will show assets even if transaction is spent
31
+ * ⚠️ Contract has no way of knowing which transaction is spent
32
+ * @param contract chamber contract instance.
33
+ * @param valKey claiming key.
34
+ * @param valTo recipient.
35
+ * @returns The asset associated with the transaction.
36
+ */
37
+ declare function fetchTxAssets(contract: ChamberTypedContract, valKey: string, valTo: string): Promise<Asset>;
38
+ /**
39
+ * Checks if the transaction exists on the tree in the contract
40
+ * gets tx array from the contract and returns true if tx is found in the leaves
41
+ * ⚠️ Will show transaction exists even if it is spent
42
+ * ⚠️ Contract has no way of knowing which transaction is spent
43
+ * @param contract chamber contract instance.
44
+ * @param valKey claiming key.
45
+ * @param valTo recipient.
46
+ * @param tokenAddr token address.
47
+ * @param amount token amount.
48
+ * @returns True if the transaction exists, false otherwise.
49
+ */
50
+ declare function checkTxExists(contract: ChamberTypedContract, valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<boolean>;
51
+ /**
52
+ * Receives tx leaves array and returns true if tx is found on the tree
53
+ * @param contract chamber contract instance.
54
+ * @param valKey claiming key.
55
+ * @param valTo recipient.
56
+ * @param tokenAddr token address.
57
+ * @param amount token amount.
58
+ * @returns index of the transaction
59
+ */
60
+ declare function getTxIndexInTree(leaves: bigint[], valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<number>;
61
+
62
+ export { type Asset, checkTxExists, devStr, devVal, fetchTxAssets, getChamber, getTxIndexInTree };
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ checkTxExists: () => checkTxExists,
24
+ devStr: () => devStr,
25
+ devVal: () => devVal,
26
+ fetchTxAssets: () => fetchTxAssets,
27
+ getChamber: () => getChamber,
28
+ getTxIndexInTree: () => getTxIndexInTree
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/dev.ts
33
+ var devStr = (val) => devVal(val, "");
34
+ var devVal = (val, deflt = void 0) => {
35
+ return typeof window !== "undefined" && window.localStorage.getItem("devVals") ? val : deflt;
36
+ };
37
+
38
+ // src/utils.ts
39
+ var import_starknet = require("starknet");
40
+ var import_config = require("@mistcash/config");
41
+ var import_crypto = require("@mistcash/crypto");
42
+ function getChamber(provider) {
43
+ return new import_starknet.Contract(
44
+ import_config.CHAMBER_ABI,
45
+ import_config.CHAMBER_ADDR_MAINNET,
46
+ provider
47
+ ).typedv2(import_config.CHAMBER_ABI);
48
+ }
49
+ async function fetchTxAssets(contract, valKey, valTo) {
50
+ const asset = await contract.read_tx(await (0, import_crypto.txSecret)(valKey, valTo));
51
+ let amount = asset.amount;
52
+ if (typeof amount == "number") {
53
+ amount = BigInt(amount);
54
+ } else if (typeof amount != "bigint") {
55
+ amount = BigInt(`${amount.low}`);
56
+ }
57
+ return { amount, addr: asset.addr };
58
+ }
59
+ async function checkTxExists(contract, valKey, valTo, tokenAddr, amount) {
60
+ return await getTxIndexInTree(await contract.tx_array(), valKey, valTo, tokenAddr, amount) !== -1;
61
+ }
62
+ async function getTxIndexInTree(leaves, valKey, valTo, tokenAddr, amount) {
63
+ const tx_hash = await (0, import_crypto.txHash)(valKey, valTo, tokenAddr, amount);
64
+ return leaves.indexOf(tx_hash);
65
+ }
66
+ // Annotate the CommonJS export names for ESM import in node:
67
+ 0 && (module.exports = {
68
+ checkTxExists,
69
+ devStr,
70
+ devVal,
71
+ fetchTxAssets,
72
+ getChamber,
73
+ getTxIndexInTree
74
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,42 @@
1
+ // src/dev.ts
2
+ var devStr = (val) => devVal(val, "");
3
+ var devVal = (val, deflt = void 0) => {
4
+ return typeof window !== "undefined" && window.localStorage.getItem("devVals") ? val : deflt;
5
+ };
6
+
7
+ // src/utils.ts
8
+ import { Contract } from "starknet";
9
+ import { CHAMBER_ABI, CHAMBER_ADDR_MAINNET } from "@mistcash/config";
10
+ import { txSecret, txHash } from "@mistcash/crypto";
11
+ function getChamber(provider) {
12
+ return new Contract(
13
+ CHAMBER_ABI,
14
+ CHAMBER_ADDR_MAINNET,
15
+ provider
16
+ ).typedv2(CHAMBER_ABI);
17
+ }
18
+ async function fetchTxAssets(contract, valKey, valTo) {
19
+ const asset = await contract.read_tx(await txSecret(valKey, valTo));
20
+ let amount = asset.amount;
21
+ if (typeof amount == "number") {
22
+ amount = BigInt(amount);
23
+ } else if (typeof amount != "bigint") {
24
+ amount = BigInt(`${amount.low}`);
25
+ }
26
+ return { amount, addr: asset.addr };
27
+ }
28
+ async function checkTxExists(contract, valKey, valTo, tokenAddr, amount) {
29
+ return await getTxIndexInTree(await contract.tx_array(), valKey, valTo, tokenAddr, amount) !== -1;
30
+ }
31
+ async function getTxIndexInTree(leaves, valKey, valTo, tokenAddr, amount) {
32
+ const tx_hash = await txHash(valKey, valTo, tokenAddr, amount);
33
+ return leaves.indexOf(tx_hash);
34
+ }
35
+ export {
36
+ checkTxExists,
37
+ devStr,
38
+ devVal,
39
+ fetchTxAssets,
40
+ getChamber,
41
+ getTxIndexInTree
42
+ };
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ roots: ['<rootDir>/src', '<rootDir>/tests']
5
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@mistcash/sdk",
3
+ "version": "0.1.0-beta",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "MIST Core SDK",
8
+ "main": "dist/index.js",
9
+ "module": "dist/index.mjs",
10
+ "types": "dist/index.d.ts",
11
+ "dependencies": {
12
+ "starknet": "^7.6.4",
13
+ "@mistcash/config": "0.1.0-beta",
14
+ "@mistcash/crypto": "0.1.0-beta"
15
+ },
16
+ "devDependencies": {
17
+ "@types/jest": "^29.5.0",
18
+ "jest": "^29.5.0",
19
+ "tsup": "^8.0.0",
20
+ "typescript": "^5.0.0"
21
+ },
22
+ "scripts": {
23
+ "build": "tsup src/index.ts --format cjs,esm --dts",
24
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
25
+ "test": "jest",
26
+ "test:watch": "jest --watch",
27
+ "clean": "rm -rf dist"
28
+ }
29
+ }
package/readme.md ADDED
@@ -0,0 +1,33 @@
1
+ # @mistcash/sdk
2
+
3
+ MIST Core SDK
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ pnpm add @mistcash/sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { type Asset, fetchTxAssets, getChamber } from '@mistcash/sdk';
15
+
16
+ // Takes in Starknet provider
17
+ // Returns Typed chamber contract
18
+ const contract = getChamber(provider);
19
+
20
+ // Fetch transaction assets from the chamber contract.
21
+ // guarantees transaction was generated
22
+ // ⚠️ Will show assets even if transaction is spent
23
+ // ⚠️ Contract has no way of knowing which transaction is spent
24
+ const asset = await fetchTxAssets(contract, valKey, valTo);
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ Head over to https://github.com/mistcash/sdk for details.
30
+
31
+ ## License
32
+
33
+ MIT
package/src/dev.ts ADDED
@@ -0,0 +1,16 @@
1
+
2
+ /**
3
+ * devVal shortcut for empty string
4
+ * @uses devVal
5
+ */
6
+ export const devStr = (val: string) => devVal(val, '') as string;
7
+
8
+ /**
9
+ * Dev util to preset defaults if devVals is set in localStorage
10
+ * @param val value to set
11
+ * @param deflt default value when no dev vals is set
12
+ * @returns devVals ? val : deflt
13
+ */
14
+ export const devVal = <T,>(val: T, deflt: T | undefined = undefined) => {
15
+ return typeof window !== 'undefined' && window.localStorage.getItem('devVals') ? val : deflt
16
+ };
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './dev';
2
+ export * from './utils';
3
+ export * from './types';
package/src/types.ts ADDED
@@ -0,0 +1,4 @@
1
+ export interface Asset {
2
+ amount: bigint;
3
+ addr: string;
4
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,68 @@
1
+ import { AccountInterface, Contract, ProviderInterface, Uint256 } from 'starknet';
2
+ import { CHAMBER_ABI, CHAMBER_ADDR_MAINNET, ChamberTypedContract } from '@mistcash/config';
3
+ import { txSecret, txHash } from '@mistcash/crypto';
4
+ import { Asset } from './types';
5
+
6
+ /**
7
+ * Returns Chamber starknet contract
8
+ * @param provider Starknet provider
9
+ * @returns Typed chamber contract
10
+ */
11
+ export function getChamber(provider?: ProviderInterface | AccountInterface): ChamberTypedContract {
12
+ return new Contract(
13
+ CHAMBER_ABI,
14
+ CHAMBER_ADDR_MAINNET,
15
+ provider
16
+ ).typedv2(CHAMBER_ABI)
17
+ }
18
+
19
+ /**
20
+ * Fetch transaction assets from the chamber contract.
21
+ * ⚠️ Will show assets even if transaction is spent
22
+ * ⚠️ Contract has no way of knowing which transaction is spent
23
+ * @param contract chamber contract instance.
24
+ * @param valKey claiming key.
25
+ * @param valTo recipient.
26
+ * @returns The asset associated with the transaction.
27
+ */
28
+ export async function fetchTxAssets(contract: ChamberTypedContract, valKey: string, valTo: string): Promise<Asset> {
29
+ const asset = await contract.read_tx(await txSecret(valKey, valTo))
30
+ let amount = asset.amount;
31
+ if (typeof amount == 'number') {
32
+ amount = BigInt(amount);
33
+
34
+ } else if (typeof amount != 'bigint') {
35
+ amount = BigInt(`${amount.low}`);
36
+ }
37
+ return { amount, addr: asset.addr }
38
+ }
39
+
40
+ /**
41
+ * Checks if the transaction exists on the tree in the contract
42
+ * gets tx array from the contract and returns true if tx is found in the leaves
43
+ * ⚠️ Will show transaction exists even if it is spent
44
+ * ⚠️ Contract has no way of knowing which transaction is spent
45
+ * @param contract chamber contract instance.
46
+ * @param valKey claiming key.
47
+ * @param valTo recipient.
48
+ * @param tokenAddr token address.
49
+ * @param amount token amount.
50
+ * @returns True if the transaction exists, false otherwise.
51
+ */
52
+ export async function checkTxExists(contract: ChamberTypedContract, valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<boolean> {
53
+ return await getTxIndexInTree(await contract.tx_array() as bigint[], valKey, valTo, tokenAddr, amount) !== -1;
54
+ }
55
+
56
+ /**
57
+ * Receives tx leaves array and returns true if tx is found on the tree
58
+ * @param contract chamber contract instance.
59
+ * @param valKey claiming key.
60
+ * @param valTo recipient.
61
+ * @param tokenAddr token address.
62
+ * @param amount token amount.
63
+ * @returns index of the transaction
64
+ */
65
+ export async function getTxIndexInTree(leaves: bigint[], valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<number> {
66
+ const tx_hash = await txHash(valKey, valTo, tokenAddr, amount)
67
+ return leaves.indexOf(tx_hash);
68
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": [
8
+ "src/**/*"
9
+ ]
10
+ }