-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmandate.js
More file actions
162 lines (135 loc) · 4.31 KB
/
mandate.js
File metadata and controls
162 lines (135 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const crypto = require('crypto');
const { secp256k1 } = require('@noble/curves/secp256k1');
const constants = require('../constants');
function canonicalJSON(obj) {
if (obj === null || obj === undefined) return 'null';
if (typeof obj === 'boolean' || typeof obj === 'number') return JSON.stringify(obj);
if (typeof obj === 'string') return JSON.stringify(obj);
if (Array.isArray(obj)) {
const items = obj.map((item) => canonicalJSON(item));
return `[${items.join(',')}]`;
}
if (typeof obj === 'object') {
const sortedKeys = Object.keys(obj).sort();
const pairs = sortedKeys.map((key) => `${JSON.stringify(key)}:${canonicalJSON(obj[key])}`);
return `{${pairs.join(',')}}`;
}
return JSON.stringify(obj);
}
function sha256hex(str) {
return crypto.createHash('sha256').update(str, 'utf8').digest('hex');
}
function base64url(input) {
const buf = Buffer.isBuffer(input) ? input : Buffer.from(input, 'utf8');
return buf.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
function signES256K(claims, privateKeyHex) {
const header = { alg: 'ES256K', typ: 'JWT' };
const b64header = base64url(JSON.stringify(header));
const b64claims = base64url(JSON.stringify(claims));
const signingInput = `${b64header}.${b64claims}`;
const msgHash = crypto.createHash('sha256')
.update(signingInput, 'utf8')
.digest();
const privateKeyBytes = Buffer.from(privateKeyHex.replace(/^0x/, ''), 'hex');
if (privateKeyBytes.length !== 32) {
throw new Error('MERCHANT_PRIVATE_KEY must be a 32-byte hex value');
}
const compactSig = Buffer.from(secp256k1.sign(msgHash, privateKeyBytes).toCompactRawBytes());
const b64sig = base64url(compactSig);
return `${b64header}.${b64claims}.${b64sig}`;
}
function getTokenAddress(token) {
const tokenCfg = constants.TOKEN_CONFIG?.[token];
if (tokenCfg?.address) return tokenCfg.address;
throw new Error(`Unsupported token for cart mandate: ${token}. Configure TOKEN_CONFIG_JSON in backend/.env.`);
}
function getTokenCoin(token) {
const tokenCfg = constants.TOKEN_CONFIG?.[token];
if (tokenCfg?.coin) return tokenCfg.coin;
throw new Error(`Unsupported token for cart mandate: ${token}. Configure TOKEN_CONFIG_JSON in backend/.env.`);
}
async function buildCartMandate({
merchantName,
cartId,
paymentRequestId,
amount,
token,
description,
toAddress,
expiryHours = 2,
}) {
const now = Date.now();
const iatSec = Math.floor(now / 1000);
const expSec = iatSec + expiryHours * 3600;
const expiry = new Date(now + expiryHours * 3600 * 1000);
const contractAddress = getTokenAddress(token);
const coin = getTokenCoin(token);
const contents = {
id: cartId,
user_cart_confirmation_required: true,
merchant_name: merchantName,
cart_expiry: expiry.toISOString(),
payment_request: {
method_data: [
{
supported_methods: 'https://www.x402.org/',
data: {
supported_methods: 'https://www.x402.org/',
x402Version: 2,
network: 'hashkey-testnet',
chain_id: 133,
contract_address: contractAddress,
pay_to: toAddress,
coin,
},
},
],
details: {
id: paymentRequestId,
display_items: [
{
label: description || 'Payment',
amount: {
currency: 'USD',
value: amount,
},
},
],
total: {
label: 'Total',
amount: {
currency: 'USD',
value: amount,
},
},
},
},
};
const cartHash = sha256hex(canonicalJSON(contents));
const claims = {
iss: merchantName,
sub: merchantName,
aud: 'HashkeyMerchant',
iat: iatSec,
exp: expSec,
jti: `JWT-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
cart_hash: cartHash,
};
const jwt = signES256K(claims, constants.MERCHANT_PRIVATE_KEY);
const cartMandate = {
contents,
merchant_authorization: jwt,
};
return { cartMandate, jwt };
}
module.exports = {
buildCartMandate,
canonicalJSON,
sha256hex,
getTokenAddress,
getTokenCoin,
};