r402-near 0.17.1

NEAR chain support for the x402 payment protocol.
Documentation
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! JSON-RPC client for NEAR preflight reads and (facilitator) settlement.

use std::fmt::{Debug, Formatter};

use near_jsonrpc_client::errors::JsonRpcError;
use near_jsonrpc_client::{JsonRpcClient, methods};
use near_jsonrpc_primitives::types::query::{QueryResponseKind, RpcQueryError};
use near_primitives::types::{AccountId, BlockReference, Finality, FunctionArgs};
use near_primitives::views::{
    AccessKeyPermissionView, ExecutionStatusView, FinalExecutionStatus, QueryRequest,
};
use serde::Deserialize;

use crate::EMPTY_CONTRACT_CODE_HASH;

/// Access-key permission variants relevant to delegate-action verification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NearAccessKeyPermissionKind {
    /// Full-access key (required for `ft_transfer` with 1 yocto deposit).
    FullAccess,
    /// Restricted function-call key (must be rejected).
    FunctionCall,
    /// Any other permission variant (must be rejected).
    Unknown,
}

/// Result of an on-chain `view_access_key` query.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NearAccessKeyView {
    /// On-chain nonce for the access key.
    pub nonce: u64,
    /// Normalized permission variant.
    pub permission_kind: NearAccessKeyPermissionKind,
}

/// Result of an on-chain `view_account` query.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NearAccountView {
    /// Base58 code hash; equals [`crate::EMPTY_CONTRACT_CODE_HASH`] when no
    /// contract is deployed.
    pub code_hash: String,
}

impl NearAccountView {
    /// Returns `true` when the account has no contract code.
    #[must_use]
    pub fn has_no_code(&self) -> bool {
        self.code_hash == EMPTY_CONTRACT_CODE_HASH
    }
}

/// Status of NEP-145 storage registration for a recipient.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NearStorageBalance {
    /// Token does not implement NEP-145.
    Unsupported,
    /// Token implements NEP-145 and the account is registered.
    Registered,
    /// Token implements NEP-145 and the account is not registered.
    Unregistered,
}

/// Status of a single on-chain receipt outcome.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NearReceiptStatus {
    /// Inner `ft_transfer` receipt succeeded.
    Success {
        /// Base64-encoded success value.
        value: String,
    },
    /// Inner receipt failed (or was not observed as success).
    Failure {
        /// Error description.
        error: String,
    },
}

/// Result of submitting the outer relayer transaction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NearSettlementOutcome {
    /// Final outer transaction hash.
    pub transaction: String,
    /// Status of the inner `ft_transfer` receipt.
    pub inner_receipt: NearReceiptStatus,
}

/// Errors from NEAR JSON-RPC reads and submits.
#[derive(Debug, thiserror::Error)]
pub enum NearRpcError {
    /// JSON-RPC transport or handler failure.
    #[error("near rpc error: {0}")]
    Rpc(String),
    /// Response JSON could not be parsed.
    #[error("near rpc parse error: {0}")]
    Parse(String),
}

/// Read-only NEAR RPC operations used by the client and facilitator.
pub trait NearRpc: Send + Sync {
    /// Current final block height.
    fn current_block_height(&self) -> impl Future<Output = Result<u64, NearRpcError>> + Send;

    /// `view_account`. Resolves to `None` when the account does not exist.
    fn view_account(
        &self,
        account_id: &str,
    ) -> impl Future<Output = Result<Option<NearAccountView>, NearRpcError>> + Send;

    /// `view_access_key`. Resolves to `None` when the key does not exist.
    fn view_access_key(
        &self,
        account_id: &str,
        public_key: &str,
    ) -> impl Future<Output = Result<Option<NearAccessKeyView>, NearRpcError>> + Send;

    /// `ft_balance_of` on the token contract, in atomic units.
    fn ft_balance_of(
        &self,
        token: &str,
        account_id: &str,
    ) -> impl Future<Output = Result<u128, NearRpcError>> + Send;

    /// `storage_balance_of` on the token contract (NEP-145).
    fn storage_balance_of(
        &self,
        token: &str,
        account_id: &str,
    ) -> impl Future<Output = Result<NearStorageBalance, NearRpcError>> + Send;
}

/// JSON-RPC client pointed at a FastNEAR-compatible endpoint.
#[derive(Clone)]
pub struct NearJsonRpc {
    client: JsonRpcClient,
}

impl Debug for NearJsonRpc {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NearJsonRpc").finish_non_exhaustive()
    }
}

impl NearJsonRpc {
    /// Connects to the given RPC URL.
    #[must_use]
    pub fn connect(url: impl AsRef<str>) -> Self {
        Self {
            client: JsonRpcClient::connect(url.as_ref()),
        }
    }

