@atomiqlabs/sdk 8.7.7 → 8.9.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/api/index.d.ts +1 -0
- package/api/index.js +3 -0
- package/dist/ApiList.d.ts +37 -0
- package/dist/ApiList.js +30 -0
- package/dist/api/ApiEndpoints.d.ts +393 -0
- package/dist/api/ApiEndpoints.js +2 -0
- package/dist/api/ApiParser.d.ts +10 -0
- package/dist/api/ApiParser.js +134 -0
- package/dist/api/ApiTypes.d.ts +157 -0
- package/dist/api/ApiTypes.js +75 -0
- package/dist/api/SerializedAction.d.ts +40 -0
- package/dist/api/SerializedAction.js +59 -0
- package/dist/api/SwapperApi.d.ts +50 -0
- package/dist/api/SwapperApi.js +431 -0
- package/dist/api/index.d.ts +5 -0
- package/dist/api/index.js +24 -0
- package/dist/bitcoin/coinselect2/accumulative.d.ts +1 -0
- package/dist/bitcoin/coinselect2/accumulative.js +1 -1
- package/dist/bitcoin/coinselect2/blackjack.d.ts +1 -0
- package/dist/bitcoin/coinselect2/blackjack.js +1 -1
- package/dist/bitcoin/coinselect2/index.d.ts +3 -2
- package/dist/bitcoin/coinselect2/index.js +2 -2
- package/dist/bitcoin/coinselect2/utils.d.ts +7 -2
- package/dist/bitcoin/coinselect2/utils.js +45 -10
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +8 -25
- package/dist/bitcoin/wallet/BitcoinWallet.js +31 -18
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +40 -2
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +7 -2
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +10 -4
- package/dist/events/UnifiedSwapEventListener.d.ts +4 -3
- package/dist/events/UnifiedSwapEventListener.js +8 -2
- package/dist/http/HttpUtils.d.ts +4 -2
- package/dist/http/HttpUtils.js +10 -4
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +2 -1
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +7 -2
- package/dist/intermediaries/IntermediaryDiscovery.js +4 -4
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +182 -15
- package/dist/intermediaries/apis/IntermediaryAPI.js +192 -31
- package/dist/intermediaries/auth/SignedKeyBasedAuth.d.ts +14 -0
- package/dist/intermediaries/auth/SignedKeyBasedAuth.js +68 -0
- package/dist/storage/IUnifiedStorage.d.ts +45 -3
- package/dist/storage/UnifiedSwapStorage.d.ts +8 -2
- package/dist/storage/UnifiedSwapStorage.js +46 -8
- package/dist/swapper/Swapper.d.ts +77 -4
- package/dist/swapper/Swapper.js +117 -25
- package/dist/swapper/SwapperUtils.d.ts +18 -2
- package/dist/swapper/SwapperUtils.js +39 -1
- package/dist/swaps/ISwap.d.ts +70 -9
- package/dist/swaps/ISwap.js +28 -6
- package/dist/swaps/ISwapWrapper.d.ts +11 -1
- package/dist/swaps/ISwapWrapper.js +23 -3
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +1 -1
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -2
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +2 -1
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +2 -2
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -2
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +47 -31
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +201 -67
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +6 -6
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +82 -15
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +304 -98
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +6 -6
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +75 -42
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +424 -87
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +7 -7
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +54 -11
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +214 -41
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +2 -1
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +7 -8
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +5 -5
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +85 -22
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +299 -56
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +41 -7
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +183 -58
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +53 -12
- package/dist/swaps/trusted/ln/LnForGasSwap.js +163 -49
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +1 -2
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +14 -13
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +30 -47
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +3 -1
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +4 -4
- package/dist/types/SwapExecutionAction.d.ts +141 -34
- package/dist/types/SwapExecutionAction.js +104 -0
- package/dist/types/SwapExecutionStep.d.ts +144 -0
- package/dist/types/SwapExecutionStep.js +87 -0
- package/dist/types/TokenAmount.d.ts +6 -0
- package/dist/types/TokenAmount.js +26 -1
- package/dist/utils/BitcoinUtils.d.ts +4 -0
- package/dist/utils/BitcoinUtils.js +73 -1
- package/dist/utils/BitcoinWalletUtils.d.ts +2 -2
- package/dist/utils/Utils.d.ts +3 -1
- package/dist/utils/Utils.js +7 -1
- package/package.json +7 -4
- package/src/api/ApiEndpoints.ts +427 -0
- package/src/api/ApiParser.ts +138 -0
- package/src/api/ApiTypes.ts +229 -0
- package/src/api/SerializedAction.ts +97 -0
- package/src/api/SwapperApi.ts +545 -0
- package/src/api/index.ts +5 -0
- package/src/bitcoin/coinselect2/accumulative.ts +2 -1
- package/src/bitcoin/coinselect2/blackjack.ts +2 -1
- package/src/bitcoin/coinselect2/index.ts +5 -4
- package/src/bitcoin/coinselect2/utils.ts +55 -14
- package/src/bitcoin/wallet/BitcoinWallet.ts +69 -57
- package/src/bitcoin/wallet/IBitcoinWallet.ts +44 -3
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +12 -4
- package/src/events/UnifiedSwapEventListener.ts +11 -3
- package/src/http/HttpUtils.ts +10 -4
- package/src/http/paramcoders/client/StreamingFetchPromise.ts +4 -2
- package/src/index.ts +1 -0
- package/src/intermediaries/IntermediaryDiscovery.ts +9 -2
- package/src/intermediaries/apis/IntermediaryAPI.ts +335 -35
- package/src/intermediaries/auth/SignedKeyBasedAuth.ts +69 -0
- package/src/storage/IUnifiedStorage.ts +45 -4
- package/src/storage/UnifiedSwapStorage.ts +42 -8
- package/src/swapper/Swapper.ts +165 -24
- package/src/swapper/SwapperUtils.ts +42 -2
- package/src/swaps/ISwap.ts +88 -16
- package/src/swaps/ISwapWrapper.ts +28 -3
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +5 -3
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +3 -1
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +4 -1
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +264 -67
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +6 -4
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +390 -89
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +6 -4
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +548 -94
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +7 -5
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +276 -45
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +7 -6
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +5 -3
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +413 -64
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +239 -61
- package/src/swaps/trusted/ln/LnForGasSwap.ts +211 -47
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +1 -2
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +32 -51
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +5 -3
- package/src/types/SwapExecutionAction.ts +266 -43
- package/src/types/SwapExecutionStep.ts +224 -0
- package/src/types/TokenAmount.ts +36 -2
- package/src/utils/BitcoinUtils.ts +73 -0
- package/src/utils/BitcoinWalletUtils.ts +2 -2
- package/src/utils/Utils.ts +10 -1
- package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +0 -258
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {InputSchema, InputSchemaField} from "./ApiTypes";
|
|
2
|
+
|
|
3
|
+
// Errors
|
|
4
|
+
function invalidInput(path: string, expected: string, value: unknown): Error {
|
|
5
|
+
const actualType = value == null ? String(value) : Array.isArray(value) ? "array" : typeof(value);
|
|
6
|
+
return new Error(`Invalid input "${path}", expected ${expected}, got ${actualType}`);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function missingInput(path: string): Error {
|
|
10
|
+
return new Error(`Missing required input "${path}"`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function invalidAllowedValue(path: string, allowedValues: readonly (string | number | bigint)[], value: string | number | bigint): Error {
|
|
14
|
+
return new Error(
|
|
15
|
+
`Invalid input "${path}", expected one of: ${allowedValues.map(val => val.toString()).join(", ")}, got ${value.toString()}`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Parsers
|
|
20
|
+
function parseNumber(path: string, value: unknown): number {
|
|
21
|
+
let result: number;
|
|
22
|
+
if(typeof(value) === "number") {
|
|
23
|
+
result = value;
|
|
24
|
+
} else if(typeof(value) === "string") {
|
|
25
|
+
const trimmedValue = value.trim();
|
|
26
|
+
if(trimmedValue === "") throw invalidInput(path, "finite number", value);
|
|
27
|
+
result = Number(trimmedValue);
|
|
28
|
+
} else throw invalidInput(path, "finite number", value);
|
|
29
|
+
|
|
30
|
+
if(!Number.isFinite(result)) throw invalidInput(path, "finite number", value);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseBigInt(path: string, value: unknown): bigint {
|
|
35
|
+
if(typeof(value) === "bigint") return value;
|
|
36
|
+
|
|
37
|
+
if(typeof(value) === "number") {
|
|
38
|
+
if(!Number.isSafeInteger(value)) throw invalidInput(path, "safe integer", value);
|
|
39
|
+
return BigInt(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if(typeof(value) === "string") {
|
|
43
|
+
const trimmedValue = value.trim();
|
|
44
|
+
if(!/^[+-]?\d+$/.test(trimmedValue)) throw invalidInput(path, "integer", value);
|
|
45
|
+
return BigInt(trimmedValue);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
throw invalidInput(path, "integer", value);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function parseBoolean(path: string, value: unknown): boolean {
|
|
52
|
+
Boolean(value);
|
|
53
|
+
if(typeof(value) === "boolean") return value;
|
|
54
|
+
if(value === "true") return true;
|
|
55
|
+
if(value === "false") return false;
|
|
56
|
+
throw invalidInput(path, "boolean", value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Object type check
|
|
60
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
61
|
+
return value != null && typeof(value) === "object" && !Array.isArray(value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Allowed values for enums
|
|
65
|
+
function applyAllowedValues<T extends string | number | bigint>(path: string, value: T, allowedValues?: readonly T[]): T {
|
|
66
|
+
if(allowedValues != null && !allowedValues.includes(value)) {
|
|
67
|
+
throw invalidAllowedValue(path, allowedValues, value);
|
|
68
|
+
}
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function parseField<T>(field: InputSchemaField<T>, value: unknown, path: string): T {
|
|
73
|
+
switch(field.type) {
|
|
74
|
+
case "string": {
|
|
75
|
+
if(typeof(value) !== "string") throw invalidInput(path, "string", value);
|
|
76
|
+
return applyAllowedValues(path, value, field.allowedValues as readonly string[] | undefined) as T;
|
|
77
|
+
}
|
|
78
|
+
case "number": {
|
|
79
|
+
const parsedValue = parseNumber(path, value);
|
|
80
|
+
return applyAllowedValues(path, parsedValue, field.allowedValues as readonly number[] | undefined) as T;
|
|
81
|
+
}
|
|
82
|
+
case "bigint": {
|
|
83
|
+
const parsedValue = parseBigInt(path, value);
|
|
84
|
+
return applyAllowedValues(path, parsedValue, field.allowedValues as readonly bigint[] | undefined) as T;
|
|
85
|
+
}
|
|
86
|
+
case "boolean":
|
|
87
|
+
return parseBoolean(path, value) as T;
|
|
88
|
+
case "array": {
|
|
89
|
+
if(!Array.isArray(value)) throw invalidInput(path, "array", value);
|
|
90
|
+
if(field.items == null) return [...value] as T;
|
|
91
|
+
|
|
92
|
+
return value.map((item, index) => parseField(field.items as InputSchemaField, item, `${path}[${index}]`)) as T;
|
|
93
|
+
}
|
|
94
|
+
case "object": {
|
|
95
|
+
if(!isObject(value)) throw invalidInput(path, "object", value);
|
|
96
|
+
if(field.properties == null) return {...value} as T;
|
|
97
|
+
|
|
98
|
+
return _parseApiInput(field.properties as InputSchema<T>, value, path);
|
|
99
|
+
}
|
|
100
|
+
default:
|
|
101
|
+
throw new Error(`Unsupported input schema type for "${path}": ${(field as InputSchemaField).type}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function _parseApiInput<TInput>(inputSchema: InputSchema<TInput>, rawInput: unknown, parentPath: string = ""): TInput {
|
|
106
|
+
if(!isObject(rawInput)) {
|
|
107
|
+
throw invalidInput(parentPath || "input", "object", rawInput);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const parsedInput: Partial<TInput> = {};
|
|
111
|
+
|
|
112
|
+
for(const key in inputSchema) {
|
|
113
|
+
const field = inputSchema[key];
|
|
114
|
+
const value = rawInput[key];
|
|
115
|
+
const path = parentPath === "" ? key : `${parentPath}.${key}`;
|
|
116
|
+
|
|
117
|
+
if(value == null) {
|
|
118
|
+
if(field.required) throw missingInput(path);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
parsedInput[key] = parseField(field as InputSchemaField<TInput[typeof key]>, value, path);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return parsedInput as TInput;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Parses raw input values according to the endpoint schema.
|
|
130
|
+
*
|
|
131
|
+
* This accepts untyped transport input such as HTTP query params, JSON request bodies,
|
|
132
|
+
* or CLI arguments and returns the normalized callback input type.
|
|
133
|
+
*
|
|
134
|
+
* @category API
|
|
135
|
+
*/
|
|
136
|
+
export function parseApiInput<TInput>(inputSchema: InputSchema<TInput>, rawInput: unknown): TInput {
|
|
137
|
+
return _parseApiInput(inputSchema, rawInput);
|
|
138
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import {BitcoinTokens, Token} from "../types/Token";
|
|
2
|
+
import {TokenAmount, toTokenAmount} from "../types/TokenAmount";
|
|
3
|
+
import {LNURLPay, LNURLPayParamsWithUrl} from "../types/lnurl/LNURLPay";
|
|
4
|
+
import {LNURLWithdraw, LNURLWithdrawParamsWithUrl} from "../types/lnurl/LNURLWithdraw";
|
|
5
|
+
import {parseApiInput} from "./ApiParser";
|
|
6
|
+
import {Swapper} from "../swapper/Swapper";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Unified amount type for all API responses
|
|
10
|
+
*
|
|
11
|
+
* @category API
|
|
12
|
+
*/
|
|
13
|
+
export type ApiAmount = {
|
|
14
|
+
/** Decimal format of the amount, e.g. "1.5" */
|
|
15
|
+
amount: string;
|
|
16
|
+
/** Raw base units as string, e.g. "1500000000000000000" */
|
|
17
|
+
rawAmount: string;
|
|
18
|
+
/** Token decimals, e.g. 18 */
|
|
19
|
+
decimals: number;
|
|
20
|
+
/** Token ticker, e.g. "STRK" */
|
|
21
|
+
symbol: string;
|
|
22
|
+
/** Chain identifier, e.g. "STARKNET", "BITCOIN", "LIGHTNING" */
|
|
23
|
+
chain: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Serializable token representation for API responses
|
|
28
|
+
*
|
|
29
|
+
* @category API
|
|
30
|
+
*/
|
|
31
|
+
export type ApiToken = {
|
|
32
|
+
/** Canonical token identifier accepted by the API, e.g. "BITCOIN-BTC", "LIGHTNING-BTC", "STARKNET-STRK" */
|
|
33
|
+
id: string;
|
|
34
|
+
/** Chain identifier, e.g. "STARKNET", "BITCOIN", "LIGHTNING" */
|
|
35
|
+
chainId: string;
|
|
36
|
+
/** Token ticker, e.g. "STRK" */
|
|
37
|
+
ticker: string;
|
|
38
|
+
/** Full token name */
|
|
39
|
+
name: string;
|
|
40
|
+
/** Decimal places of the token */
|
|
41
|
+
decimals: number;
|
|
42
|
+
/** Token contract address, or empty string for BTC on Bitcoin/Lightning */
|
|
43
|
+
address: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Serializable LNURL-pay representation for API responses
|
|
48
|
+
*
|
|
49
|
+
* @category API
|
|
50
|
+
*/
|
|
51
|
+
export type ApiLNURLPay = {
|
|
52
|
+
/** Marks the LNURL payload as an LNURL-pay response. */
|
|
53
|
+
type: "pay";
|
|
54
|
+
/** Minimum payable amount supported by the LNURL-pay endpoint. */
|
|
55
|
+
min: ApiAmount;
|
|
56
|
+
/** Maximum payable amount supported by the LNURL-pay endpoint. */
|
|
57
|
+
max: ApiAmount;
|
|
58
|
+
/** Maximum comment length accepted by the LNURL-pay endpoint. */
|
|
59
|
+
commentMaxLength: number;
|
|
60
|
+
/** Short human-readable description of the payee, when provided by the LNURL service. */
|
|
61
|
+
shortDescription?: string;
|
|
62
|
+
/** Longer human-readable description of the payee, when provided by the LNURL service. */
|
|
63
|
+
longDescription?: string;
|
|
64
|
+
/** Optional icon for the payee, usually encoded as a data URL. */
|
|
65
|
+
icon?: string;
|
|
66
|
+
/** Raw LNURL-pay metadata and callback parameters. */
|
|
67
|
+
params: LNURLPayParamsWithUrl;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Serializable LNURL-withdraw representation for API responses
|
|
72
|
+
*
|
|
73
|
+
* @category API
|
|
74
|
+
*/
|
|
75
|
+
export type ApiLNURLWithdraw = {
|
|
76
|
+
/** Marks the LNURL payload as an LNURL-withdraw response. */
|
|
77
|
+
type: "withdraw";
|
|
78
|
+
/** Minimum withdrawable amount supported by the LNURL-withdraw endpoint. */
|
|
79
|
+
min: ApiAmount;
|
|
80
|
+
/** Maximum withdrawable amount supported by the LNURL-withdraw endpoint. */
|
|
81
|
+
max: ApiAmount;
|
|
82
|
+
/** Raw LNURL-withdraw metadata and callback parameters. */
|
|
83
|
+
params: LNURLWithdrawParamsWithUrl;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Serializable LNURL representation for API responses
|
|
88
|
+
*
|
|
89
|
+
* @category API
|
|
90
|
+
*/
|
|
91
|
+
export type ApiLNURL = ApiLNURLPay | ApiLNURLWithdraw;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Converts a TokenAmount to the serializable ApiAmount format
|
|
95
|
+
*
|
|
96
|
+
* @category API
|
|
97
|
+
*/
|
|
98
|
+
export function toApiAmount(tokenAmount: TokenAmount): ApiAmount {
|
|
99
|
+
return {
|
|
100
|
+
amount: tokenAmount.amount,
|
|
101
|
+
rawAmount: tokenAmount.rawAmount != null ? tokenAmount.rawAmount.toString() : "0",
|
|
102
|
+
decimals: tokenAmount.token.decimals,
|
|
103
|
+
symbol: tokenAmount.token.ticker,
|
|
104
|
+
chain: tokenAmount.token.chainId
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Converts a Token to the serializable ApiToken format
|
|
110
|
+
*
|
|
111
|
+
* @category API
|
|
112
|
+
*/
|
|
113
|
+
export function toApiToken(token: Token): ApiToken {
|
|
114
|
+
return {
|
|
115
|
+
id: `${token.chainId}-${token.ticker}`,
|
|
116
|
+
chainId: token.chainId,
|
|
117
|
+
ticker: token.ticker,
|
|
118
|
+
name: token.name,
|
|
119
|
+
decimals: token.decimals,
|
|
120
|
+
address: token.address
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Converts LNURL data to the serializable API format
|
|
126
|
+
*
|
|
127
|
+
* @category API
|
|
128
|
+
*/
|
|
129
|
+
export function toApiLNURL(lnurl: LNURLPay | LNURLWithdraw, swapper: Swapper<any>): ApiLNURL {
|
|
130
|
+
if(lnurl.type === "pay") {
|
|
131
|
+
return {
|
|
132
|
+
type: "pay",
|
|
133
|
+
min: toApiAmount(toTokenAmount(lnurl.min, BitcoinTokens.BTCLN, swapper.prices)),
|
|
134
|
+
max: toApiAmount(toTokenAmount(lnurl.max, BitcoinTokens.BTCLN, swapper.prices)),
|
|
135
|
+
commentMaxLength: lnurl.commentMaxLength,
|
|
136
|
+
...(lnurl.shortDescription != null ? {shortDescription: lnurl.shortDescription} : {}),
|
|
137
|
+
...(lnurl.longDescription != null ? {longDescription: lnurl.longDescription} : {}),
|
|
138
|
+
...(lnurl.icon != null ? {icon: lnurl.icon} : {}),
|
|
139
|
+
params: lnurl.params
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
type: "withdraw",
|
|
145
|
+
min: toApiAmount(toTokenAmount(lnurl.min, BitcoinTokens.BTCLN, swapper.prices)),
|
|
146
|
+
max: toApiAmount(toTokenAmount(lnurl.max, BitcoinTokens.BTCLN, swapper.prices)),
|
|
147
|
+
params: lnurl.params
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Maps a TypeScript type to its schema type string representation
|
|
153
|
+
*
|
|
154
|
+
* @category API
|
|
155
|
+
*/
|
|
156
|
+
type TypeToSchemaType<T> =
|
|
157
|
+
NonNullable<T> extends string ? "string" :
|
|
158
|
+
NonNullable<T> extends number ? "number" :
|
|
159
|
+
NonNullable<T> extends bigint ? "bigint" :
|
|
160
|
+
NonNullable<T> extends boolean ? "boolean" :
|
|
161
|
+
NonNullable<T> extends any[] ? "array" :
|
|
162
|
+
"object";
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Schema definition for a single API input field.
|
|
166
|
+
*
|
|
167
|
+
* @category API
|
|
168
|
+
*/
|
|
169
|
+
export type InputSchemaField<T = unknown> = {
|
|
170
|
+
/** Primitive schema type inferred from the TypeScript field type. */
|
|
171
|
+
type: TypeToSchemaType<T>;
|
|
172
|
+
/** Whether the field is required by the endpoint input parser. */
|
|
173
|
+
required: boolean;
|
|
174
|
+
/** Human-readable description of the field exposed by the API schema. */
|
|
175
|
+
description: string;
|
|
176
|
+
/** Nested schema properties for object-like fields. */
|
|
177
|
+
properties?: T extends readonly any[] ? never :
|
|
178
|
+
T extends object ? {
|
|
179
|
+
[K in keyof T]-?: InputSchemaField<T[K]>;
|
|
180
|
+
} : never; // Specifies nested object properties
|
|
181
|
+
/** Schema definition for array items when the field is an array. */
|
|
182
|
+
items?: T extends readonly (infer U)[] ? InputSchemaField<U> : never; // Specifies type of the array items
|
|
183
|
+
/** Enumerated allowed values for string, number, or bigint fields when constrained. */
|
|
184
|
+
allowedValues?: NonNullable<T> extends string | number | bigint ? NonNullable<T>[] : never; // An array of allowed values for a given field
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Schema definition describing the accepted input shape for an API endpoint.
|
|
189
|
+
*
|
|
190
|
+
* @category API
|
|
191
|
+
*/
|
|
192
|
+
export type InputSchema<TInput> = {
|
|
193
|
+
[K in keyof TInput]-?: InputSchemaField<TInput[K]>;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Typed API endpoint definition for framework-agnostic integration
|
|
198
|
+
*
|
|
199
|
+
* @category API
|
|
200
|
+
*/
|
|
201
|
+
export type ApiEndpoint<TInput, TOutput, Type extends "GET" | "POST"> = {
|
|
202
|
+
/** HTTP method used by the endpoint. */
|
|
203
|
+
type: Type;
|
|
204
|
+
/** Human-readable description of what this endpoint does, written for AI agent consumption. */
|
|
205
|
+
description: string;
|
|
206
|
+
/** Structured schema describing the accepted input payload. */
|
|
207
|
+
inputSchema: InputSchema<TInput>;
|
|
208
|
+
/** Typed endpoint implementation that receives already-validated input. */
|
|
209
|
+
callback: (input: TInput, abortSignal?: AbortSignal) => Promise<TOutput>;
|
|
210
|
+
/** Raw endpoint implementation that parses unknown input into the typed callback. */
|
|
211
|
+
callbackRaw: (input: unknown, abortSignal?: AbortSignal) => Promise<TOutput>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function createApiEndpoint<TInput, TOutput, Type extends "GET" | "POST">(
|
|
215
|
+
type: Type,
|
|
216
|
+
description: string,
|
|
217
|
+
callback: (input: TInput, abortSignal?: AbortSignal) => Promise<TOutput>,
|
|
218
|
+
inputSchema: InputSchema<TInput>
|
|
219
|
+
): ApiEndpoint<TInput, TOutput, Type> {
|
|
220
|
+
return {
|
|
221
|
+
type,
|
|
222
|
+
description,
|
|
223
|
+
callback,
|
|
224
|
+
inputSchema,
|
|
225
|
+
callbackRaw: (input, abortSignal?: AbortSignal) => {
|
|
226
|
+
return callback(parseApiInput(inputSchema, input), abortSignal);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SwapExecutionAction,
|
|
3
|
+
SwapExecutionActionSendToAddress,
|
|
4
|
+
SwapExecutionActionSignPSBT,
|
|
5
|
+
SwapExecutionActionSignSmartChainTx,
|
|
6
|
+
SwapExecutionActionWait
|
|
7
|
+
} from "../types/SwapExecutionAction";
|
|
8
|
+
import {ApiAmount, toApiAmount} from "./ApiTypes";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Strips non-serializable fields (functions, complex objects) from SwapExecutionAction types.
|
|
12
|
+
* Uses key-remapping to truly omit function keys from the resulting type.
|
|
13
|
+
*
|
|
14
|
+
* - SignSmartChainTransaction: functions removed, txs → string[]
|
|
15
|
+
* - SignPSBT: functions removed, txs[].psbt (Transaction object) omitted
|
|
16
|
+
* - SendToAddress: functions removed, txs[].amount (TokenAmount) → ApiAmount
|
|
17
|
+
* - Wait: functions removed, data fields kept as-is
|
|
18
|
+
*
|
|
19
|
+
* @category API
|
|
20
|
+
*/
|
|
21
|
+
export type SerializedAction<T extends SwapExecutionAction> =
|
|
22
|
+
T["type"] extends "SignSmartChainTransaction" ? {
|
|
23
|
+
[K in keyof T as T[K] extends Function ? never : K]:
|
|
24
|
+
K extends "txs" ? string[] : T[K]
|
|
25
|
+
} :
|
|
26
|
+
T["type"] extends "SignPSBT" ? {
|
|
27
|
+
[K in keyof T as T[K] extends Function ? never : K]:
|
|
28
|
+
K extends "txs" ? (T extends { txs: (infer U)[] }
|
|
29
|
+
? Omit<U, "psbt">[] : never) : T[K]
|
|
30
|
+
} :
|
|
31
|
+
T["type"] extends "SendToAddress" ? {
|
|
32
|
+
[K in keyof T as T[K] extends Function ? never : K]:
|
|
33
|
+
K extends "txs" ? (T extends { txs: (infer U)[] }
|
|
34
|
+
? (Omit<U, "amount"> & { amount: ApiAmount })[] : never) : T[K]
|
|
35
|
+
} : {
|
|
36
|
+
[K in keyof T as T[K] extends Function ? never : K]: T[K]
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Runtime serializer that strips non-serializable fields from a SwapExecutionAction.
|
|
41
|
+
* Matches the compile-time SerializedAction<T> type.
|
|
42
|
+
*
|
|
43
|
+
* @param action The swap execution action to serialize
|
|
44
|
+
* @param txSerializer Optional chain-specific transaction serializer for SignSmartChainTransaction actions.
|
|
45
|
+
* Accepts the chain identifier and raw transaction, returns the serialized string.
|
|
46
|
+
* Falls back to JSON.stringify if not provided.
|
|
47
|
+
*
|
|
48
|
+
* @category API
|
|
49
|
+
*/
|
|
50
|
+
export async function serializeAction(
|
|
51
|
+
action: SwapExecutionAction,
|
|
52
|
+
txSerializer?: (chainId: string, tx: any) => Promise<string>
|
|
53
|
+
): Promise<SerializedAction<SwapExecutionAction>> {
|
|
54
|
+
switch (action.type) {
|
|
55
|
+
case "SendToAddress": {
|
|
56
|
+
const {waitForTransactions, ...rest} = action as SwapExecutionActionSendToAddress<boolean>;
|
|
57
|
+
return {
|
|
58
|
+
...rest,
|
|
59
|
+
txs: rest.txs.map(tx => ({
|
|
60
|
+
type: tx.type,
|
|
61
|
+
address: tx.address,
|
|
62
|
+
hyperlink: tx.hyperlink,
|
|
63
|
+
amount: toApiAmount(tx.amount)
|
|
64
|
+
}))
|
|
65
|
+
} as unknown as SerializedAction<SwapExecutionAction>;
|
|
66
|
+
}
|
|
67
|
+
case "SignPSBT": {
|
|
68
|
+
const {submitPsbt, txs, ...rest} = action as SwapExecutionActionSignPSBT<"FUNDED_PSBT" | "RAW_PSBT">;
|
|
69
|
+
return {
|
|
70
|
+
...rest,
|
|
71
|
+
txs: txs.map(tx => {
|
|
72
|
+
const {psbt, ...txRest} = tx;
|
|
73
|
+
return txRest;
|
|
74
|
+
})
|
|
75
|
+
} as unknown as SerializedAction<SwapExecutionAction>;
|
|
76
|
+
}
|
|
77
|
+
case "SignSmartChainTransaction": {
|
|
78
|
+
const {submitTransactions, ...rest} = action as SwapExecutionActionSignSmartChainTx;
|
|
79
|
+
return {
|
|
80
|
+
...rest,
|
|
81
|
+
txs: await Promise.all(rest.txs.map(tx =>
|
|
82
|
+
txSerializer != null
|
|
83
|
+
? txSerializer(rest.chain, tx)
|
|
84
|
+
: Promise.resolve(JSON.stringify(tx))
|
|
85
|
+
))
|
|
86
|
+
} as unknown as SerializedAction<SwapExecutionAction>;
|
|
87
|
+
}
|
|
88
|
+
case "Wait": {
|
|
89
|
+
const {wait, ...rest} = action as SwapExecutionActionWait;
|
|
90
|
+
return rest as unknown as SerializedAction<SwapExecutionAction>;
|
|
91
|
+
}
|
|
92
|
+
default: {
|
|
93
|
+
const _exhaustive: never = action;
|
|
94
|
+
throw new Error(`Unknown action type: ${(_exhaustive as any).type}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|