@t402/cosmos 2.4.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 +175 -0
- package/dist/cjs/exact-direct/client/index.d.ts +50 -0
- package/dist/cjs/exact-direct/client/index.js +122 -0
- package/dist/cjs/exact-direct/client/index.js.map +1 -0
- package/dist/cjs/exact-direct/facilitator/index.d.ts +70 -0
- package/dist/cjs/exact-direct/facilitator/index.js +300 -0
- package/dist/cjs/exact-direct/facilitator/index.js.map +1 -0
- package/dist/cjs/exact-direct/server/index.d.ts +87 -0
- package/dist/cjs/exact-direct/server/index.js +241 -0
- package/dist/cjs/exact-direct/server/index.js.map +1 -0
- package/dist/cjs/index.d.ts +178 -0
- package/dist/cjs/index.js +619 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types-CgseoctH.d.ts +155 -0
- package/dist/esm/chunk-2UTEGIZ2.mjs +101 -0
- package/dist/esm/chunk-2UTEGIZ2.mjs.map +1 -0
- package/dist/esm/chunk-57DBLYIR.mjs +58 -0
- package/dist/esm/chunk-57DBLYIR.mjs.map +1 -0
- package/dist/esm/chunk-JV6LXL2U.mjs +158 -0
- package/dist/esm/chunk-JV6LXL2U.mjs.map +1 -0
- package/dist/esm/chunk-QP5VO3IO.mjs +68 -0
- package/dist/esm/chunk-QP5VO3IO.mjs.map +1 -0
- package/dist/esm/chunk-XUMAS5DJ.mjs +223 -0
- package/dist/esm/chunk-XUMAS5DJ.mjs.map +1 -0
- package/dist/esm/exact-direct/client/index.d.mts +50 -0
- package/dist/esm/exact-direct/client/index.mjs +8 -0
- package/dist/esm/exact-direct/client/index.mjs.map +1 -0
- package/dist/esm/exact-direct/facilitator/index.d.mts +70 -0
- package/dist/esm/exact-direct/facilitator/index.mjs +9 -0
- package/dist/esm/exact-direct/facilitator/index.mjs.map +1 -0
- package/dist/esm/exact-direct/server/index.d.mts +87 -0
- package/dist/esm/exact-direct/server/index.mjs +9 -0
- package/dist/esm/exact-direct/server/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +178 -0
- package/dist/esm/index.mjs +83 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/types-CgseoctH.d.mts +155 -0
- package/package.json +96 -0
package/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# @t402/cosmos
|
|
2
|
+
|
|
3
|
+
Cosmos (Noble) implementation of the t402 payment protocol using the **exact-direct** payment scheme with native USDC via `MsgSend`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @t402/cosmos
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @t402/cosmos
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
This package provides support for USDC payments on Cosmos SDK chains (currently Noble) using the exact-direct scheme. The client executes a `MsgSend` transaction directly on-chain, then provides the transaction hash as proof of payment.
|
|
16
|
+
|
|
17
|
+
Three main components:
|
|
18
|
+
|
|
19
|
+
- **Client** - For applications that make payments (have Cosmos wallets)
|
|
20
|
+
- **Facilitator** - For payment processors that verify transactions via REST API
|
|
21
|
+
- **Server** - For resource servers that accept payments and build payment requirements
|
|
22
|
+
|
|
23
|
+
## Supported Networks
|
|
24
|
+
|
|
25
|
+
| Network | CAIP-2 Identifier | USDC Denomination | Bech32 Prefix | Status |
|
|
26
|
+
| -------------- | ------------------ | ----------------- | ------------- | ---------- |
|
|
27
|
+
| Noble Mainnet | `cosmos:noble-1` | `uusdc` | `noble` | Production |
|
|
28
|
+
| Noble Testnet | `cosmos:grand-1` | `uusdc` | `noble` | Testnet |
|
|
29
|
+
|
|
30
|
+
## Package Exports
|
|
31
|
+
|
|
32
|
+
### Main Package (`@t402/cosmos`)
|
|
33
|
+
|
|
34
|
+
**Constants:**
|
|
35
|
+
|
|
36
|
+
- `NOBLE_MAINNET_CAIP2` - CAIP-2 identifier for Noble mainnet
|
|
37
|
+
- `NOBLE_TESTNET_CAIP2` - CAIP-2 identifier for Noble testnet
|
|
38
|
+
- `COSMOS_NETWORKS` - Array of supported networks
|
|
39
|
+
- `NETWORK_RPC_ENDPOINTS` - RPC endpoint mapping
|
|
40
|
+
- `NETWORK_REST_ENDPOINTS` - REST endpoint mapping
|
|
41
|
+
- `SCHEME_EXACT_DIRECT` - Scheme identifier
|
|
42
|
+
- `USDC_DENOM` - USDC denomination (`uusdc`)
|
|
43
|
+
- `MSG_TYPE_SEND` - Cosmos MsgSend type URL
|
|
44
|
+
|
|
45
|
+
**Tokens:**
|
|
46
|
+
|
|
47
|
+
- `TOKEN_REGISTRY` - Token configurations by network
|
|
48
|
+
- `getTokenConfig(network, symbol)` - Get token by symbol
|
|
49
|
+
- `getTokenByDenom(network, denom)` - Get token by denomination
|
|
50
|
+
- `getDefaultToken(network)` - Get default token
|
|
51
|
+
- `getNetworkTokens(network)` - Get all tokens for a network
|
|
52
|
+
- `isNetworkSupported(network)` - Check if network is supported
|
|
53
|
+
|
|
54
|
+
**Utilities:**
|
|
55
|
+
|
|
56
|
+
- `isValidAddress(address)` - Validate Noble bech32 address (`noble1...`)
|
|
57
|
+
- `normalizeNetwork(network)` - Normalize to CAIP-2 format
|
|
58
|
+
- `extractNetworkId(network)` - Extract network ID from CAIP-2
|
|
59
|
+
- `getRpcEndpoint(network)` - Get RPC endpoint for network
|
|
60
|
+
- `getRestEndpoint(network)` - Get REST endpoint for network
|
|
61
|
+
- `toAtomicUnits(amount, decimals)` - Convert decimal to smallest units
|
|
62
|
+
- `formatAmount(amount, decimals)` - Format for display
|
|
63
|
+
|
|
64
|
+
### Client (`@t402/cosmos/exact-direct/client`)
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { ExactDirectCosmosClient } from "@t402/cosmos/exact-direct/client";
|
|
68
|
+
|
|
69
|
+
const client = new ExactDirectCosmosClient(signer);
|
|
70
|
+
const payload = await client.createPaymentPayload(2, requirements);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Server (`@t402/cosmos/exact-direct/server`)
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { ExactDirectCosmosServer } from "@t402/cosmos/exact-direct/server";
|
|
77
|
+
|
|
78
|
+
const server = new ExactDirectCosmosServer();
|
|
79
|
+
const assetAmount = await server.parsePrice(1.5, "cosmos:noble-1");
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Facilitator (`@t402/cosmos/exact-direct/facilitator`)
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { ExactDirectCosmosFacilitator } from "@t402/cosmos/exact-direct/facilitator";
|
|
86
|
+
|
|
87
|
+
const facilitator = new ExactDirectCosmosFacilitator(signer);
|
|
88
|
+
const result = await facilitator.verify(2, payloadBytes, requirementsBytes);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Payment Flow
|
|
92
|
+
|
|
93
|
+
1. **Client** requests protected resource
|
|
94
|
+
2. **Server** responds with 402 + payment requirements (network, amount, payTo)
|
|
95
|
+
3. **Client** broadcasts a `MsgSend` transaction on Noble chain
|
|
96
|
+
4. **Client** submits transaction hash as payment proof
|
|
97
|
+
5. **Facilitator** queries Noble REST API to verify the transaction
|
|
98
|
+
6. **Facilitator** confirms payment matches requirements (amount, recipient, denom)
|
|
99
|
+
|
|
100
|
+
## Payload Structure
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
interface ExactDirectCosmosPayload {
|
|
104
|
+
txHash: string; // Transaction hash
|
|
105
|
+
from: string; // Sender bech32 address (noble1...)
|
|
106
|
+
to: string; // Recipient bech32 address (noble1...)
|
|
107
|
+
amount: string; // Amount in smallest units (uusdc)
|
|
108
|
+
denom?: string; // Token denomination (default: uusdc)
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Address Format
|
|
113
|
+
|
|
114
|
+
Noble uses bech32 addresses with the `noble` prefix:
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
noble1qg5ega6dykkxc307y25pecuufrjkxkaggkkxh7
|
|
118
|
+
noble1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## USDC on Noble
|
|
122
|
+
|
|
123
|
+
Noble is an appchain purpose-built for native USDC issuance in the Cosmos ecosystem:
|
|
124
|
+
|
|
125
|
+
- **Denomination**: `uusdc` (micro-USDC)
|
|
126
|
+
- **Decimals**: 6 (1 USDC = 1,000,000 uusdc)
|
|
127
|
+
- **Transfer**: Uses standard `cosmos.bank.v1beta1.MsgSend`
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// MsgSend structure
|
|
131
|
+
{
|
|
132
|
+
"@type": "/cosmos.bank.v1beta1.MsgSend",
|
|
133
|
+
fromAddress: "noble1sender...",
|
|
134
|
+
toAddress: "noble1recipient...",
|
|
135
|
+
amount: [{ denom: "uusdc", amount: "1000000" }]
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Gas Configuration
|
|
140
|
+
|
|
141
|
+
Default gas parameters:
|
|
142
|
+
|
|
143
|
+
- Gas limit: 200,000
|
|
144
|
+
- Gas price: 0.025 uusdc
|
|
145
|
+
- Default fee: 5,000 uusdc (0.005 USDC)
|
|
146
|
+
|
|
147
|
+
## Development
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
# Build
|
|
151
|
+
pnpm build
|
|
152
|
+
|
|
153
|
+
# Test
|
|
154
|
+
pnpm test
|
|
155
|
+
|
|
156
|
+
# Test with coverage
|
|
157
|
+
pnpm test:coverage
|
|
158
|
+
|
|
159
|
+
# Lint
|
|
160
|
+
pnpm lint
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Related Packages
|
|
164
|
+
|
|
165
|
+
- `@t402/core` - Core protocol types and client
|
|
166
|
+
- `@t402/fetch` - HTTP wrapper with automatic payment handling
|
|
167
|
+
- `@t402/evm` - EVM implementation
|
|
168
|
+
- `@t402/svm` - Solana implementation
|
|
169
|
+
- `@t402/ton` - TON implementation
|
|
170
|
+
- `@t402/tron` - TRON implementation
|
|
171
|
+
- `@t402/near` - NEAR Protocol implementation
|
|
172
|
+
- `@t402/aptos` - Aptos implementation
|
|
173
|
+
- `@t402/tezos` - Tezos implementation
|
|
174
|
+
- `@t402/polkadot` - Polkadot Asset Hub implementation
|
|
175
|
+
- `@t402/stacks` - Stacks implementation
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { SchemeNetworkClient, PaymentRequirements, PaymentPayload } from '@t402/core/types';
|
|
2
|
+
import { d as ClientCosmosSigner } from '../../types-CgseoctH.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Cosmos Client Scheme Implementation - Exact Direct
|
|
6
|
+
*
|
|
7
|
+
* Creates payment payloads for Cosmos MsgSend transfers using the exact-direct scheme.
|
|
8
|
+
* In this scheme, the client executes the bank send directly and provides
|
|
9
|
+
* the transaction hash as proof of payment.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for ExactDirectCosmosClient
|
|
14
|
+
*/
|
|
15
|
+
interface ExactDirectCosmosClientConfig {
|
|
16
|
+
/** Override the default denomination */
|
|
17
|
+
denom?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Cosmos client implementation for the Exact-Direct payment scheme.
|
|
21
|
+
*
|
|
22
|
+
* Executes a Cosmos MsgSend and returns the transaction hash as proof.
|
|
23
|
+
*/
|
|
24
|
+
declare class ExactDirectCosmosClient implements SchemeNetworkClient {
|
|
25
|
+
private readonly signer;
|
|
26
|
+
private readonly config;
|
|
27
|
+
readonly scheme = "exact-direct";
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new ExactDirectCosmosClient instance.
|
|
30
|
+
*
|
|
31
|
+
* @param signer - The Cosmos signer for client operations
|
|
32
|
+
* @param config - Optional configuration overrides
|
|
33
|
+
*/
|
|
34
|
+
constructor(signer: ClientCosmosSigner, config?: ExactDirectCosmosClientConfig);
|
|
35
|
+
/**
|
|
36
|
+
* Creates a payment payload by executing the transfer.
|
|
37
|
+
*
|
|
38
|
+
* Unlike other schemes where the client creates a signed message for
|
|
39
|
+
* the facilitator to execute, the exact-direct scheme has the client
|
|
40
|
+
* execute the transfer directly. The transaction hash is then used
|
|
41
|
+
* as proof of payment.
|
|
42
|
+
*
|
|
43
|
+
* @param t402Version - The t402 protocol version
|
|
44
|
+
* @param paymentRequirements - The payment requirements
|
|
45
|
+
* @returns Promise resolving to a payment payload with transaction hash
|
|
46
|
+
*/
|
|
47
|
+
createPaymentPayload(t402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, "t402Version" | "payload">>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { ExactDirectCosmosClient, type ExactDirectCosmosClientConfig };
|
|
@@ -0,0 +1,122 @@
|
|
|
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/exact-direct/client/index.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
ExactDirectCosmosClient: () => ExactDirectCosmosClient
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(client_exports);
|
|
26
|
+
|
|
27
|
+
// src/constants.ts
|
|
28
|
+
var NOBLE_MAINNET_CAIP2 = "cosmos:noble-1";
|
|
29
|
+
var NOBLE_TESTNET_CAIP2 = "cosmos:grand-1";
|
|
30
|
+
var NOBLE_MAINNET_RPC = "https://noble-rpc.polkachu.com";
|
|
31
|
+
var NOBLE_TESTNET_RPC = "https://rpc.testnet.noble.strange.love";
|
|
32
|
+
var NOBLE_MAINNET_REST = "https://noble-api.polkachu.com";
|
|
33
|
+
var NOBLE_TESTNET_REST = "https://api.testnet.noble.strange.love";
|
|
34
|
+
var NETWORK_RPC_ENDPOINTS = {
|
|
35
|
+
[NOBLE_MAINNET_CAIP2]: NOBLE_MAINNET_RPC,
|
|
36
|
+
[NOBLE_TESTNET_CAIP2]: NOBLE_TESTNET_RPC
|
|
37
|
+
};
|
|
38
|
+
var NETWORK_REST_ENDPOINTS = {
|
|
39
|
+
[NOBLE_MAINNET_CAIP2]: NOBLE_MAINNET_REST,
|
|
40
|
+
[NOBLE_TESTNET_CAIP2]: NOBLE_TESTNET_REST
|
|
41
|
+
};
|
|
42
|
+
var NOBLE_BECH32_PREFIX = "noble";
|
|
43
|
+
var USDC_DENOM = "uusdc";
|
|
44
|
+
var SCHEME_EXACT_DIRECT = "exact-direct";
|
|
45
|
+
var MAX_TRANSACTION_AGE = 5 * 60 * 1e3;
|
|
46
|
+
var COSMOS_CAIP2_NAMESPACE = "cosmos";
|
|
47
|
+
|
|
48
|
+
// src/utils.ts
|
|
49
|
+
function normalizeNetwork(network) {
|
|
50
|
+
if (network.startsWith(`${COSMOS_CAIP2_NAMESPACE}:`)) {
|
|
51
|
+
return network;
|
|
52
|
+
}
|
|
53
|
+
return `${COSMOS_CAIP2_NAMESPACE}:${network}`;
|
|
54
|
+
}
|
|
55
|
+
function isValidAddress(address) {
|
|
56
|
+
if (!address || address.length < 10) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return address.startsWith(`${NOBLE_BECH32_PREFIX}1`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/exact-direct/client/scheme.ts
|
|
63
|
+
var ExactDirectCosmosClient = class {
|
|
64
|
+
/**
|
|
65
|
+
* Creates a new ExactDirectCosmosClient instance.
|
|
66
|
+
*
|
|
67
|
+
* @param signer - The Cosmos signer for client operations
|
|
68
|
+
* @param config - Optional configuration overrides
|
|
69
|
+
*/
|
|
70
|
+
constructor(signer, config = {}) {
|
|
71
|
+
this.signer = signer;
|
|
72
|
+
this.config = config;
|
|
73
|
+
this.scheme = SCHEME_EXACT_DIRECT;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Creates a payment payload by executing the transfer.
|
|
77
|
+
*
|
|
78
|
+
* Unlike other schemes where the client creates a signed message for
|
|
79
|
+
* the facilitator to execute, the exact-direct scheme has the client
|
|
80
|
+
* execute the transfer directly. The transaction hash is then used
|
|
81
|
+
* as proof of payment.
|
|
82
|
+
*
|
|
83
|
+
* @param t402Version - The t402 protocol version
|
|
84
|
+
* @param paymentRequirements - The payment requirements
|
|
85
|
+
* @returns Promise resolving to a payment payload with transaction hash
|
|
86
|
+
*/
|
|
87
|
+
async createPaymentPayload(t402Version, paymentRequirements) {
|
|
88
|
+
const network = normalizeNetwork(paymentRequirements.network);
|
|
89
|
+
if (!paymentRequirements.payTo) {
|
|
90
|
+
throw new Error("PayTo address is required");
|
|
91
|
+
}
|
|
92
|
+
if (!paymentRequirements.amount) {
|
|
93
|
+
throw new Error("Amount is required");
|
|
94
|
+
}
|
|
95
|
+
if (!isValidAddress(paymentRequirements.payTo)) {
|
|
96
|
+
throw new Error(`Invalid recipient address: ${paymentRequirements.payTo}`);
|
|
97
|
+
}
|
|
98
|
+
if (!isValidAddress(this.signer.address)) {
|
|
99
|
+
throw new Error(`Invalid sender address: ${this.signer.address}`);
|
|
100
|
+
}
|
|
101
|
+
const recipient = paymentRequirements.payTo;
|
|
102
|
+
const amount = paymentRequirements.amount;
|
|
103
|
+
const denom = paymentRequirements.extra?.denom || this.config.denom || USDC_DENOM;
|
|
104
|
+
const txHash = await this.signer.sendTokens(network, recipient, amount, denom);
|
|
105
|
+
const payload = {
|
|
106
|
+
txHash,
|
|
107
|
+
from: this.signer.address,
|
|
108
|
+
to: recipient,
|
|
109
|
+
amount,
|
|
110
|
+
denom
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
t402Version,
|
|
114
|
+
payload
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
ExactDirectCosmosClient
|
|
121
|
+
});
|
|
122
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/exact-direct/client/index.ts","../../../../src/constants.ts","../../../../src/utils.ts","../../../../src/exact-direct/client/scheme.ts"],"sourcesContent":["/**\n * Cosmos Exact-Direct Client Exports\n */\n\nexport { ExactDirectCosmosClient, type ExactDirectCosmosClientConfig } from \"./scheme.js\";\n","/**\n * Cosmos Network Constants\n *\n * This module provides constants for Cosmos blockchain integration including:\n * - CAIP-2 network identifiers (Noble chain)\n * - RPC and REST endpoints\n * - Bech32 prefix configuration\n * - USDC denomination and gas parameters\n */\n\n/**\n * CAIP-2 Network Identifiers for Cosmos (Noble)\n */\nexport const NOBLE_MAINNET_CAIP2 = \"cosmos:noble-1\";\nexport const NOBLE_TESTNET_CAIP2 = \"cosmos:grand-1\";\n\n/**\n * Supported Cosmos networks\n */\nexport const COSMOS_NETWORKS = [NOBLE_MAINNET_CAIP2, NOBLE_TESTNET_CAIP2] as const;\n\nexport type CosmosNetwork = (typeof COSMOS_NETWORKS)[number];\n\n/**\n * Default RPC endpoints\n */\nexport const NOBLE_MAINNET_RPC = \"https://noble-rpc.polkachu.com\";\nexport const NOBLE_TESTNET_RPC = \"https://rpc.testnet.noble.strange.love\";\n\n/**\n * Default REST endpoints\n */\nexport const NOBLE_MAINNET_REST = \"https://noble-api.polkachu.com\";\nexport const NOBLE_TESTNET_REST = \"https://api.testnet.noble.strange.love\";\n\n/**\n * Network RPC endpoint mapping\n */\nexport const NETWORK_RPC_ENDPOINTS: Record<string, string> = {\n [NOBLE_MAINNET_CAIP2]: NOBLE_MAINNET_RPC,\n [NOBLE_TESTNET_CAIP2]: NOBLE_TESTNET_RPC,\n};\n\n/**\n * Network REST endpoint mapping\n */\nexport const NETWORK_REST_ENDPOINTS: Record<string, string> = {\n [NOBLE_MAINNET_CAIP2]: NOBLE_MAINNET_REST,\n [NOBLE_TESTNET_CAIP2]: NOBLE_TESTNET_REST,\n};\n\n/**\n * Bech32 prefix for Noble addresses\n */\nexport const NOBLE_BECH32_PREFIX = \"noble\";\n\n/**\n * USDC denomination on Noble\n * uusdc = micro-USDC (1 USDC = 1,000,000 uusdc)\n */\nexport const USDC_DENOM = \"uusdc\";\n\n/**\n * Default gas parameters\n */\nexport const DEFAULT_GAS_LIMIT = 200000;\nexport const DEFAULT_GAS_PRICE = \"0.025uusdc\";\nexport const DEFAULT_FEE_AMOUNT = \"5000\"; // 0.005 USDC\n\n/**\n * Cosmos message type for bank send\n */\nexport const MSG_TYPE_SEND = \"/cosmos.bank.v1beta1.MsgSend\";\n\n/**\n * Scheme identifier for exact-direct payments\n */\nexport const SCHEME_EXACT_DIRECT = \"exact-direct\";\n\n/**\n * Maximum transaction age to accept (in milliseconds)\n */\nexport const MAX_TRANSACTION_AGE = 5 * 60 * 1000; // 5 minutes\n\n/**\n * CAIP-2 namespace for Cosmos\n */\nexport const COSMOS_CAIP2_NAMESPACE = \"cosmos\";\n","/**\n * Cosmos Utility Functions\n *\n * Helper functions for Cosmos address validation, network normalization,\n * and amount conversion.\n */\n\nimport {\n COSMOS_CAIP2_NAMESPACE,\n NOBLE_BECH32_PREFIX,\n NETWORK_RPC_ENDPOINTS,\n NETWORK_REST_ENDPOINTS,\n type CosmosNetwork,\n} from \"./constants.js\";\n\n/**\n * Normalize a network identifier to CAIP-2 format\n * @param network - Network identifier (e.g., \"noble-1\", \"cosmos:noble-1\")\n * @returns CAIP-2 formatted network identifier\n */\nexport function normalizeNetwork(network: string): CosmosNetwork {\n // Already in CAIP-2 format\n if (network.startsWith(`${COSMOS_CAIP2_NAMESPACE}:`)) {\n return network as CosmosNetwork;\n }\n // Convert shorthand to CAIP-2\n return `${COSMOS_CAIP2_NAMESPACE}:${network}` as CosmosNetwork;\n}\n\n/**\n * Extract network ID from CAIP-2 identifier\n * @param network - CAIP-2 network identifier\n * @returns Network ID (e.g., \"noble-1\")\n */\nexport function extractNetworkId(network: string): string {\n if (network.includes(\":\")) {\n return network.split(\":\")[1];\n }\n return network;\n}\n\n/**\n * Validate a Cosmos bech32 address with Noble prefix\n * Checks that the address starts with the \"noble\" prefix followed by \"1\"\n * @param address - Address to validate\n * @returns Whether the address is valid\n */\nexport function isValidAddress(address: string): boolean {\n if (!address || address.length < 10) {\n return false;\n }\n return address.startsWith(`${NOBLE_BECH32_PREFIX}1`);\n}\n\n/**\n * Get RPC endpoint for a network\n * @param network - CAIP-2 network identifier\n * @returns RPC endpoint URL\n */\nexport function getRpcEndpoint(network: string): string {\n const normalizedNetwork = normalizeNetwork(network);\n return NETWORK_RPC_ENDPOINTS[normalizedNetwork] || NETWORK_RPC_ENDPOINTS[\"cosmos:noble-1\"];\n}\n\n/**\n * Get REST endpoint for a network\n * @param network - CAIP-2 network identifier\n * @returns REST endpoint URL\n */\nexport function getRestEndpoint(network: string): string {\n const normalizedNetwork = normalizeNetwork(network);\n return NETWORK_REST_ENDPOINTS[normalizedNetwork] || NETWORK_REST_ENDPOINTS[\"cosmos:noble-1\"];\n}\n\n/**\n * Convert decimal amount to token units (atomic units)\n * @param decimalAmount - Amount with decimals (e.g., \"1.50\")\n * @param decimals - Token decimals\n * @returns Amount in smallest units\n */\nexport function toAtomicUnits(decimalAmount: string | number, decimals: number): bigint {\n const amount = typeof decimalAmount === \"string\" ? parseFloat(decimalAmount) : decimalAmount;\n if (isNaN(amount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const multiplier = 10 ** decimals;\n return BigInt(Math.floor(amount * multiplier));\n}\n\n/**\n * Format amount for display (with decimals)\n * @param amount - Amount in smallest units\n * @param decimals - Number of decimal places\n * @returns Formatted amount string\n */\nexport function formatAmount(amount: bigint, decimals: number): string {\n const divisor = BigInt(10 ** decimals);\n const whole = amount / divisor;\n const remainder = amount % divisor;\n const decimal = remainder.toString().padStart(decimals, \"0\").slice(0, 2);\n return `${whole}.${decimal}`;\n}\n\n/**\n * Get the list of supported networks\n * @returns Array of CAIP-2 network identifiers\n */\nexport function getSupportedNetworks(): string[] {\n return Object.keys(NETWORK_RPC_ENDPOINTS);\n}\n","/**\n * Cosmos Client Scheme Implementation - Exact Direct\n *\n * Creates payment payloads for Cosmos MsgSend transfers using the exact-direct scheme.\n * In this scheme, the client executes the bank send directly and provides\n * the transaction hash as proof of payment.\n */\n\nimport type { PaymentPayload, PaymentRequirements, SchemeNetworkClient } from \"@t402/core/types\";\nimport type { ClientCosmosSigner, ExactDirectCosmosPayload } from \"../../types.js\";\nimport { SCHEME_EXACT_DIRECT, USDC_DENOM } from \"../../constants.js\";\nimport { normalizeNetwork, isValidAddress } from \"../../utils.js\";\n\n/**\n * Configuration for ExactDirectCosmosClient\n */\nexport interface ExactDirectCosmosClientConfig {\n /** Override the default denomination */\n denom?: string;\n}\n\n/**\n * Cosmos client implementation for the Exact-Direct payment scheme.\n *\n * Executes a Cosmos MsgSend and returns the transaction hash as proof.\n */\nexport class ExactDirectCosmosClient implements SchemeNetworkClient {\n readonly scheme = SCHEME_EXACT_DIRECT;\n\n /**\n * Creates a new ExactDirectCosmosClient instance.\n *\n * @param signer - The Cosmos signer for client operations\n * @param config - Optional configuration overrides\n */\n constructor(\n private readonly signer: ClientCosmosSigner,\n private readonly config: ExactDirectCosmosClientConfig = {},\n ) {}\n\n /**\n * Creates a payment payload by executing the transfer.\n *\n * Unlike other schemes where the client creates a signed message for\n * the facilitator to execute, the exact-direct scheme has the client\n * execute the transfer directly. The transaction hash is then used\n * as proof of payment.\n *\n * @param t402Version - The t402 protocol version\n * @param paymentRequirements - The payment requirements\n * @returns Promise resolving to a payment payload with transaction hash\n */\n async createPaymentPayload(\n t402Version: number,\n paymentRequirements: PaymentRequirements,\n ): Promise<Pick<PaymentPayload, \"t402Version\" | \"payload\">> {\n // Normalize and validate network\n const network = normalizeNetwork(paymentRequirements.network);\n\n // Validate required fields\n if (!paymentRequirements.payTo) {\n throw new Error(\"PayTo address is required\");\n }\n if (!paymentRequirements.amount) {\n throw new Error(\"Amount is required\");\n }\n\n // Validate addresses\n if (!isValidAddress(paymentRequirements.payTo)) {\n throw new Error(`Invalid recipient address: ${paymentRequirements.payTo}`);\n }\n if (!isValidAddress(this.signer.address)) {\n throw new Error(`Invalid sender address: ${this.signer.address}`);\n }\n\n const recipient = paymentRequirements.payTo;\n const amount = paymentRequirements.amount;\n\n // Determine denomination from extra field, config, or default\n const denom =\n (paymentRequirements.extra?.denom as string) || this.config.denom || USDC_DENOM;\n\n // Execute the transfer\n const txHash = await this.signer.sendTokens(network, recipient, amount, denom);\n\n // Build the payload\n const payload: ExactDirectCosmosPayload = {\n txHash,\n from: this.signer.address,\n to: recipient,\n amount: amount,\n denom,\n };\n\n return {\n t402Version,\n payload,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaO,IAAM,sBAAsB;AAC5B,IAAM,sBAAsB;AAY5B,IAAM,oBAAoB;AAC1B,IAAM,oBAAoB;AAK1B,IAAM,qBAAqB;AAC3B,IAAM,qBAAqB;AAK3B,IAAM,wBAAgD;AAAA,EAC3D,CAAC,mBAAmB,GAAG;AAAA,EACvB,CAAC,mBAAmB,GAAG;AACzB;AAKO,IAAM,yBAAiD;AAAA,EAC5D,CAAC,mBAAmB,GAAG;AAAA,EACvB,CAAC,mBAAmB,GAAG;AACzB;AAKO,IAAM,sBAAsB;AAM5B,IAAM,aAAa;AAiBnB,IAAM,sBAAsB;AAK5B,IAAM,sBAAsB,IAAI,KAAK;AAKrC,IAAM,yBAAyB;;;ACnE/B,SAAS,iBAAiB,SAAgC;AAE/D,MAAI,QAAQ,WAAW,GAAG,sBAAsB,GAAG,GAAG;AACpD,WAAO;AAAA,EACT;AAEA,SAAO,GAAG,sBAAsB,IAAI,OAAO;AAC7C;AAoBO,SAAS,eAAe,SAA0B;AACvD,MAAI,CAAC,WAAW,QAAQ,SAAS,IAAI;AACnC,WAAO;AAAA,EACT;AACA,SAAO,QAAQ,WAAW,GAAG,mBAAmB,GAAG;AACrD;;;AC1BO,IAAM,0BAAN,MAA6D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASlE,YACmB,QACA,SAAwC,CAAC,GAC1D;AAFiB;AACA;AAVnB,SAAS,SAAS;AAAA,EAWf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcH,MAAM,qBACJ,aACA,qBAC0D;AAE1D,UAAM,UAAU,iBAAiB,oBAAoB,OAAO;AAG5D,QAAI,CAAC,oBAAoB,OAAO;AAC9B,YAAM,IAAI,MAAM,2BAA2B;AAAA,IAC7C;AACA,QAAI,CAAC,oBAAoB,QAAQ;AAC/B,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACtC;AAGA,QAAI,CAAC,eAAe,oBAAoB,KAAK,GAAG;AAC9C,YAAM,IAAI,MAAM,8BAA8B,oBAAoB,KAAK,EAAE;AAAA,IAC3E;AACA,QAAI,CAAC,eAAe,KAAK,OAAO,OAAO,GAAG;AACxC,YAAM,IAAI,MAAM,2BAA2B,KAAK,OAAO,OAAO,EAAE;AAAA,IAClE;AAEA,UAAM,YAAY,oBAAoB;AACtC,UAAM,SAAS,oBAAoB;AAGnC,UAAM,QACH,oBAAoB,OAAO,SAAoB,KAAK,OAAO,SAAS;AAGvE,UAAM,SAAS,MAAM,KAAK,OAAO,WAAW,SAAS,WAAW,QAAQ,KAAK;AAG7E,UAAM,UAAoC;AAAA,MACxC;AAAA,MACA,MAAM,KAAK,OAAO;AAAA,MAClB,IAAI;AAAA,MACJ;AAAA,MACA;AAAA,IACF;AAEA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { SchemeNetworkFacilitator, Network, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse } from '@t402/core/types';
|
|
2
|
+
import { F as FacilitatorCosmosSigner } from '../../types-CgseoctH.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Cosmos Facilitator Scheme Implementation - Exact Direct
|
|
6
|
+
*
|
|
7
|
+
* Verifies and settles Cosmos (Noble USDC) payments using the exact-direct scheme.
|
|
8
|
+
* The facilitator verifies that the client's transaction was successful
|
|
9
|
+
* and matches the payment requirements.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for ExactDirectCosmosFacilitator
|
|
14
|
+
*/
|
|
15
|
+
interface ExactDirectCosmosFacilitatorConfig {
|
|
16
|
+
/** Maximum age of a transaction to accept (in milliseconds) */
|
|
17
|
+
maxTransactionAge?: number;
|
|
18
|
+
/** Duration to cache used transaction hashes (in milliseconds) */
|
|
19
|
+
usedTxCacheDuration?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Cosmos facilitator implementation for the Exact-Direct payment scheme.
|
|
23
|
+
* Verifies transaction proofs and confirms payments.
|
|
24
|
+
*/
|
|
25
|
+
declare class ExactDirectCosmosFacilitator implements SchemeNetworkFacilitator {
|
|
26
|
+
private readonly signer;
|
|
27
|
+
readonly scheme = "exact-direct";
|
|
28
|
+
readonly caipFamily = "cosmos:*";
|
|
29
|
+
private readonly config;
|
|
30
|
+
private usedTxs;
|
|
31
|
+
constructor(signer: FacilitatorCosmosSigner, config?: ExactDirectCosmosFacilitatorConfig);
|
|
32
|
+
/**
|
|
33
|
+
* Get extra data for a supported kind
|
|
34
|
+
*/
|
|
35
|
+
getExtra(network: Network): Record<string, unknown> | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Get signer addresses for a network
|
|
38
|
+
*/
|
|
39
|
+
getSigners(network: Network): string[];
|
|
40
|
+
/**
|
|
41
|
+
* Verify a payment payload
|
|
42
|
+
*/
|
|
43
|
+
verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
44
|
+
/**
|
|
45
|
+
* Settle a payment - for exact-direct, the transfer is already complete
|
|
46
|
+
*/
|
|
47
|
+
settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
|
|
48
|
+
/**
|
|
49
|
+
* Find a MsgSend in transaction messages
|
|
50
|
+
*/
|
|
51
|
+
private findMsgSend;
|
|
52
|
+
/**
|
|
53
|
+
* Get a specific coin amount from a MsgSend by denomination
|
|
54
|
+
*/
|
|
55
|
+
private getAmountByDenom;
|
|
56
|
+
/**
|
|
57
|
+
* Check if a transaction has been used
|
|
58
|
+
*/
|
|
59
|
+
private isTxUsed;
|
|
60
|
+
/**
|
|
61
|
+
* Mark a transaction as used
|
|
62
|
+
*/
|
|
63
|
+
private markTxUsed;
|
|
64
|
+
/**
|
|
65
|
+
* Start the cleanup interval for used transactions
|
|
66
|
+
*/
|
|
67
|
+
private startCleanupInterval;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { ExactDirectCosmosFacilitator, type ExactDirectCosmosFacilitatorConfig };
|