@4mica/sdk 0.5.2 → 0.5.3
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 +26 -15
- package/dist/abi/core4mica.d.ts +1244 -0
- package/dist/abi/core4mica.js +1606 -0
- package/dist/abi/erc20.d.ts +141 -0
- package/dist/abi/erc20.js +188 -0
- package/dist/auth.d.ts +2 -2
- package/dist/auth.js +11 -0
- package/dist/bls.js +3 -3
- package/dist/client.d.ts +7 -9
- package/dist/client.js +16 -26
- package/dist/config.d.ts +3 -4
- package/dist/config.js +12 -7
- package/dist/contract.d.ts +25 -20
- package/dist/contract.js +133 -89
- package/dist/errors.js +4 -0
- package/dist/guarantee.js +19 -20
- package/dist/models.js +63 -0
- package/dist/rpc.js +5 -0
- package/dist/signing.d.ts +6 -23
- package/dist/signing.js +25 -20
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +9 -5
- package/dist/x402/index.js +2 -0
- package/package.json +14 -14
package/dist/contract.js
CHANGED
|
@@ -1,134 +1,178 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.ContractGateway = void 0;
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const errors_1 = require("./errors");
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const chains_1 = require("viem/chains");
|
|
6
|
+
const core4mica_1 = require("./abi/core4mica");
|
|
11
7
|
const utils_1 = require("./utils");
|
|
8
|
+
const CHAINS = {
|
|
9
|
+
1: chains_1.mainnet,
|
|
10
|
+
11155111: chains_1.sepolia,
|
|
11
|
+
8453: chains_1.base,
|
|
12
|
+
84532: chains_1.baseSepolia,
|
|
13
|
+
137: chains_1.polygon,
|
|
14
|
+
80002: chains_1.polygonAmoy,
|
|
15
|
+
};
|
|
16
|
+
function getChain(chainId, rpcUrl) {
|
|
17
|
+
const chain = CHAINS[chainId];
|
|
18
|
+
if (chain)
|
|
19
|
+
return chain;
|
|
20
|
+
return {
|
|
21
|
+
id: chainId,
|
|
22
|
+
name: `local-${chainId}`,
|
|
23
|
+
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
|
|
24
|
+
rpcUrls: {
|
|
25
|
+
default: { http: [rpcUrl] },
|
|
26
|
+
public: { http: [rpcUrl] },
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
12
30
|
class ContractGateway {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
this.
|
|
21
|
-
this.wallet = new ethers_1.Wallet(privateKey, this.provider);
|
|
22
|
-
const factory = contractFactory ??
|
|
23
|
-
((addr, abi, signer) => {
|
|
24
|
-
const resolvedAbi = abi.abi ?? abi;
|
|
25
|
-
return new ethers_1.Contract(addr, resolvedAbi, signer);
|
|
26
|
-
});
|
|
27
|
-
this.contract = factory(contractAddress, core4mica_json_1.default, this.wallet);
|
|
28
|
-
}
|
|
29
|
-
async getChainId() {
|
|
30
|
-
const network = await this.provider.getNetwork();
|
|
31
|
-
return Number(network.chainId);
|
|
31
|
+
publicClient;
|
|
32
|
+
walletClient;
|
|
33
|
+
contract;
|
|
34
|
+
erc20Cache = new Map();
|
|
35
|
+
constructor(publicClient, walletClient, contract) {
|
|
36
|
+
this.publicClient = publicClient;
|
|
37
|
+
this.walletClient = walletClient;
|
|
38
|
+
this.contract = contract;
|
|
32
39
|
}
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
static async create(rpcUrl, signer, contractAddress, chainId) {
|
|
41
|
+
const chain = getChain(chainId, rpcUrl);
|
|
42
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
43
|
+
transport: (0, viem_1.http)(rpcUrl),
|
|
44
|
+
});
|
|
45
|
+
const rpcChainId = await publicClient.getChainId();
|
|
46
|
+
if (rpcChainId !== Number(chainId)) {
|
|
47
|
+
throw new Error(`Connected to chain ${rpcChainId}, expected ${chainId}`);
|
|
37
48
|
}
|
|
38
|
-
|
|
49
|
+
const walletClient = (0, viem_1.createWalletClient)({
|
|
50
|
+
transport: (0, viem_1.http)(rpcUrl),
|
|
51
|
+
account: signer,
|
|
52
|
+
chain,
|
|
53
|
+
});
|
|
54
|
+
const contract = (0, viem_1.getContract)({
|
|
55
|
+
address: contractAddress,
|
|
56
|
+
abi: core4mica_1.core4micaAbi,
|
|
57
|
+
client: {
|
|
58
|
+
public: publicClient,
|
|
59
|
+
wallet: walletClient,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
return new ContractGateway(publicClient, walletClient, contract);
|
|
39
63
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
catch (err) {
|
|
49
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
50
|
-
throw new errors_1.ContractError(message);
|
|
64
|
+
erc20(token) {
|
|
65
|
+
if (!this.erc20Cache.has(token)) {
|
|
66
|
+
this.erc20Cache.set(token, (0, viem_1.getContract)({
|
|
67
|
+
address: token,
|
|
68
|
+
abi: viem_1.erc20Abi,
|
|
69
|
+
client: { public: this.publicClient, wallet: this.walletClient },
|
|
70
|
+
}));
|
|
51
71
|
}
|
|
72
|
+
return this.erc20Cache.get(token);
|
|
52
73
|
}
|
|
53
74
|
async getGuaranteeDomain() {
|
|
54
|
-
return this.
|
|
75
|
+
return this.contract.read.guaranteeDomainSeparator();
|
|
55
76
|
}
|
|
56
77
|
async approveErc20(token, amount) {
|
|
57
78
|
const erc20 = this.erc20(token);
|
|
58
|
-
|
|
59
|
-
const spender =
|
|
60
|
-
|
|
79
|
+
// spender address logic
|
|
80
|
+
const spender = this.contract.address;
|
|
81
|
+
const hash = await erc20.write.approve([spender, (0, utils_1.parseU256)(amount)]);
|
|
82
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
61
83
|
}
|
|
62
84
|
async deposit(amount, erc20Token) {
|
|
85
|
+
let hash;
|
|
63
86
|
if (erc20Token) {
|
|
64
|
-
|
|
87
|
+
hash = await this.contract.write.depositStablecoin([erc20Token, (0, utils_1.parseU256)(amount)]);
|
|
65
88
|
}
|
|
66
|
-
|
|
67
|
-
value: (0, utils_1.parseU256)(amount)
|
|
68
|
-
}
|
|
89
|
+
else {
|
|
90
|
+
hash = await this.contract.write.deposit({ value: (0, utils_1.parseU256)(amount) });
|
|
91
|
+
}
|
|
92
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
69
93
|
}
|
|
70
94
|
async getUserAssets() {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
catch (err) {
|
|
81
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
82
|
-
throw new errors_1.ContractError(message);
|
|
83
|
-
}
|
|
95
|
+
const addr = this.walletClient.account.address;
|
|
96
|
+
const result = await this.contract.read.getUserAllAssets([addr]);
|
|
97
|
+
return result.map((a) => ({
|
|
98
|
+
asset: a.asset,
|
|
99
|
+
collateral: a.collateral,
|
|
100
|
+
withdrawalRequestTimestamp: a.withdrawalRequestTimestamp,
|
|
101
|
+
withdrawalRequestAmount: a.withdrawalRequestAmount,
|
|
102
|
+
}));
|
|
84
103
|
}
|
|
85
104
|
async getPaymentStatus(tabId) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
catch (err) {
|
|
95
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
96
|
-
throw new errors_1.ContractError(message);
|
|
97
|
-
}
|
|
105
|
+
const [paid, remunerated, asset] = await this.contract.read.getPaymentStatus([
|
|
106
|
+
(0, utils_1.parseU256)(tabId),
|
|
107
|
+
]);
|
|
108
|
+
return {
|
|
109
|
+
paid,
|
|
110
|
+
remunerated,
|
|
111
|
+
asset,
|
|
112
|
+
};
|
|
98
113
|
}
|
|
99
114
|
async payTabEth(tabId, reqId, amount, recipient) {
|
|
100
|
-
const data =
|
|
101
|
-
const
|
|
115
|
+
const data = new TextEncoder().encode(`tab_id:${tabId.toString(16)};req_id:${reqId.toString(16)}`);
|
|
116
|
+
const hash = await this.walletClient.sendTransaction({
|
|
102
117
|
to: recipient,
|
|
103
118
|
value: (0, utils_1.parseU256)(amount),
|
|
104
|
-
data: (0,
|
|
105
|
-
};
|
|
106
|
-
return this.
|
|
119
|
+
data: (0, utils_1.hexFromBytes)(data),
|
|
120
|
+
});
|
|
121
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
107
122
|
}
|
|
108
123
|
async payTabErc20(tabId, amount, erc20Token, recipient) {
|
|
109
|
-
|
|
124
|
+
const hash = await this.contract.write.payTabInERC20Token([
|
|
125
|
+
(0, utils_1.parseU256)(tabId),
|
|
126
|
+
erc20Token,
|
|
127
|
+
(0, utils_1.parseU256)(amount),
|
|
128
|
+
recipient,
|
|
129
|
+
]);
|
|
130
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
110
131
|
}
|
|
111
132
|
async requestWithdrawal(amount, erc20Token) {
|
|
133
|
+
const value = (0, utils_1.parseU256)(amount);
|
|
134
|
+
let hash;
|
|
112
135
|
if (erc20Token) {
|
|
113
|
-
|
|
136
|
+
hash = await this.contract.write.requestWithdrawal([erc20Token, value]);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
hash = await this.contract.write.requestWithdrawal([value]);
|
|
114
140
|
}
|
|
115
|
-
return this.
|
|
141
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
116
142
|
}
|
|
117
143
|
async cancelWithdrawal(erc20Token) {
|
|
144
|
+
let hash;
|
|
118
145
|
if (erc20Token) {
|
|
119
|
-
|
|
146
|
+
hash = await this.contract.write.cancelWithdrawal([erc20Token]);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
hash = await this.contract.write.cancelWithdrawal();
|
|
120
150
|
}
|
|
121
|
-
return this.
|
|
151
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
122
152
|
}
|
|
123
153
|
async finalizeWithdrawal(erc20Token) {
|
|
154
|
+
let hash;
|
|
124
155
|
if (erc20Token) {
|
|
125
|
-
|
|
156
|
+
hash = await this.contract.write.finalizeWithdrawal([erc20Token]);
|
|
126
157
|
}
|
|
127
|
-
|
|
158
|
+
else {
|
|
159
|
+
hash = await this.contract.write.finalizeWithdrawal();
|
|
160
|
+
}
|
|
161
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
128
162
|
}
|
|
129
163
|
async remunerate(claimsBlob, signatureWords) {
|
|
130
|
-
const sigStruct =
|
|
131
|
-
|
|
164
|
+
const sigStruct = {
|
|
165
|
+
x_c0_a: (0, utils_1.hexFromBytes)(signatureWords[0]),
|
|
166
|
+
x_c0_b: (0, utils_1.hexFromBytes)(signatureWords[1]),
|
|
167
|
+
x_c1_a: (0, utils_1.hexFromBytes)(signatureWords[2]),
|
|
168
|
+
x_c1_b: (0, utils_1.hexFromBytes)(signatureWords[3]),
|
|
169
|
+
y_c0_a: (0, utils_1.hexFromBytes)(signatureWords[4]),
|
|
170
|
+
y_c0_b: (0, utils_1.hexFromBytes)(signatureWords[5]),
|
|
171
|
+
y_c1_a: (0, utils_1.hexFromBytes)(signatureWords[6]),
|
|
172
|
+
y_c1_b: (0, utils_1.hexFromBytes)(signatureWords[7]),
|
|
173
|
+
};
|
|
174
|
+
const hash = await this.contract.write.remunerate([(0, utils_1.hexFromBytes)(claimsBlob), sigStruct]);
|
|
175
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
132
176
|
}
|
|
133
177
|
}
|
|
134
178
|
exports.ContractGateway = ContractGateway;
|
package/dist/errors.js
CHANGED
|
@@ -12,6 +12,8 @@ class ConfigError extends FourMicaError {
|
|
|
12
12
|
}
|
|
13
13
|
exports.ConfigError = ConfigError;
|
|
14
14
|
class RpcError extends FourMicaError {
|
|
15
|
+
status;
|
|
16
|
+
body;
|
|
15
17
|
constructor(message, options) {
|
|
16
18
|
super(message);
|
|
17
19
|
this.status = options?.status;
|
|
@@ -47,6 +49,8 @@ class AuthDecodeError extends AuthError {
|
|
|
47
49
|
}
|
|
48
50
|
exports.AuthDecodeError = AuthDecodeError;
|
|
49
51
|
class AuthApiError extends AuthError {
|
|
52
|
+
status;
|
|
53
|
+
body;
|
|
50
54
|
constructor(message, options) {
|
|
51
55
|
super(message);
|
|
52
56
|
this.status = options?.status;
|
package/dist/guarantee.js
CHANGED
|
@@ -2,24 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.encodeGuaranteeClaims = encodeGuaranteeClaims;
|
|
4
4
|
exports.decodeGuaranteeClaims = decodeGuaranteeClaims;
|
|
5
|
-
const
|
|
5
|
+
const viem_1 = require("viem");
|
|
6
6
|
const errors_1 = require("./errors");
|
|
7
7
|
const utils_1 = require("./utils");
|
|
8
8
|
const CLAIM_TYPES = [
|
|
9
|
-
'bytes32',
|
|
10
|
-
'uint256',
|
|
11
|
-
'uint256',
|
|
12
|
-
'address',
|
|
13
|
-
'address',
|
|
14
|
-
'uint256',
|
|
15
|
-
'uint256',
|
|
16
|
-
'address',
|
|
17
|
-
'uint64',
|
|
18
|
-
'uint64',
|
|
9
|
+
{ type: 'bytes32' },
|
|
10
|
+
{ type: 'uint256' },
|
|
11
|
+
{ type: 'uint256' },
|
|
12
|
+
{ type: 'address' },
|
|
13
|
+
{ type: 'address' },
|
|
14
|
+
{ type: 'uint256' },
|
|
15
|
+
{ type: 'uint256' },
|
|
16
|
+
{ type: 'address' },
|
|
17
|
+
{ type: 'uint64' },
|
|
18
|
+
{ type: 'uint64' },
|
|
19
19
|
];
|
|
20
|
-
const coder = ethers_1.AbiCoder.defaultAbiCoder();
|
|
21
20
|
function ensureDomainBytes(domain) {
|
|
22
|
-
const bytes = typeof domain === 'string' ? (0,
|
|
21
|
+
const bytes = typeof domain === 'string' ? (0, viem_1.toBytes)(domain) : domain;
|
|
23
22
|
if (bytes.length !== 32) {
|
|
24
23
|
throw new errors_1.VerificationError('domain separator must be 32 bytes');
|
|
25
24
|
}
|
|
@@ -30,8 +29,8 @@ function encodeGuaranteeClaims(claims) {
|
|
|
30
29
|
throw new errors_1.VerificationError(`unsupported guarantee claims version: ${claims.version}`);
|
|
31
30
|
}
|
|
32
31
|
const domain = ensureDomainBytes(claims.domain);
|
|
33
|
-
const encoded =
|
|
34
|
-
domain,
|
|
32
|
+
const encoded = (0, viem_1.encodeAbiParameters)(CLAIM_TYPES, [
|
|
33
|
+
(0, utils_1.hexFromBytes)(domain),
|
|
35
34
|
(0, utils_1.parseU256)(claims.tabId),
|
|
36
35
|
(0, utils_1.parseU256)(claims.reqId),
|
|
37
36
|
claims.userAddress,
|
|
@@ -42,17 +41,17 @@ function encodeGuaranteeClaims(claims) {
|
|
|
42
41
|
BigInt(claims.timestamp),
|
|
43
42
|
BigInt(claims.version),
|
|
44
43
|
]);
|
|
45
|
-
return
|
|
44
|
+
return (0, viem_1.encodeAbiParameters)([{ type: 'uint64' }, { type: 'bytes' }], [BigInt(claims.version), encoded]);
|
|
46
45
|
}
|
|
47
46
|
function decodeGuaranteeClaims(data) {
|
|
48
|
-
const rawBytes = typeof data === 'string' ? (0,
|
|
49
|
-
const [version, encoded] =
|
|
47
|
+
const rawBytes = typeof data === 'string' ? (0, viem_1.toBytes)(data) : data;
|
|
48
|
+
const [version, encoded] = (0, viem_1.decodeAbiParameters)([{ type: 'uint64' }, { type: 'bytes' }], rawBytes);
|
|
50
49
|
if (version !== 1n) {
|
|
51
50
|
throw new errors_1.VerificationError(`unsupported guarantee claims version: ${version}`);
|
|
52
51
|
}
|
|
53
|
-
const [domain, tabId, reqId, user, recipient, amount, totalAmount, asset, timestamp, claimsVersion,] =
|
|
52
|
+
const [domain, tabId, reqId, user, recipient, amount, totalAmount, asset, timestamp, claimsVersion,] = (0, viem_1.decodeAbiParameters)(CLAIM_TYPES, encoded);
|
|
54
53
|
return {
|
|
55
|
-
domain: (0,
|
|
54
|
+
domain: (0, viem_1.toBytes)(domain),
|
|
56
55
|
userAddress: user,
|
|
57
56
|
recipientAddress: recipient,
|
|
58
57
|
tabId: (0, utils_1.parseU256)(tabId),
|
package/dist/models.js
CHANGED
|
@@ -12,6 +12,13 @@ exports.ADMIN_API_KEY_PREFIX = 'ak_';
|
|
|
12
12
|
exports.ADMIN_SCOPE_SUSPEND_USERS = 'user_suspension:write';
|
|
13
13
|
exports.ADMIN_SCOPE_MANAGE_KEYS = 'admin_api_keys:manage';
|
|
14
14
|
class PaymentGuaranteeRequestClaims {
|
|
15
|
+
userAddress;
|
|
16
|
+
recipientAddress;
|
|
17
|
+
tabId;
|
|
18
|
+
reqId;
|
|
19
|
+
amount;
|
|
20
|
+
timestamp;
|
|
21
|
+
assetAddress;
|
|
15
22
|
constructor(init) {
|
|
16
23
|
this.userAddress = init.userAddress;
|
|
17
24
|
this.recipientAddress = init.recipientAddress;
|
|
@@ -36,6 +43,9 @@ class PaymentGuaranteeRequestClaims {
|
|
|
36
43
|
}
|
|
37
44
|
exports.PaymentGuaranteeRequestClaims = PaymentGuaranteeRequestClaims;
|
|
38
45
|
class UserSuspensionStatus {
|
|
46
|
+
userAddress;
|
|
47
|
+
suspended;
|
|
48
|
+
updatedAt;
|
|
39
49
|
constructor(userAddress, suspended, updatedAt) {
|
|
40
50
|
this.userAddress = userAddress;
|
|
41
51
|
this.suspended = suspended;
|
|
@@ -47,6 +57,11 @@ class UserSuspensionStatus {
|
|
|
47
57
|
}
|
|
48
58
|
exports.UserSuspensionStatus = UserSuspensionStatus;
|
|
49
59
|
class AdminApiKeyInfo {
|
|
60
|
+
id;
|
|
61
|
+
name;
|
|
62
|
+
scopes;
|
|
63
|
+
createdAt;
|
|
64
|
+
revokedAt;
|
|
50
65
|
constructor(id, name, scopes, createdAt, revokedAt) {
|
|
51
66
|
this.id = id;
|
|
52
67
|
this.name = name;
|
|
@@ -61,6 +76,11 @@ class AdminApiKeyInfo {
|
|
|
61
76
|
}
|
|
62
77
|
exports.AdminApiKeyInfo = AdminApiKeyInfo;
|
|
63
78
|
class AdminApiKeySecret {
|
|
79
|
+
id;
|
|
80
|
+
name;
|
|
81
|
+
scopes;
|
|
82
|
+
createdAt;
|
|
83
|
+
apiKey;
|
|
64
84
|
constructor(id, name, scopes, createdAt, apiKey) {
|
|
65
85
|
this.id = id;
|
|
66
86
|
this.name = name;
|
|
@@ -81,6 +101,16 @@ function getAny(raw, ...keys) {
|
|
|
81
101
|
return undefined;
|
|
82
102
|
}
|
|
83
103
|
class TabInfo {
|
|
104
|
+
tabId;
|
|
105
|
+
userAddress;
|
|
106
|
+
recipientAddress;
|
|
107
|
+
assetAddress;
|
|
108
|
+
startTimestamp;
|
|
109
|
+
ttlSeconds;
|
|
110
|
+
status;
|
|
111
|
+
settlementStatus;
|
|
112
|
+
createdAt;
|
|
113
|
+
updatedAt;
|
|
84
114
|
constructor(tabId, userAddress, recipientAddress, assetAddress, startTimestamp, ttlSeconds, status, settlementStatus, createdAt, updatedAt) {
|
|
85
115
|
this.tabId = tabId;
|
|
86
116
|
this.userAddress = userAddress;
|
|
@@ -99,6 +129,14 @@ class TabInfo {
|
|
|
99
129
|
}
|
|
100
130
|
exports.TabInfo = TabInfo;
|
|
101
131
|
class GuaranteeInfo {
|
|
132
|
+
tabId;
|
|
133
|
+
reqId;
|
|
134
|
+
fromAddress;
|
|
135
|
+
toAddress;
|
|
136
|
+
assetAddress;
|
|
137
|
+
amount;
|
|
138
|
+
timestamp;
|
|
139
|
+
certificate;
|
|
102
140
|
constructor(tabId, reqId, fromAddress, toAddress, assetAddress, amount, timestamp, certificate) {
|
|
103
141
|
this.tabId = tabId;
|
|
104
142
|
this.reqId = reqId;
|
|
@@ -115,6 +153,8 @@ class GuaranteeInfo {
|
|
|
115
153
|
}
|
|
116
154
|
exports.GuaranteeInfo = GuaranteeInfo;
|
|
117
155
|
class PendingRemunerationInfo {
|
|
156
|
+
tab;
|
|
157
|
+
latestGuarantee;
|
|
118
158
|
constructor(tab, latestGuarantee) {
|
|
119
159
|
this.tab = tab;
|
|
120
160
|
this.latestGuarantee = latestGuarantee;
|
|
@@ -126,6 +166,15 @@ class PendingRemunerationInfo {
|
|
|
126
166
|
}
|
|
127
167
|
exports.PendingRemunerationInfo = PendingRemunerationInfo;
|
|
128
168
|
class CollateralEventInfo {
|
|
169
|
+
id;
|
|
170
|
+
userAddress;
|
|
171
|
+
assetAddress;
|
|
172
|
+
amount;
|
|
173
|
+
eventType;
|
|
174
|
+
tabId;
|
|
175
|
+
reqId;
|
|
176
|
+
txId;
|
|
177
|
+
createdAt;
|
|
129
178
|
constructor(id, userAddress, assetAddress, amount, eventType, tabId, reqId, txId, createdAt = 0) {
|
|
130
179
|
this.id = id;
|
|
131
180
|
this.userAddress = userAddress;
|
|
@@ -145,6 +194,12 @@ class CollateralEventInfo {
|
|
|
145
194
|
}
|
|
146
195
|
exports.CollateralEventInfo = CollateralEventInfo;
|
|
147
196
|
class AssetBalanceInfo {
|
|
197
|
+
userAddress;
|
|
198
|
+
assetAddress;
|
|
199
|
+
total;
|
|
200
|
+
locked;
|
|
201
|
+
version;
|
|
202
|
+
updatedAt;
|
|
148
203
|
constructor(userAddress, assetAddress, total, locked, version, updatedAt) {
|
|
149
204
|
this.userAddress = userAddress;
|
|
150
205
|
this.assetAddress = assetAddress;
|
|
@@ -159,6 +214,14 @@ class AssetBalanceInfo {
|
|
|
159
214
|
}
|
|
160
215
|
exports.AssetBalanceInfo = AssetBalanceInfo;
|
|
161
216
|
class RecipientPaymentInfo {
|
|
217
|
+
userAddress;
|
|
218
|
+
recipientAddress;
|
|
219
|
+
txHash;
|
|
220
|
+
amount;
|
|
221
|
+
verified;
|
|
222
|
+
finalized;
|
|
223
|
+
failed;
|
|
224
|
+
createdAt;
|
|
162
225
|
constructor(userAddress, recipientAddress, txHash, amount, verified, finalized, failed, createdAt) {
|
|
163
226
|
this.userAddress = userAddress;
|
|
164
227
|
this.recipientAddress = recipientAddress;
|
package/dist/rpc.js
CHANGED
|
@@ -8,6 +8,11 @@ function serializeTabId(tabId) {
|
|
|
8
8
|
return `0x${BigInt(tabId).toString(16)}`;
|
|
9
9
|
}
|
|
10
10
|
class RpcProxy {
|
|
11
|
+
baseUrl;
|
|
12
|
+
adminApiKey;
|
|
13
|
+
bearerToken;
|
|
14
|
+
bearerTokenProvider;
|
|
15
|
+
fetchFn;
|
|
11
16
|
constructor(endpoint, adminApiKey, fetchFn = fetch) {
|
|
12
17
|
this.baseUrl = endpoint.endsWith('/') ? endpoint.slice(0, -1) : endpoint;
|
|
13
18
|
this.adminApiKey = adminApiKey;
|
package/dist/signing.d.ts
CHANGED
|
@@ -1,22 +1,5 @@
|
|
|
1
|
+
import { Account, type Hex } from 'viem';
|
|
1
2
|
import { PaymentGuaranteeRequestClaims, PaymentSignature, SigningScheme } from './models';
|
|
2
|
-
/**
|
|
3
|
-
* ClientEvmSigner - Used by x402 clients to sign payment authorizations
|
|
4
|
-
* This is typically a LocalAccount or wallet that holds private keys
|
|
5
|
-
* and can sign EIP-712 typed data for payment authorizations
|
|
6
|
-
*/
|
|
7
|
-
export type EvmSigner = {
|
|
8
|
-
readonly address: `0x${string}`;
|
|
9
|
-
signTypedData(message: {
|
|
10
|
-
domain: Record<string, unknown>;
|
|
11
|
-
types: Record<string, unknown>;
|
|
12
|
-
primaryType: string;
|
|
13
|
-
message: Record<string, unknown>;
|
|
14
|
-
}): Promise<`0x${string}`>;
|
|
15
|
-
signMessage(message: {
|
|
16
|
-
message: string;
|
|
17
|
-
}): Promise<`0x${string}`>;
|
|
18
|
-
};
|
|
19
|
-
export declare function createLocalSigner(privateKey: string): EvmSigner;
|
|
20
3
|
export declare class CorePublicParameters {
|
|
21
4
|
publicKey: Uint8Array;
|
|
22
5
|
contractAddress: string;
|
|
@@ -70,12 +53,12 @@ export type GuaranteeTypedData = {
|
|
|
70
53
|
chainId: number;
|
|
71
54
|
};
|
|
72
55
|
message: {
|
|
73
|
-
user:
|
|
74
|
-
recipient:
|
|
56
|
+
user: Hex;
|
|
57
|
+
recipient: Hex;
|
|
75
58
|
tabId: bigint;
|
|
76
59
|
reqId: bigint;
|
|
77
60
|
amount: bigint;
|
|
78
|
-
asset:
|
|
61
|
+
asset: Hex;
|
|
79
62
|
timestamp: bigint;
|
|
80
63
|
};
|
|
81
64
|
};
|
|
@@ -100,7 +83,7 @@ export declare function validateGuaranteeSigningContext(params: CorePublicParame
|
|
|
100
83
|
export declare function buildGuaranteeTypedData(params: CorePublicParameters, claims: PaymentGuaranteeRequestClaims): GuaranteeTypedData;
|
|
101
84
|
export declare function encodeGuaranteeEip191(claims: PaymentGuaranteeRequestClaims): string;
|
|
102
85
|
export declare class PaymentSigner {
|
|
103
|
-
|
|
104
|
-
constructor(signer:
|
|
86
|
+
readonly signer: Account;
|
|
87
|
+
constructor(signer: Account);
|
|
105
88
|
signRequest(params: CorePublicParameters, claims: PaymentGuaranteeRequestClaims, scheme?: SigningScheme): Promise<PaymentSignature>;
|
|
106
89
|
}
|
package/dist/signing.js
CHANGED
|
@@ -1,31 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PaymentSigner = exports.GUARANTEE_EIP712_TYPES = exports.CorePublicParameters = void 0;
|
|
4
|
-
exports.createLocalSigner = createLocalSigner;
|
|
5
4
|
exports.validateGuaranteeTypedData = validateGuaranteeTypedData;
|
|
6
5
|
exports.validateGuaranteeSigningContext = validateGuaranteeSigningContext;
|
|
7
6
|
exports.buildGuaranteeTypedData = buildGuaranteeTypedData;
|
|
8
7
|
exports.encodeGuaranteeEip191 = encodeGuaranteeEip191;
|
|
9
|
-
const
|
|
8
|
+
const viem_1 = require("viem");
|
|
10
9
|
const errors_1 = require("./errors");
|
|
11
10
|
const models_1 = require("./models");
|
|
12
11
|
const utils_1 = require("./utils");
|
|
13
|
-
function createLocalSigner(privateKey) {
|
|
14
|
-
privateKey = (0, utils_1.normalizePrivateKey)(privateKey);
|
|
15
|
-
const wallet = new ethers_1.Wallet(privateKey);
|
|
16
|
-
return {
|
|
17
|
-
address: wallet.address,
|
|
18
|
-
signTypedData: async (message) => {
|
|
19
|
-
const signature = await wallet.signTypedData(message.domain, { [message.primaryType]: message.types[message.primaryType] }, message.message);
|
|
20
|
-
return signature;
|
|
21
|
-
},
|
|
22
|
-
signMessage: async ({ message }) => {
|
|
23
|
-
const signature = await wallet.signMessage(message);
|
|
24
|
-
return signature;
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
12
|
class CorePublicParameters {
|
|
13
|
+
publicKey;
|
|
14
|
+
contractAddress;
|
|
15
|
+
ethereumHttpRpcUrl;
|
|
16
|
+
eip712Name;
|
|
17
|
+
eip712Version;
|
|
18
|
+
chainId;
|
|
29
19
|
constructor(publicKey, contractAddress, ethereumHttpRpcUrl, eip712Name, eip712Version, chainId) {
|
|
30
20
|
this.publicKey = publicKey;
|
|
31
21
|
this.contractAddress = contractAddress;
|
|
@@ -37,7 +27,7 @@ class CorePublicParameters {
|
|
|
37
27
|
static fromRpc(payload) {
|
|
38
28
|
const pkRaw = payload.public_key ?? payload.publicKey;
|
|
39
29
|
const pk = typeof pkRaw === 'string'
|
|
40
|
-
? (0,
|
|
30
|
+
? (0, viem_1.toBytes)(pkRaw)
|
|
41
31
|
: pkRaw instanceof Uint8Array
|
|
42
32
|
? pkRaw
|
|
43
33
|
: Array.isArray(pkRaw)
|
|
@@ -176,18 +166,27 @@ function buildGuaranteeTypedData(params, claims) {
|
|
|
176
166
|
};
|
|
177
167
|
}
|
|
178
168
|
function encodeGuaranteeEip191(claims) {
|
|
179
|
-
const payload =
|
|
169
|
+
const payload = (0, viem_1.encodeAbiParameters)([
|
|
170
|
+
{ type: 'address' },
|
|
171
|
+
{ type: 'address' },
|
|
172
|
+
{ type: 'uint256' },
|
|
173
|
+
{ type: 'uint256' },
|
|
174
|
+
{ type: 'uint256' },
|
|
175
|
+
{ type: 'address' },
|
|
176
|
+
{ type: 'uint64' },
|
|
177
|
+
], [
|
|
180
178
|
claims.userAddress,
|
|
181
179
|
claims.recipientAddress,
|
|
182
180
|
claims.tabId,
|
|
183
181
|
claims.reqId,
|
|
184
182
|
claims.amount,
|
|
185
183
|
claims.assetAddress,
|
|
186
|
-
claims.timestamp,
|
|
184
|
+
BigInt(claims.timestamp),
|
|
187
185
|
]);
|
|
188
186
|
return payload;
|
|
189
187
|
}
|
|
190
188
|
class PaymentSigner {
|
|
189
|
+
signer;
|
|
191
190
|
constructor(signer) {
|
|
192
191
|
this.signer = signer;
|
|
193
192
|
}
|
|
@@ -195,6 +194,9 @@ class PaymentSigner {
|
|
|
195
194
|
try {
|
|
196
195
|
validateGuaranteeSigningContext(params, claims, { signerAddress: this.signer.address });
|
|
197
196
|
if (scheme === models_1.SigningScheme.EIP712) {
|
|
197
|
+
if (!this.signer.signTypedData) {
|
|
198
|
+
throw new errors_1.SigningError('signTypedData is not supported for this account');
|
|
199
|
+
}
|
|
198
200
|
const typed = buildGuaranteeTypedData(params, claims);
|
|
199
201
|
const signature = await this.signer.signTypedData({
|
|
200
202
|
domain: typed.domain,
|
|
@@ -205,6 +207,9 @@ class PaymentSigner {
|
|
|
205
207
|
return { signature, scheme };
|
|
206
208
|
}
|
|
207
209
|
if (scheme === models_1.SigningScheme.EIP191) {
|
|
210
|
+
if (!this.signer.signMessage) {
|
|
211
|
+
throw new errors_1.SigningError('signMessage is not supported for this account');
|
|
212
|
+
}
|
|
208
213
|
const message = encodeGuaranteeEip191(claims);
|
|
209
214
|
const signature = await this.signer.signMessage({ message });
|
|
210
215
|
return { signature, scheme };
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Hex } from 'viem';
|
|
1
2
|
export declare class ValidationError extends Error {
|
|
2
3
|
constructor(message: string);
|
|
3
4
|
}
|
|
@@ -6,3 +7,4 @@ export declare function normalizePrivateKey(raw: string): string;
|
|
|
6
7
|
export declare function normalizeAddress(raw: string): string;
|
|
7
8
|
export declare function parseU256(value: number | bigint | string): bigint;
|
|
8
9
|
export declare function serializeU256(value: number | bigint | string): string;
|
|
10
|
+
export declare function hexFromBytes(bytes: Uint8Array): Hex;
|