    /// Returns a clone of the underlying JSON-RPC client.
    #[must_use]
    pub fn client(&self) -> JsonRpcClient {
        self.client.clone()
    }
}

impl NearRpc for NearJsonRpc {
    async fn current_block_height(&self) -> Result<u64, NearRpcError> {
        let block = self
            .client
            .call(methods::block::RpcBlockRequest {
                block_reference: BlockReference::Finality(Finality::Final),
            })
            .await
            .map_err(|e| NearRpcError::Rpc(e.to_string()))?;
        Ok(block.header.height)
    }

    async fn view_account(
        &self,
        account_id: &str,
    ) -> Result<Option<NearAccountView>, NearRpcError> {
        let account_id = account_id
            .parse::<AccountId>()
            .map_err(|e| NearRpcError::Parse(format!("{e}")))?;
        let request = methods::query::RpcQueryRequest {
            block_reference: BlockReference::Finality(Finality::Final),
            request: QueryRequest::ViewAccount { account_id },
        };
        match self.client.call(request).await {
            Ok(response) => match response.kind {
                QueryResponseKind::ViewAccount(view) => Ok(Some(NearAccountView {
                    code_hash: view.code_hash.to_string(),
                })),
                other => Err(NearRpcError::Parse(format!(
                    "unexpected view_account response: {other:?}"
                ))),
            },
            Err(err) if is_unknown_account(&err) => Ok(None),
            Err(err) => Err(NearRpcError::Rpc(err.to_string())),
        }
    }

    async fn view_access_key(
        &self,
        account_id: &str,
        public_key: &str,
    ) -> Result<Option<NearAccessKeyView>, NearRpcError> {
        let account_id = account_id
            .parse::<AccountId>()
            .map_err(|e| NearRpcError::Parse(format!("{e}")))?;
        let public_key = public_key
            .parse::<near_crypto::PublicKey>()
            .map_err(|e| NearRpcError::Parse(format!("{e}")))?;
        let request = methods::query::RpcQueryRequest {
            block_reference: BlockReference::Finality(Finality::Final),
            request: QueryRequest::ViewAccessKey {
                account_id,
                public_key,
            },
        };
        match self.client.call(request).await {
            Ok(response) => match response.kind {
                QueryResponseKind::AccessKey(view) => Ok(Some(NearAccessKeyView {
                    nonce: view.nonce,
                    permission_kind: permission_kind(&view.permission),
                })),
                other => Err(NearRpcError::Parse(format!(
                    "unexpected view_access_key response: {other:?}"
                ))),
            },
            Err(err) if is_unknown_access_key(&err) => Ok(None),
            Err(err) => Err(NearRpcError::Rpc(err.to_string())),
        }
    }

    async fn ft_balance_of(&self, token: &str, account_id: &str) -> Result<u128, NearRpcError> {
        let result = call_function(&self.client, token, "ft_balance_of", account_id).await?;
        parse_ft_balance(&result)
    }

    async fn storage_balance_of(
        &self,
        token: &str,
        account_id: &str,
    ) -> Result<NearStorageBalance, NearRpcError> {
        match call_function(&self.client, token, "storage_balance_of", account_id).await {
            Ok(bytes) => parse_storage_balance(&bytes),
            Err(err) if is_method_not_found_msg(&err.to_string()) => {
                Ok(NearStorageBalance::Unsupported)
            }
            Err(err) => Err(err),
        }
    }
}

impl<T: NearRpc + Send + Sync> NearRpc for &T {
    fn current_block_height(&self) -> impl Future<Output = Result<u64, NearRpcError>> + Send {
        (**self).current_block_height()
    }

    fn view_account(
        &self,
        account_id: &str,
    ) -> impl Future<Output = Result<Option<NearAccountView>, NearRpcError>> + Send {
        (**self).view_account(account_id)
    }

    fn view_access_key(
        &self,
        account_id: &str,
        public_key: &str,
    ) -> impl Future<Output = Result<Option<NearAccessKeyView>, NearRpcError>> + Send {
        (**self).view_access_key(account_id, public_key)
    }

    fn ft_balance_of(
        &self,
        token: &str,
        account_id: &str,
    ) -> impl Future<Output = Result<u128, NearRpcError>> + Send {
        (**self).ft_balance_of(token, account_id)
    }

    fn storage_balance_of(
        &self,
        token: &str,
        account_id: &str,
    ) -> impl Future<Output = Result<NearStorageBalance, NearRpcError>> + Send {
        (**self).storage_balance_of(token, account_id)
    }
}

impl<T: NearRpc + Send + Sync> NearRpc for std::sync::Arc<T> {
    fn current_block_height(&self) -> impl Future<Output = Result<u64, NearRpcError>> + Send {
        (**self).current_block_height()
    }

