@4mica/sdk 1.0.0 → 1.0.2
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 +17 -0
- package/dist/contract.d.ts +1 -0
- package/dist/contract.js +44 -18
- package/package.json +7 -2
- package/dist/abi/core4mica.json +0 -1605
- package/dist/abi/erc20.json +0 -187
- package/dist/client.d.ts +0 -56
- package/dist/client.js +0 -258
- package/dist/src/abi/core4mica.json +0 -1605
- package/dist/src/abi/erc20.json +0 -187
- package/dist/src/bls.d.ts +0 -5
- package/dist/src/bls.js +0 -45
- package/dist/src/client.d.ts +0 -55
- package/dist/src/client.js +0 -214
- package/dist/src/config.d.ts +0 -21
- package/dist/src/config.js +0 -76
- package/dist/src/contract.d.ts +0 -33
- package/dist/src/contract.js +0 -121
- package/dist/src/errors.d.ts +0 -17
- package/dist/src/errors.js +0 -31
- package/dist/src/guarantee.d.ts +0 -3
- package/dist/src/guarantee.js +0 -66
- package/dist/src/index.d.ts +0 -11
- package/dist/src/index.js +0 -27
- package/dist/src/models.d.ts +0 -119
- package/dist/src/models.js +0 -132
- package/dist/src/rpc.d.ts +0 -30
- package/dist/src/rpc.js +0 -122
- package/dist/src/signing.d.ts +0 -16
- package/dist/src/signing.js +0 -102
- package/dist/src/utils.d.ts +0 -8
- package/dist/src/utils.js +0 -78
- package/dist/src/x402.d.ts +0 -77
- package/dist/src/x402.js +0 -214
- package/dist/tests/config.test.d.ts +0 -1
- package/dist/tests/config.test.js +0 -26
- package/dist/tests/guarantee.test.d.ts +0 -1
- package/dist/tests/guarantee.test.js +0 -26
- package/dist/tests/rpc.test.d.ts +0 -1
- package/dist/tests/rpc.test.js +0 -44
- package/dist/tests/signing.test.d.ts +0 -1
- package/dist/tests/signing.test.js +0 -25
- package/dist/tests/utils.test.d.ts +0 -1
- package/dist/tests/utils.test.js +0 -25
- package/dist/tests/x402.test.d.ts +0 -1
- package/dist/tests/x402.test.js +0 -65
- package/dist/x402.d.ts +0 -78
- package/dist/x402.js +0 -231
package/dist/src/abi/erc20.json
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"abi": [
|
|
3
|
-
{
|
|
4
|
-
"type": "function",
|
|
5
|
-
"name": "allowance",
|
|
6
|
-
"inputs": [
|
|
7
|
-
{
|
|
8
|
-
"name": "owner",
|
|
9
|
-
"type": "address",
|
|
10
|
-
"internalType": "address"
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
"name": "spender",
|
|
14
|
-
"type": "address",
|
|
15
|
-
"internalType": "address"
|
|
16
|
-
}
|
|
17
|
-
],
|
|
18
|
-
"outputs": [
|
|
19
|
-
{
|
|
20
|
-
"name": "",
|
|
21
|
-
"type": "uint256",
|
|
22
|
-
"internalType": "uint256"
|
|
23
|
-
}
|
|
24
|
-
],
|
|
25
|
-
"stateMutability": "view"
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
"type": "function",
|
|
29
|
-
"name": "approve",
|
|
30
|
-
"inputs": [
|
|
31
|
-
{
|
|
32
|
-
"name": "spender",
|
|
33
|
-
"type": "address",
|
|
34
|
-
"internalType": "address"
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
"name": "value",
|
|
38
|
-
"type": "uint256",
|
|
39
|
-
"internalType": "uint256"
|
|
40
|
-
}
|
|
41
|
-
],
|
|
42
|
-
"outputs": [
|
|
43
|
-
{
|
|
44
|
-
"name": "",
|
|
45
|
-
"type": "bool",
|
|
46
|
-
"internalType": "bool"
|
|
47
|
-
}
|
|
48
|
-
],
|
|
49
|
-
"stateMutability": "nonpayable"
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
"type": "function",
|
|
53
|
-
"name": "balanceOf",
|
|
54
|
-
"inputs": [
|
|
55
|
-
{
|
|
56
|
-
"name": "account",
|
|
57
|
-
"type": "address",
|
|
58
|
-
"internalType": "address"
|
|
59
|
-
}
|
|
60
|
-
],
|
|
61
|
-
"outputs": [
|
|
62
|
-
{
|
|
63
|
-
"name": "",
|
|
64
|
-
"type": "uint256",
|
|
65
|
-
"internalType": "uint256"
|
|
66
|
-
}
|
|
67
|
-
],
|
|
68
|
-
"stateMutability": "view"
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
"type": "function",
|
|
72
|
-
"name": "totalSupply",
|
|
73
|
-
"inputs": [],
|
|
74
|
-
"outputs": [
|
|
75
|
-
{
|
|
76
|
-
"name": "",
|
|
77
|
-
"type": "uint256",
|
|
78
|
-
"internalType": "uint256"
|
|
79
|
-
}
|
|
80
|
-
],
|
|
81
|
-
"stateMutability": "view"
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
"type": "function",
|
|
85
|
-
"name": "transfer",
|
|
86
|
-
"inputs": [
|
|
87
|
-
{
|
|
88
|
-
"name": "to",
|
|
89
|
-
"type": "address",
|
|
90
|
-
"internalType": "address"
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
"name": "value",
|
|
94
|
-
"type": "uint256",
|
|
95
|
-
"internalType": "uint256"
|
|
96
|
-
}
|
|
97
|
-
],
|
|
98
|
-
"outputs": [
|
|
99
|
-
{
|
|
100
|
-
"name": "",
|
|
101
|
-
"type": "bool",
|
|
102
|
-
"internalType": "bool"
|
|
103
|
-
}
|
|
104
|
-
],
|
|
105
|
-
"stateMutability": "nonpayable"
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
"type": "function",
|
|
109
|
-
"name": "transferFrom",
|
|
110
|
-
"inputs": [
|
|
111
|
-
{
|
|
112
|
-
"name": "from",
|
|
113
|
-
"type": "address",
|
|
114
|
-
"internalType": "address"
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
"name": "to",
|
|
118
|
-
"type": "address",
|
|
119
|
-
"internalType": "address"
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
"name": "value",
|
|
123
|
-
"type": "uint256",
|
|
124
|
-
"internalType": "uint256"
|
|
125
|
-
}
|
|
126
|
-
],
|
|
127
|
-
"outputs": [
|
|
128
|
-
{
|
|
129
|
-
"name": "",
|
|
130
|
-
"type": "bool",
|
|
131
|
-
"internalType": "bool"
|
|
132
|
-
}
|
|
133
|
-
],
|
|
134
|
-
"stateMutability": "nonpayable"
|
|
135
|
-
},
|
|
136
|
-
{
|
|
137
|
-
"type": "event",
|
|
138
|
-
"name": "Approval",
|
|
139
|
-
"inputs": [
|
|
140
|
-
{
|
|
141
|
-
"name": "owner",
|
|
142
|
-
"type": "address",
|
|
143
|
-
"indexed": true,
|
|
144
|
-
"internalType": "address"
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
"name": "spender",
|
|
148
|
-
"type": "address",
|
|
149
|
-
"indexed": true,
|
|
150
|
-
"internalType": "address"
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
"name": "value",
|
|
154
|
-
"type": "uint256",
|
|
155
|
-
"indexed": false,
|
|
156
|
-
"internalType": "uint256"
|
|
157
|
-
}
|
|
158
|
-
],
|
|
159
|
-
"anonymous": false
|
|
160
|
-
},
|
|
161
|
-
{
|
|
162
|
-
"type": "event",
|
|
163
|
-
"name": "Transfer",
|
|
164
|
-
"inputs": [
|
|
165
|
-
{
|
|
166
|
-
"name": "from",
|
|
167
|
-
"type": "address",
|
|
168
|
-
"indexed": true,
|
|
169
|
-
"internalType": "address"
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
"name": "to",
|
|
173
|
-
"type": "address",
|
|
174
|
-
"indexed": true,
|
|
175
|
-
"internalType": "address"
|
|
176
|
-
},
|
|
177
|
-
{
|
|
178
|
-
"name": "value",
|
|
179
|
-
"type": "uint256",
|
|
180
|
-
"indexed": false,
|
|
181
|
-
"internalType": "uint256"
|
|
182
|
-
}
|
|
183
|
-
],
|
|
184
|
-
"anonymous": false
|
|
185
|
-
}
|
|
186
|
-
]
|
|
187
|
-
}
|
package/dist/src/bls.d.ts
DELETED
package/dist/src/bls.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.signatureToWords = signatureToWords;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const errors_1 = require("./errors");
|
|
6
|
-
function splitFp(value) {
|
|
7
|
-
const be48 = value.toString(16).padStart(96, '0');
|
|
8
|
-
const bytes = (0, ethers_1.getBytes)(`0x${be48}`);
|
|
9
|
-
const hi = new Uint8Array(32);
|
|
10
|
-
hi.set(bytes.slice(0, 16), 16);
|
|
11
|
-
const lo = bytes.slice(16);
|
|
12
|
-
return [hi, lo];
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Expand a compressed G2 signature into the tuple expected by the contract.
|
|
16
|
-
* This mirrors the Python helper and requires the optional `@noble/curves` dependency.
|
|
17
|
-
*/
|
|
18
|
-
function signatureToWords(signatureHex) {
|
|
19
|
-
let curves;
|
|
20
|
-
try {
|
|
21
|
-
// Lazy require to keep the dependency optional.
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
23
|
-
curves = require('@noble/curves/bls12-381');
|
|
24
|
-
}
|
|
25
|
-
catch (err) {
|
|
26
|
-
throw new errors_1.VerificationError('BLS decoding requires @noble/curves; install it to enable remuneration');
|
|
27
|
-
}
|
|
28
|
-
try {
|
|
29
|
-
const sigBytes = (0, ethers_1.getBytes)(signatureHex);
|
|
30
|
-
const point = curves.bls12_381.G2.ProjectivePoint.fromHex(sigBytes);
|
|
31
|
-
const affine = point.toAffine();
|
|
32
|
-
const [x0, x1] = affine.x.c;
|
|
33
|
-
const [y0, y1] = affine.y.c;
|
|
34
|
-
const coords = [x0, x1, y0, y1].map((fp) => BigInt(fp.value ?? fp));
|
|
35
|
-
const words = [];
|
|
36
|
-
coords.forEach((coord) => {
|
|
37
|
-
const [hi, lo] = splitFp(coord);
|
|
38
|
-
words.push(hi, lo);
|
|
39
|
-
});
|
|
40
|
-
return words;
|
|
41
|
-
}
|
|
42
|
-
catch (err) {
|
|
43
|
-
throw new errors_1.VerificationError(`invalid BLS signature: ${err?.message ?? String(err)}`);
|
|
44
|
-
}
|
|
45
|
-
}
|
package/dist/src/client.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Config } from './config';
|
|
2
|
-
import { ContractGateway } from './contract';
|
|
3
|
-
import { AssetBalanceInfo, BLSCert, CollateralEventInfo, GuaranteeInfo, PaymentGuaranteeClaims, PaymentGuaranteeRequestClaims, PaymentSignature, PendingRemunerationInfo, RecipientPaymentInfo, SigningScheme, TabInfo, TabPaymentStatus, UserInfo } from './models';
|
|
4
|
-
import { RpcProxy } from './rpc';
|
|
5
|
-
import { CorePublicParameters, PaymentSigner } from './signing';
|
|
6
|
-
export declare class Client {
|
|
7
|
-
readonly rpc: RpcProxy;
|
|
8
|
-
readonly params: CorePublicParameters;
|
|
9
|
-
readonly gateway: ContractGateway;
|
|
10
|
-
readonly guaranteeDomain: string;
|
|
11
|
-
readonly user: UserClient;
|
|
12
|
-
readonly recipient: RecipientClient;
|
|
13
|
-
readonly signer: PaymentSigner;
|
|
14
|
-
private constructor();
|
|
15
|
-
static new(cfg: Config): Promise<Client>;
|
|
16
|
-
private static buildGateway;
|
|
17
|
-
private static validateChainId;
|
|
18
|
-
aclose(): Promise<void>;
|
|
19
|
-
}
|
|
20
|
-
export declare class UserClient {
|
|
21
|
-
private client;
|
|
22
|
-
constructor(client: Client);
|
|
23
|
-
get guaranteeDomain(): string;
|
|
24
|
-
approveErc20(token: string, amount: number | bigint | string): Promise<any>;
|
|
25
|
-
deposit(amount: number | bigint | string, erc20Token?: string): Promise<any>;
|
|
26
|
-
getUser(): Promise<UserInfo[]>;
|
|
27
|
-
getTabPaymentStatus(tabId: number | bigint): Promise<TabPaymentStatus>;
|
|
28
|
-
signPayment(claims: PaymentGuaranteeRequestClaims, scheme?: SigningScheme): Promise<PaymentSignature>;
|
|
29
|
-
payTab(tabId: number | bigint, reqId: number | bigint, amount: number | bigint | string, recipientAddress: string, erc20Token?: string): Promise<any>;
|
|
30
|
-
requestWithdrawal(amount: number | bigint | string, erc20Token?: string): Promise<any>;
|
|
31
|
-
cancelWithdrawal(erc20Token?: string): Promise<any>;
|
|
32
|
-
finalizeWithdrawal(erc20Token?: string): Promise<any>;
|
|
33
|
-
}
|
|
34
|
-
export declare class RecipientClient {
|
|
35
|
-
private client;
|
|
36
|
-
constructor(client: Client);
|
|
37
|
-
private get recipientAddress();
|
|
38
|
-
get guaranteeDomain(): string;
|
|
39
|
-
private checkSigner;
|
|
40
|
-
createTab(userAddress: string, recipientAddress: string, erc20Token: string | undefined | null, ttl?: number | null): Promise<bigint>;
|
|
41
|
-
getTabPaymentStatus(tabId: number | bigint): Promise<TabPaymentStatus>;
|
|
42
|
-
issuePaymentGuarantee(claims: PaymentGuaranteeRequestClaims, signature: string, scheme: SigningScheme): Promise<BLSCert>;
|
|
43
|
-
verifyPaymentGuarantee(cert: BLSCert): PaymentGuaranteeClaims;
|
|
44
|
-
remunerate(cert: BLSCert): Promise<any>;
|
|
45
|
-
listSettledTabs(): Promise<TabInfo[]>;
|
|
46
|
-
listPendingRemunerations(): Promise<PendingRemunerationInfo[]>;
|
|
47
|
-
getTab(tabId: number | bigint): Promise<TabInfo | null>;
|
|
48
|
-
listRecipientTabs(settlementStatuses?: string[]): Promise<TabInfo[]>;
|
|
49
|
-
getTabGuarantees(tabId: number | bigint): Promise<GuaranteeInfo[]>;
|
|
50
|
-
getLatestGuarantee(tabId: number | bigint): Promise<GuaranteeInfo | null>;
|
|
51
|
-
getGuarantee(tabId: number | bigint, reqId: number | bigint): Promise<GuaranteeInfo | null>;
|
|
52
|
-
listRecipientPayments(): Promise<RecipientPaymentInfo[]>;
|
|
53
|
-
getCollateralEventsForTab(tabId: number | bigint): Promise<CollateralEventInfo[]>;
|
|
54
|
-
getUserAssetBalance(userAddress: string, assetAddress: string): Promise<AssetBalanceInfo | null>;
|
|
55
|
-
}
|
package/dist/src/client.js
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RecipientClient = exports.UserClient = exports.Client = void 0;
|
|
4
|
-
const bls_1 = require("./bls");
|
|
5
|
-
const contract_1 = require("./contract");
|
|
6
|
-
const errors_1 = require("./errors");
|
|
7
|
-
const guarantee_1 = require("./guarantee");
|
|
8
|
-
const models_1 = require("./models");
|
|
9
|
-
const rpc_1 = require("./rpc");
|
|
10
|
-
const signing_1 = require("./signing");
|
|
11
|
-
const utils_1 = require("./utils");
|
|
12
|
-
function tabStatusFromRpc(status) {
|
|
13
|
-
const paid = status.paid !== undefined ? status.paid : (status.paidAmount ?? 0);
|
|
14
|
-
const remunerated = status.remunerated !== undefined ? status.remunerated : (status.paidOut ?? false);
|
|
15
|
-
const asset = status.asset ?? status.assetAddress;
|
|
16
|
-
return {
|
|
17
|
-
paid: (0, utils_1.parseU256)(paid),
|
|
18
|
-
remunerated: Boolean(remunerated),
|
|
19
|
-
asset,
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
class Client {
|
|
23
|
-
constructor(cfg, rpc, params, gateway, guaranteeDomain, signer) {
|
|
24
|
-
this.rpc = rpc;
|
|
25
|
-
this.params = params;
|
|
26
|
-
this.gateway = gateway;
|
|
27
|
-
this.guaranteeDomain = guaranteeDomain;
|
|
28
|
-
this.signer = signer;
|
|
29
|
-
this.user = new UserClient(this);
|
|
30
|
-
this.recipient = new RecipientClient(this);
|
|
31
|
-
}
|
|
32
|
-
static async new(cfg) {
|
|
33
|
-
const rpc = new rpc_1.RpcProxy(cfg.rpcUrl, cfg.adminApiKey);
|
|
34
|
-
const params = await rpc.getPublicParams();
|
|
35
|
-
const gateway = Client.buildGateway(cfg, params);
|
|
36
|
-
await Client.validateChainId(gateway, params.chainId);
|
|
37
|
-
const guaranteeDomain = await gateway.getGuaranteeDomain();
|
|
38
|
-
const signer = new signing_1.PaymentSigner(cfg.walletPrivateKey);
|
|
39
|
-
return new Client(cfg, rpc, params, gateway, guaranteeDomain, signer);
|
|
40
|
-
}
|
|
41
|
-
static buildGateway(cfg, params) {
|
|
42
|
-
const ethRpcUrl = cfg.ethereumHttpRpcUrl ?? params.ethereumHttpRpcUrl;
|
|
43
|
-
const contractAddress = cfg.contractAddress ?? params.contractAddress;
|
|
44
|
-
return new contract_1.ContractGateway(ethRpcUrl, cfg.walletPrivateKey, contractAddress, params.chainId);
|
|
45
|
-
}
|
|
46
|
-
static async validateChainId(gateway, expectedChainId) {
|
|
47
|
-
try {
|
|
48
|
-
const chainId = await gateway.getChainId();
|
|
49
|
-
if (Number(chainId) !== Number(expectedChainId)) {
|
|
50
|
-
throw new errors_1.ClientInitializationError(`chain id mismatch between core (${expectedChainId}) and provider (${chainId})`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
catch (err) {
|
|
54
|
-
throw new errors_1.ClientInitializationError(err?.message ?? String(err));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
async aclose() {
|
|
58
|
-
await this.rpc.aclose();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
exports.Client = Client;
|
|
62
|
-
class UserClient {
|
|
63
|
-
constructor(client) {
|
|
64
|
-
this.client = client;
|
|
65
|
-
}
|
|
66
|
-
get guaranteeDomain() {
|
|
67
|
-
return this.client.guaranteeDomain;
|
|
68
|
-
}
|
|
69
|
-
async approveErc20(token, amount) {
|
|
70
|
-
return this.client.gateway.approveErc20(token, amount);
|
|
71
|
-
}
|
|
72
|
-
async deposit(amount, erc20Token) {
|
|
73
|
-
return this.client.gateway.deposit(amount, erc20Token);
|
|
74
|
-
}
|
|
75
|
-
async getUser() {
|
|
76
|
-
const assets = await this.client.gateway.getUserAssets();
|
|
77
|
-
return assets.map((a) => ({
|
|
78
|
-
asset: a.asset,
|
|
79
|
-
collateral: (0, utils_1.parseU256)(a.collateral),
|
|
80
|
-
withdrawalRequestAmount: (0, utils_1.parseU256)(a.withdrawal_request_amount),
|
|
81
|
-
withdrawalRequestTimestamp: Number(a.withdrawal_request_timestamp),
|
|
82
|
-
}));
|
|
83
|
-
}
|
|
84
|
-
async getTabPaymentStatus(tabId) {
|
|
85
|
-
const status = await this.client.gateway.getPaymentStatus(tabId);
|
|
86
|
-
return tabStatusFromRpc(status);
|
|
87
|
-
}
|
|
88
|
-
async signPayment(claims, scheme = models_1.SigningScheme.EIP712) {
|
|
89
|
-
return this.client.signer.signRequest(this.client.params, claims, scheme);
|
|
90
|
-
}
|
|
91
|
-
async payTab(tabId, reqId, amount, recipientAddress, erc20Token) {
|
|
92
|
-
if (erc20Token) {
|
|
93
|
-
return this.client.gateway.payTabErc20(tabId, amount, erc20Token, recipientAddress);
|
|
94
|
-
}
|
|
95
|
-
return this.client.gateway.payTabEth(tabId, reqId, amount, recipientAddress);
|
|
96
|
-
}
|
|
97
|
-
async requestWithdrawal(amount, erc20Token) {
|
|
98
|
-
return this.client.gateway.requestWithdrawal(amount, erc20Token);
|
|
99
|
-
}
|
|
100
|
-
async cancelWithdrawal(erc20Token) {
|
|
101
|
-
return this.client.gateway.cancelWithdrawal(erc20Token);
|
|
102
|
-
}
|
|
103
|
-
async finalizeWithdrawal(erc20Token) {
|
|
104
|
-
return this.client.gateway.finalizeWithdrawal(erc20Token);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
exports.UserClient = UserClient;
|
|
108
|
-
class RecipientClient {
|
|
109
|
-
constructor(client) {
|
|
110
|
-
this.client = client;
|
|
111
|
-
}
|
|
112
|
-
get recipientAddress() {
|
|
113
|
-
return (0, utils_1.normalizeAddress)(this.client.gateway.wallet.address);
|
|
114
|
-
}
|
|
115
|
-
get guaranteeDomain() {
|
|
116
|
-
return this.client.guaranteeDomain;
|
|
117
|
-
}
|
|
118
|
-
checkSigner(expected) {
|
|
119
|
-
if ((0, utils_1.normalizeAddress)(expected) !== this.recipientAddress) {
|
|
120
|
-
throw new errors_1.VerificationError('signer address does not match recipient address');
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
async createTab(userAddress, recipientAddress, erc20Token, ttl) {
|
|
124
|
-
this.checkSigner(recipientAddress);
|
|
125
|
-
const body = {
|
|
126
|
-
user_address: (0, utils_1.normalizeAddress)(userAddress),
|
|
127
|
-
recipient_address: (0, utils_1.normalizeAddress)(recipientAddress),
|
|
128
|
-
erc20_token: erc20Token ? (0, utils_1.normalizeAddress)(erc20Token) : null,
|
|
129
|
-
ttl: ttl ?? null,
|
|
130
|
-
};
|
|
131
|
-
const result = await this.client.rpc.createPaymentTab(body);
|
|
132
|
-
return (0, utils_1.parseU256)(result.id ?? result.tabId ?? result.tab_id);
|
|
133
|
-
}
|
|
134
|
-
async getTabPaymentStatus(tabId) {
|
|
135
|
-
const status = await this.client.gateway.getPaymentStatus(tabId);
|
|
136
|
-
return tabStatusFromRpc(status);
|
|
137
|
-
}
|
|
138
|
-
async issuePaymentGuarantee(claims, signature, scheme) {
|
|
139
|
-
this.checkSigner(claims.recipientAddress);
|
|
140
|
-
const payload = {
|
|
141
|
-
claims: {
|
|
142
|
-
version: 'v1',
|
|
143
|
-
user_address: claims.userAddress,
|
|
144
|
-
recipient_address: claims.recipientAddress,
|
|
145
|
-
tab_id: (0, utils_1.serializeU256)(claims.tabId),
|
|
146
|
-
amount: (0, utils_1.serializeU256)(claims.amount),
|
|
147
|
-
asset_address: claims.assetAddress,
|
|
148
|
-
timestamp: Number(claims.timestamp),
|
|
149
|
-
},
|
|
150
|
-
signature,
|
|
151
|
-
scheme,
|
|
152
|
-
};
|
|
153
|
-
const cert = await this.client.rpc.issueGuarantee(payload);
|
|
154
|
-
return { claims: cert.claims, signature: cert.signature };
|
|
155
|
-
}
|
|
156
|
-
verifyPaymentGuarantee(cert) {
|
|
157
|
-
const claims = (0, guarantee_1.decodeGuaranteeClaims)(cert.claims);
|
|
158
|
-
const domainHex = this.guaranteeDomain.startsWith('0x')
|
|
159
|
-
? this.guaranteeDomain.slice(2)
|
|
160
|
-
: Buffer.from(this.guaranteeDomain).toString('hex');
|
|
161
|
-
const claimsHex = Buffer.from(claims.domain).toString('hex');
|
|
162
|
-
if (claimsHex !== domainHex) {
|
|
163
|
-
throw new errors_1.VerificationError('guarantee domain mismatch');
|
|
164
|
-
}
|
|
165
|
-
return claims;
|
|
166
|
-
}
|
|
167
|
-
async remunerate(cert) {
|
|
168
|
-
this.verifyPaymentGuarantee(cert);
|
|
169
|
-
const sigWords = (0, bls_1.signatureToWords)(cert.signature);
|
|
170
|
-
const claimsBytes = Buffer.from(cert.claims.replace(/^0x/, ''), 'hex');
|
|
171
|
-
return this.client.gateway.remunerate(claimsBytes, sigWords);
|
|
172
|
-
}
|
|
173
|
-
async listSettledTabs() {
|
|
174
|
-
const tabs = await this.client.rpc.listSettledTabs(this.recipientAddress);
|
|
175
|
-
return tabs.map((t) => models_1.TabInfo.fromRpc(t));
|
|
176
|
-
}
|
|
177
|
-
async listPendingRemunerations() {
|
|
178
|
-
const items = await this.client.rpc.listPendingRemunerations(this.recipientAddress);
|
|
179
|
-
return items.map((item) => models_1.PendingRemunerationInfo.fromRpc(item));
|
|
180
|
-
}
|
|
181
|
-
async getTab(tabId) {
|
|
182
|
-
const result = await this.client.rpc.getTab(tabId);
|
|
183
|
-
return result ? models_1.TabInfo.fromRpc(result) : null;
|
|
184
|
-
}
|
|
185
|
-
async listRecipientTabs(settlementStatuses) {
|
|
186
|
-
const tabs = await this.client.rpc.listRecipientTabs(this.recipientAddress, settlementStatuses);
|
|
187
|
-
return tabs.map((t) => models_1.TabInfo.fromRpc(t));
|
|
188
|
-
}
|
|
189
|
-
async getTabGuarantees(tabId) {
|
|
190
|
-
const guarantees = await this.client.rpc.getTabGuarantees(tabId);
|
|
191
|
-
return guarantees.map((g) => models_1.GuaranteeInfo.fromRpc(g));
|
|
192
|
-
}
|
|
193
|
-
async getLatestGuarantee(tabId) {
|
|
194
|
-
const result = await this.client.rpc.getLatestGuarantee(tabId);
|
|
195
|
-
return result ? models_1.GuaranteeInfo.fromRpc(result) : null;
|
|
196
|
-
}
|
|
197
|
-
async getGuarantee(tabId, reqId) {
|
|
198
|
-
const result = await this.client.rpc.getGuarantee(tabId, reqId);
|
|
199
|
-
return result ? models_1.GuaranteeInfo.fromRpc(result) : null;
|
|
200
|
-
}
|
|
201
|
-
async listRecipientPayments() {
|
|
202
|
-
const payments = await this.client.rpc.listRecipientPayments(this.recipientAddress);
|
|
203
|
-
return payments.map((p) => models_1.RecipientPaymentInfo.fromRpc(p));
|
|
204
|
-
}
|
|
205
|
-
async getCollateralEventsForTab(tabId) {
|
|
206
|
-
const events = await this.client.rpc.getCollateralEventsForTab(tabId);
|
|
207
|
-
return events.map((ev) => models_1.CollateralEventInfo.fromRpc(ev));
|
|
208
|
-
}
|
|
209
|
-
async getUserAssetBalance(userAddress, assetAddress) {
|
|
210
|
-
const balance = await this.client.rpc.getUserAssetBalance(userAddress, assetAddress);
|
|
211
|
-
return balance ? models_1.AssetBalanceInfo.fromRpc(balance) : null;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
exports.RecipientClient = RecipientClient;
|
package/dist/src/config.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export interface Config {
|
|
2
|
-
rpcUrl: string;
|
|
3
|
-
walletPrivateKey: string;
|
|
4
|
-
ethereumHttpRpcUrl?: string;
|
|
5
|
-
contractAddress?: string;
|
|
6
|
-
adminApiKey?: string;
|
|
7
|
-
}
|
|
8
|
-
export declare class ConfigBuilder {
|
|
9
|
-
private _rpcUrl;
|
|
10
|
-
private _walletPrivateKey;
|
|
11
|
-
private _ethereumHttpRpcUrl?;
|
|
12
|
-
private _contractAddress?;
|
|
13
|
-
private _adminApiKey?;
|
|
14
|
-
rpcUrl(value: string): ConfigBuilder;
|
|
15
|
-
walletPrivateKey(value: string): ConfigBuilder;
|
|
16
|
-
ethereumHttpRpcUrl(value: string): ConfigBuilder;
|
|
17
|
-
contractAddress(value: string): ConfigBuilder;
|
|
18
|
-
adminApiKey(value: string): ConfigBuilder;
|
|
19
|
-
fromEnv(): ConfigBuilder;
|
|
20
|
-
build(): Config;
|
|
21
|
-
}
|
package/dist/src/config.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConfigBuilder = void 0;
|
|
4
|
-
const errors_1 = require("./errors");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
class ConfigBuilder {
|
|
7
|
-
constructor() {
|
|
8
|
-
this._rpcUrl = 'https://api.4mica.xyz/';
|
|
9
|
-
}
|
|
10
|
-
rpcUrl(value) {
|
|
11
|
-
this._rpcUrl = value;
|
|
12
|
-
return this;
|
|
13
|
-
}
|
|
14
|
-
walletPrivateKey(value) {
|
|
15
|
-
this._walletPrivateKey = value;
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
ethereumHttpRpcUrl(value) {
|
|
19
|
-
this._ethereumHttpRpcUrl = value;
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
contractAddress(value) {
|
|
23
|
-
this._contractAddress = value;
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
26
|
-
adminApiKey(value) {
|
|
27
|
-
this._adminApiKey = value;
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
fromEnv() {
|
|
31
|
-
const env = process.env;
|
|
32
|
-
if (env['4MICA_RPC_URL'])
|
|
33
|
-
this._rpcUrl = env['4MICA_RPC_URL'];
|
|
34
|
-
if (env['4MICA_WALLET_PRIVATE_KEY'])
|
|
35
|
-
this._walletPrivateKey = env['4MICA_WALLET_PRIVATE_KEY'];
|
|
36
|
-
if (env['4MICA_ETHEREUM_HTTP_RPC_URL'])
|
|
37
|
-
this._ethereumHttpRpcUrl = env['4MICA_ETHEREUM_HTTP_RPC_URL'];
|
|
38
|
-
if (env['4MICA_CONTRACT_ADDRESS'])
|
|
39
|
-
this._contractAddress = env['4MICA_CONTRACT_ADDRESS'];
|
|
40
|
-
if (env['4MICA_ADMIN_API_KEY'])
|
|
41
|
-
this._adminApiKey = env['4MICA_ADMIN_API_KEY'];
|
|
42
|
-
return this;
|
|
43
|
-
}
|
|
44
|
-
build() {
|
|
45
|
-
if (!this._walletPrivateKey) {
|
|
46
|
-
throw new errors_1.ConfigError('missing wallet_private_key');
|
|
47
|
-
}
|
|
48
|
-
if (!this._rpcUrl) {
|
|
49
|
-
throw new errors_1.ConfigError('missing rpc_url');
|
|
50
|
-
}
|
|
51
|
-
try {
|
|
52
|
-
const rpcUrl = (0, utils_1.validateUrl)(this._rpcUrl);
|
|
53
|
-
const walletPrivateKey = (0, utils_1.normalizePrivateKey)(this._walletPrivateKey);
|
|
54
|
-
const ethereumHttpRpcUrl = this._ethereumHttpRpcUrl
|
|
55
|
-
? (0, utils_1.validateUrl)(this._ethereumHttpRpcUrl)
|
|
56
|
-
: undefined;
|
|
57
|
-
const contractAddress = this._contractAddress
|
|
58
|
-
? (0, utils_1.normalizeAddress)(this._contractAddress)
|
|
59
|
-
: undefined;
|
|
60
|
-
return {
|
|
61
|
-
rpcUrl,
|
|
62
|
-
walletPrivateKey,
|
|
63
|
-
ethereumHttpRpcUrl,
|
|
64
|
-
contractAddress,
|
|
65
|
-
adminApiKey: this._adminApiKey,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
if (err instanceof utils_1.ValidationError) {
|
|
70
|
-
throw new errors_1.ConfigError(err.message);
|
|
71
|
-
}
|
|
72
|
-
throw err;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
exports.ConfigBuilder = ConfigBuilder;
|
package/dist/src/contract.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Contract, JsonRpcProvider, Wallet } from 'ethers';
|
|
2
|
-
type ContractFactory = (address: string, abi: any, signer: Wallet) => Contract;
|
|
3
|
-
export declare class ContractGateway {
|
|
4
|
-
readonly provider: JsonRpcProvider;
|
|
5
|
-
readonly wallet: Wallet;
|
|
6
|
-
readonly contract: Contract;
|
|
7
|
-
private erc20Cache;
|
|
8
|
-
constructor(ethRpcUrl: string, privateKey: string, contractAddress: string, chainId: number | bigint, provider?: JsonRpcProvider, contractFactory?: ContractFactory);
|
|
9
|
-
getChainId(): Promise<number>;
|
|
10
|
-
private erc20;
|
|
11
|
-
private send;
|
|
12
|
-
getGuaranteeDomain(): Promise<string>;
|
|
13
|
-
approveErc20(token: string, amount: number | bigint | string): Promise<any>;
|
|
14
|
-
deposit(amount: number | bigint | string, erc20Token?: string): Promise<any>;
|
|
15
|
-
getUserAssets(): Promise<{
|
|
16
|
-
asset: string;
|
|
17
|
-
collateral: bigint;
|
|
18
|
-
withdrawal_request_timestamp: bigint;
|
|
19
|
-
withdrawal_request_amount: bigint;
|
|
20
|
-
}[]>;
|
|
21
|
-
getPaymentStatus(tabId: number | bigint): Promise<{
|
|
22
|
-
paid: bigint;
|
|
23
|
-
remunerated: boolean;
|
|
24
|
-
asset: string;
|
|
25
|
-
}>;
|
|
26
|
-
payTabEth(tabId: number | bigint, reqId: number | bigint, amount: number | bigint | string, recipient: string): Promise<any>;
|
|
27
|
-
payTabErc20(tabId: number | bigint, amount: number | bigint | string, erc20Token: string, recipient: string): Promise<any>;
|
|
28
|
-
requestWithdrawal(amount: number | bigint | string, erc20Token?: string): Promise<any>;
|
|
29
|
-
cancelWithdrawal(erc20Token?: string): Promise<any>;
|
|
30
|
-
finalizeWithdrawal(erc20Token?: string): Promise<any>;
|
|
31
|
-
remunerate(claimsBlob: Uint8Array, signatureWords: Uint8Array[]): Promise<any>;
|
|
32
|
-
}
|
|
33
|
-
export {};
|