@t402/tezos 2.3.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/README.md +161 -0
- package/dist/exact-direct/client/index.d.cts +92 -0
- package/dist/exact-direct/client/index.d.ts +92 -0
- package/dist/exact-direct/client/index.js +204 -0
- package/dist/exact-direct/client/index.js.map +1 -0
- package/dist/exact-direct/client/index.mjs +176 -0
- package/dist/exact-direct/client/index.mjs.map +1 -0
- package/dist/exact-direct/facilitator/index.d.cts +110 -0
- package/dist/exact-direct/facilitator/index.d.ts +110 -0
- package/dist/exact-direct/facilitator/index.js +331 -0
- package/dist/exact-direct/facilitator/index.js.map +1 -0
- package/dist/exact-direct/facilitator/index.mjs +303 -0
- package/dist/exact-direct/facilitator/index.mjs.map +1 -0
- package/dist/exact-direct/server/index.d.cts +109 -0
- package/dist/exact-direct/server/index.d.ts +109 -0
- package/dist/exact-direct/server/index.js +226 -0
- package/dist/exact-direct/server/index.js.map +1 -0
- package/dist/exact-direct/server/index.mjs +198 -0
- package/dist/exact-direct/server/index.mjs.map +1 -0
- package/dist/index.d.cts +124 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.js +228 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +170 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types-DQMtUOa_.d.cts +125 -0
- package/dist/types-DQMtUOa_.d.ts +125 -0
- package/package.json +100 -0
- package/src/constants.ts +53 -0
- package/src/exact-direct/client/index.ts +13 -0
- package/src/exact-direct/client/register.ts +71 -0
- package/src/exact-direct/client/scheme.ts +177 -0
- package/src/exact-direct/facilitator/index.ts +13 -0
- package/src/exact-direct/facilitator/register.ts +74 -0
- package/src/exact-direct/facilitator/scheme.ts +311 -0
- package/src/exact-direct/server/index.ts +13 -0
- package/src/exact-direct/server/register.ts +64 -0
- package/src/exact-direct/server/scheme.ts +205 -0
- package/src/index.ts +32 -0
- package/src/tokens.ts +86 -0
- package/src/types.ts +160 -0
- package/src/utils.ts +128 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tezos mechanism types
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Network } from "@t402/core/types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Exact-direct payment payload for Tezos
|
|
9
|
+
*/
|
|
10
|
+
export type ExactDirectTezosPayload = {
|
|
11
|
+
/** Operation hash (o...) */
|
|
12
|
+
opHash: string;
|
|
13
|
+
/** Sender address (tz1/tz2/tz3...) */
|
|
14
|
+
from: string;
|
|
15
|
+
/** Recipient address */
|
|
16
|
+
to: string;
|
|
17
|
+
/** Amount in smallest units */
|
|
18
|
+
amount: string;
|
|
19
|
+
/** FA2 contract address (KT1...) */
|
|
20
|
+
contractAddress: string;
|
|
21
|
+
/** Token ID within the FA2 contract */
|
|
22
|
+
tokenId: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Tezos signer interface for client-side operations
|
|
27
|
+
*/
|
|
28
|
+
export interface TezosSigner {
|
|
29
|
+
/** Get the signer's address */
|
|
30
|
+
getAddress(): Promise<string>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Execute an FA2 transfer
|
|
34
|
+
* @param contractAddress FA2 contract address
|
|
35
|
+
* @param tokenId Token ID
|
|
36
|
+
* @param to Recipient address
|
|
37
|
+
* @param amount Amount to transfer
|
|
38
|
+
* @returns Operation hash
|
|
39
|
+
*/
|
|
40
|
+
transfer(
|
|
41
|
+
contractAddress: string,
|
|
42
|
+
tokenId: number,
|
|
43
|
+
to: string,
|
|
44
|
+
amount: bigint,
|
|
45
|
+
): Promise<string>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get token balance
|
|
49
|
+
* @param contractAddress FA2 contract address
|
|
50
|
+
* @param tokenId Token ID
|
|
51
|
+
* @param address Address to check (optional, defaults to signer address)
|
|
52
|
+
* @returns Balance in smallest units
|
|
53
|
+
*/
|
|
54
|
+
getBalance(
|
|
55
|
+
contractAddress: string,
|
|
56
|
+
tokenId: number,
|
|
57
|
+
address?: string,
|
|
58
|
+
): Promise<bigint>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Tezos signer interface for facilitator operations
|
|
63
|
+
*/
|
|
64
|
+
export interface FacilitatorTezosSigner {
|
|
65
|
+
/** Get facilitator addresses for a network */
|
|
66
|
+
getAddresses(network: Network): string[];
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Query an operation by hash
|
|
70
|
+
* @param opHash Operation hash
|
|
71
|
+
* @returns Operation result or null if not found
|
|
72
|
+
*/
|
|
73
|
+
queryOperation(opHash: string): Promise<TezosOperationResult | null>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Get token balance for an address
|
|
77
|
+
* @param contractAddress FA2 contract address
|
|
78
|
+
* @param tokenId Token ID
|
|
79
|
+
* @param address Address to check
|
|
80
|
+
* @returns Balance as string
|
|
81
|
+
*/
|
|
82
|
+
getBalance(
|
|
83
|
+
contractAddress: string,
|
|
84
|
+
tokenId: number,
|
|
85
|
+
address: string,
|
|
86
|
+
): Promise<string>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Tezos operation result from indexer
|
|
91
|
+
*/
|
|
92
|
+
export interface TezosOperationResult {
|
|
93
|
+
/** Operation hash */
|
|
94
|
+
hash: string;
|
|
95
|
+
/** Block level */
|
|
96
|
+
level: number;
|
|
97
|
+
/** Timestamp */
|
|
98
|
+
timestamp: string;
|
|
99
|
+
/** Status: applied, failed, backtracked, skipped */
|
|
100
|
+
status: "applied" | "failed" | "backtracked" | "skipped";
|
|
101
|
+
/** Sender address */
|
|
102
|
+
sender: {
|
|
103
|
+
address: string;
|
|
104
|
+
};
|
|
105
|
+
/** Target contract (for contract calls) */
|
|
106
|
+
target?: {
|
|
107
|
+
address: string;
|
|
108
|
+
};
|
|
109
|
+
/** Entrypoint called */
|
|
110
|
+
entrypoint?: string;
|
|
111
|
+
/** Parameter value */
|
|
112
|
+
parameter?: unknown;
|
|
113
|
+
/** Amount transferred (in mutez for XTZ) */
|
|
114
|
+
amount?: number;
|
|
115
|
+
/** Errors if failed */
|
|
116
|
+
errors?: Array<{
|
|
117
|
+
type: string;
|
|
118
|
+
message?: string;
|
|
119
|
+
}>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* FA2 transfer parameter structure
|
|
124
|
+
*/
|
|
125
|
+
export interface FA2TransferParam {
|
|
126
|
+
from_: string;
|
|
127
|
+
txs: Array<{
|
|
128
|
+
to_: string;
|
|
129
|
+
token_id: number;
|
|
130
|
+
amount: string;
|
|
131
|
+
}>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Check if a string is a valid Tezos address
|
|
136
|
+
*/
|
|
137
|
+
export function isValidTezosAddress(address: string): boolean {
|
|
138
|
+
if (!address) return false;
|
|
139
|
+
// tz1, tz2, tz3 for implicit accounts, KT1 for contracts
|
|
140
|
+
const prefixPattern = /^(tz1|tz2|tz3|KT1)/;
|
|
141
|
+
if (!prefixPattern.test(address)) return false;
|
|
142
|
+
// Base58 check - length should be 36 characters
|
|
143
|
+
return address.length === 36;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Check if a string is a valid Tezos operation hash
|
|
148
|
+
*/
|
|
149
|
+
export function isValidOperationHash(opHash: string): boolean {
|
|
150
|
+
if (!opHash) return false;
|
|
151
|
+
// Operation hashes start with 'o' and are 51 characters
|
|
152
|
+
return opHash.startsWith("o") && opHash.length === 51;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Check if a network is a Tezos network
|
|
157
|
+
*/
|
|
158
|
+
export function isTezosNetwork(network: string): boolean {
|
|
159
|
+
return network.startsWith("tezos:");
|
|
160
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tezos utility functions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
NETWORK_CONFIGS,
|
|
7
|
+
TEZOS_CAIP2_NAMESPACE,
|
|
8
|
+
SUPPORTED_NETWORKS,
|
|
9
|
+
} from "./constants.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get network configuration
|
|
13
|
+
*/
|
|
14
|
+
export function getNetworkConfig(network: string) {
|
|
15
|
+
return NETWORK_CONFIGS[network];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Check if a network is supported
|
|
20
|
+
*/
|
|
21
|
+
export function isSupportedNetwork(network: string): boolean {
|
|
22
|
+
return (SUPPORTED_NETWORKS as readonly string[]).includes(network);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parse a CAIP-2 network identifier to extract the chain reference
|
|
27
|
+
*/
|
|
28
|
+
export function parseNetworkId(network: string): {
|
|
29
|
+
namespace: string;
|
|
30
|
+
reference: string;
|
|
31
|
+
} | null {
|
|
32
|
+
const parts = network.split(":");
|
|
33
|
+
if (parts.length !== 2) return null;
|
|
34
|
+
return {
|
|
35
|
+
namespace: parts[0],
|
|
36
|
+
reference: parts[1],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Build a CAIP-2 network identifier
|
|
42
|
+
*/
|
|
43
|
+
export function buildNetworkId(reference: string): string {
|
|
44
|
+
return `${TEZOS_CAIP2_NAMESPACE}:${reference}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get indexer URL for a network
|
|
49
|
+
*/
|
|
50
|
+
export function getIndexerUrl(network: string): string | undefined {
|
|
51
|
+
return NETWORK_CONFIGS[network]?.indexerUrl;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get RPC URL for a network
|
|
56
|
+
*/
|
|
57
|
+
export function getRpcUrl(network: string): string | undefined {
|
|
58
|
+
return NETWORK_CONFIGS[network]?.rpcUrl;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Compare two Tezos addresses (case-insensitive for base58)
|
|
63
|
+
*/
|
|
64
|
+
export function compareAddresses(addr1: string, addr2: string): boolean {
|
|
65
|
+
if (!addr1 || !addr2) return false;
|
|
66
|
+
return addr1 === addr2;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Format amount with decimals
|
|
71
|
+
*/
|
|
72
|
+
export function formatAmount(amount: bigint, decimals: number): string {
|
|
73
|
+
const divisor = BigInt(10 ** decimals);
|
|
74
|
+
const wholePart = amount / divisor;
|
|
75
|
+
const fractionalPart = amount % divisor;
|
|
76
|
+
const fractionalStr = fractionalPart.toString().padStart(decimals, "0");
|
|
77
|
+
return `${wholePart}.${fractionalStr}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Parse amount string to bigint
|
|
82
|
+
*/
|
|
83
|
+
export function parseAmount(amount: string, decimals: number): bigint {
|
|
84
|
+
const [whole, fractional = ""] = amount.split(".");
|
|
85
|
+
const paddedFractional = fractional.padEnd(decimals, "0").slice(0, decimals);
|
|
86
|
+
return BigInt(whole + paddedFractional);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Extract FA2 transfer details from operation parameter
|
|
91
|
+
*/
|
|
92
|
+
export function extractFA2TransferDetails(
|
|
93
|
+
parameter: unknown,
|
|
94
|
+
): {
|
|
95
|
+
from: string;
|
|
96
|
+
to: string;
|
|
97
|
+
tokenId: number;
|
|
98
|
+
amount: string;
|
|
99
|
+
} | null {
|
|
100
|
+
if (!parameter || !Array.isArray(parameter)) return null;
|
|
101
|
+
|
|
102
|
+
// FA2 transfer parameter is an array of { from_: string, txs: [...] }
|
|
103
|
+
const firstTransfer = parameter[0];
|
|
104
|
+
if (!firstTransfer || typeof firstTransfer !== "object") return null;
|
|
105
|
+
|
|
106
|
+
const transfer = firstTransfer as Record<string, unknown>;
|
|
107
|
+
const from = transfer.from_ as string;
|
|
108
|
+
const txs = transfer.txs as Array<{
|
|
109
|
+
to_: string;
|
|
110
|
+
token_id: number | string;
|
|
111
|
+
amount: number | string;
|
|
112
|
+
}>;
|
|
113
|
+
|
|
114
|
+
if (!from || !Array.isArray(txs) || txs.length === 0) return null;
|
|
115
|
+
|
|
116
|
+
const firstTx = txs[0];
|
|
117
|
+
if (!firstTx) return null;
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
from,
|
|
121
|
+
to: firstTx.to_,
|
|
122
|
+
tokenId:
|
|
123
|
+
typeof firstTx.token_id === "string"
|
|
124
|
+
? parseInt(firstTx.token_id, 10)
|
|
125
|
+
: firstTx.token_id,
|
|
126
|
+
amount: String(firstTx.amount),
|
|
127
|
+
};
|
|
128
|
+
}
|