    fn view_account(
        &self,
        account_id: &str,
    ) -> impl Future<Output = Result<Option<NearAccountView>, NearRpcError>> + Send {
        (**self).view_account(account_id)
    }

    fn view_access_key(
        &self,
        account_id: &str,
        public_key: &str,
    ) -> impl Future<Output = Result<Option<NearAccessKeyView>, NearRpcError>> + Send {
        (**self).view_access_key(account_id, public_key)
    }

    fn ft_balance_of(
        &self,
        token: &str,
        account_id: &str,
    ) -> impl Future<Output = Result<u128, NearRpcError>> + Send {
        (**self).ft_balance_of(token, account_id)
    }

    fn storage_balance_of(
        &self,
        token: &str,
        account_id: &str,
    ) -> impl Future<Output = Result<NearStorageBalance, NearRpcError>> + Send {
        (**self).storage_balance_of(token, account_id)
    }
}

const fn permission_kind(permission: &AccessKeyPermissionView) -> NearAccessKeyPermissionKind {
    match permission {
        AccessKeyPermissionView::FullAccess => NearAccessKeyPermissionKind::FullAccess,
        AccessKeyPermissionView::FunctionCall { .. } => NearAccessKeyPermissionKind::FunctionCall,
        AccessKeyPermissionView::GasKeyFunctionCall { .. }
        | AccessKeyPermissionView::GasKeyFullAccess { .. } => NearAccessKeyPermissionKind::Unknown,
    }
}

fn is_unknown_account(err: &JsonRpcError<RpcQueryError>) -> bool {
    matches!(
        err.handler_error(),
        Some(RpcQueryError::UnknownAccount { .. })
    )
}

fn is_unknown_access_key(err: &JsonRpcError<RpcQueryError>) -> bool {
    matches!(
        err.handler_error(),
        Some(RpcQueryError::UnknownAccessKey { .. })
    )
}

fn is_method_not_found_msg(msg: &str) -> bool {
    msg.contains("MethodNotFound")
        || msg.contains("MethodResolveError")
        || msg.contains("MethodEmptyName")
}

async fn call_function(
    client: &JsonRpcClient,
    token: &str,
    method_name: &str,
    account_id: &str,
) -> Result<Vec<u8>, NearRpcError> {
    let contract_id = token
        .parse::<AccountId>()
        .map_err(|e| NearRpcError::Parse(format!("{e}")))?;
    let args = serde_json::to_vec(&serde_json::json!({ "account_id": account_id }))
        .map_err(|e| NearRpcError::Parse(e.to_string()))?;
    let request = methods::query::RpcQueryRequest {
        block_reference: BlockReference::Finality(Finality::Final),
        request: QueryRequest::CallFunction {
            account_id: contract_id,
            method_name: method_name.to_owned(),
            args: FunctionArgs::from(args),
        },
    };
    match client.call(request).await {
        Ok(response) => match response.kind {
            QueryResponseKind::CallResult(result) => Ok(result.result),
            other => Err(NearRpcError::Parse(format!(
                "unexpected {method_name} response: {other:?}"
            ))),
        },
        Err(err) => {
            if let Some(RpcQueryError::ContractExecutionError { vm_error, .. }) =
                err.handler_error()
                && is_method_not_found_msg(vm_error)
            {
                return Err(NearRpcError::Rpc(vm_error.clone()));
            }
            Err(NearRpcError::Rpc(err.to_string()))
        }
    }
}

fn parse_ft_balance(bytes: &[u8]) -> Result<u128, NearRpcError> {
    let value: serde_json::Value =
        serde_json::from_slice(bytes).map_err(|e| NearRpcError::Parse(e.to_string()))?;
    let as_str = match value {
        serde_json::Value::String(s) => s,
        other => other.to_string(),
    };
    let trimmed = as_str.trim_matches('"');
    if !trimmed.bytes().all(|b| b.is_ascii_digit()) {
        return Err(NearRpcError::Parse(format!(
            "invalid_ft_balance_of_result: {trimmed}"
        )));
    }
    trimmed
        .parse()
        .map_err(|_| NearRpcError::Parse("invalid_ft_balance_of_result".to_owned()))
}

fn parse_storage_balance(bytes: &[u8]) -> Result<NearStorageBalance, NearRpcError> {
    let value: serde_json::Value =
        serde_json::from_slice(bytes).map_err(|e| NearRpcError::Parse(e.to_string()))?;
    match value {
        serde_json::Value::Null => Ok(NearStorageBalance::Unregistered),
        serde_json::Value::Object(_) => Ok(NearStorageBalance::Registered),
        other => Err(NearRpcError::Parse(format!(
            "invalid storage_balance_of result: {other}"
        ))),
    }
}

/// Interprets a final execution outcome for settlement (spec §7).
///
/// Success requires a token-contract receipt with `SuccessValue`.
#[must_use]
pub fn interpret_settlement_outcome(
    status: &FinalExecutionStatus,
    receipts: &[(String, ExecutionStatusView)],
    token_contract_id: &str,
) -> NearReceiptStatus {
    if let FinalExecutionStatus::Failure(err) = status {
        return NearReceiptStatus::Failure {
            error: format!("{err:?}"),
        };
    }
    for (_executor, receipt_status) in receipts {
        if let ExecutionStatusView::Failure(err) = receipt_status {
            return NearReceiptStatus::Failure {
                error: format!("{err:?}"),
            };
        }
    }
    let token_status = receipts
        .iter()
        .find(|(executor, _)| executor == token_contract_id)
        .map(|(_, receipt_status)| receipt_status);
    match token_status {
        Some(ExecutionStatusView::SuccessValue(value)) => NearReceiptStatus::Success {
            value: String::from_utf8_lossy(value).into_owned(),
        },
        _ => NearReceiptStatus::Failure {
            error: "inner_ft_transfer_receipt_not_successful".to_owned(),
        },
    }
}

/// JSON shape of `ft_transfer` args.
#[derive(Debug, Clone, Deserialize)]
pub struct FtTransferArgs {
    /// Recipient account ID.
    pub receiver_id: String,
    /// Amount in atomic units as a decimal string.
    pub amount: String,
}

/// Parses NEP-141 `ft_transfer` JSON args.
///
/// # Errors
///
/// Returns [`NearRpcError::Parse`] when JSON is malformed or required fields
/// are missing.
pub fn parse_ft_transfer_args(args: &[u8]) -> Result<FtTransferArgs, NearRpcError> {
    let decoded: FtTransferArgs =
        serde_json::from_slice(args).map_err(|e| NearRpcError::Parse(e.to_string()))?;
    if decoded.receiver_id.is_empty() {
        return Err(NearRpcError::Parse(
            "invalid_ft_transfer_args_receiver_id".to_owned(),
        ));
    }
    if decoded.amount.is_empty() || !decoded.amount.bytes().all(|b| b.is_ascii_digit()) {
        return Err(NearRpcError::Parse(
            "invalid_ft_transfer_args_amount".to_owned(),
        ));
    }
    Ok(decoded)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, reason = "test assertions")]
mod tests {
    use super::*;

    #[test]
    fn parse_ft_transfer_args_accepts_valid() {
        let args = br#"{"receiver_id":"merchant.testnet","amount":"1000000"}"#;
        let parsed = parse_ft_transfer_args(args).unwrap();
        assert_eq!(parsed.receiver_id, "merchant.testnet");
        assert_eq!(parsed.amount, "1000000");
    }

    #[test]
    fn parse_ft_transfer_args_rejects_bad_amount() {
        let args = br#"{"receiver_id":"merchant.testnet","amount":"1.0"}"#;
        assert!(parse_ft_transfer_args(args).is_err());
    }

    #[test]
    fn parse_storage_null_is_unregistered() {
        assert_eq!(
            parse_storage_balance(b"null").unwrap(),
            NearStorageBalance::Unregistered
        );
    }

    #[test]
    fn parse_storage_object_is_registered() {
        assert_eq!(
            parse_storage_balance(br#"{"total":"1","available":"1"}"#).unwrap(),
            NearStorageBalance::Registered
        );
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, reason = "test assertions")]
mod json_rpc_tests {
    use serde_json::json;
    use wiremock::matchers::method;
    use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};

    use super::*;

    struct QueryAccount;

    impl Respond for QueryAccount {
        fn respond(&self, request: &Request) -> ResponseTemplate {
            let body: serde_json::Value =
                serde_json::from_slice(&request.body).unwrap_or_else(|_| json!({}));
            let id = body.get("id").cloned().unwrap_or_else(|| json!("dontcare"));
            ResponseTemplate::new(200).set_body_json(json!({
                "jsonrpc": "2.0",
                "id": id,
                "result": {
                    "amount": "0",
                    "locked": "0",
                    "code_hash": "11111111111111111111111111111112",
                    "storage_usage": 0,
                    "storage_paid_at": 0,
                    "block_height": 1000,
                    "block_hash": "11111111111111111111111111111111"
                }
            }))
        }
    }

    #[tokio::test]
    async fn view_account_via_json_rpc() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(QueryAccount)
            .mount(&server)
            .await;
        let rpc = NearJsonRpc::connect(server.uri());
        let account = rpc
            .view_account("alice.testnet")
            .await
            .expect("view_account")
            .expect("account exists");
        assert_eq!(account.code_hash, "11111111111111111111111111111112");
    }
}