-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathAttestationDataCard.tsx
More file actions
106 lines (96 loc) · 3.71 KB
/
AttestationDataCard.tsx
File metadata and controls
106 lines (96 loc) · 3.71 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
import { Account, useAccountInfo, useFetchAccountInfo } from '@providers/accounts';
import React from 'react';
import {
Attestation as SasAttestation,
convertSasSchemaToBorshSchema,
decodeSchema,
Schema as SasSchema,
} from 'sas-lib';
import { SolarizedJsonViewer as ReactJson } from '@/app/components/common/JsonViewer';
import { toBase64 } from '@/app/shared/lib/bytes';
import { Logger } from '@/app/shared/lib/logger';
import {
decodeAccount,
decodeWithType,
deserializeAttestationDataWithBorsh200,
isAttestationAccount,
} from '@/app/utils/attestation-service';
import { mapToPublicKey } from '@/app/utils/kit-wrapper';
export function AttestationDataCard({ account, onNotFound }: { account?: Account; onNotFound: () => never }) {
if (!account || !isAttestationAccount(account)) {
return onNotFound();
}
const decoded = decodeAccount(account);
if (decoded?.type === 'attestation') {
return <AttestationCard attestation={decoded.data.data} />;
} else if (decoded?.type === 'schema') {
return <SchemaCard schema={decoded.data.data} />;
}
return onNotFound();
}
function SchemaCard({ schema }: { schema: SasSchema }) {
const borshSchema = convertSasSchemaToBorshSchema(schema);
return (
<div className="card">
<div className="card-header">
<div className="row align-items-center">
<div className="col">
<h3 className="card-header-title">Schema Layout (Borsh)</h3>
</div>
</div>
</div>
<div className="card metadata-json-viewer m-4">
<ReactJson src={borshSchema['schema']} style={{ padding: 25 }} name={false} />
</div>
</div>
);
}
function AttestationCard({ attestation }: { attestation: SasAttestation }) {
const schemaAccountInfo = useAccountInfo(mapToPublicKey(attestation.schema).toBase58());
const fetchAccountInfo = useFetchAccountInfo();
React.useEffect(() => {
if (!schemaAccountInfo?.data) {
fetchAccountInfo(mapToPublicKey(attestation.schema), 'parsed');
}
}, [schemaAccountInfo?.data, fetchAccountInfo, attestation.schema]);
let decoded: any | null = null;
try {
if (schemaAccountInfo?.data) {
const schema: SasSchema = decodeWithType(schemaAccountInfo.data, 'schema', decodeSchema)?.data.data;
decoded = deserializeAttestationDataWithBorsh200(schema, Uint8Array.from(attestation.data));
}
} catch (e) {
Logger.error(e);
}
return (
<div className="card">
<div className="card-header">
<div className="row align-items-center">
<div className="col">
<h3 className="card-header-title">Attestation Data {decoded ? '' : 'Raw (Base64)'}</h3>
</div>
</div>
</div>
{decoded ? (
<div className="card metadata-json-viewer m-4">
<ReactJson src={decoded} style={{ padding: 25 }} name={false} />
</div>
) : (
<div
className="font-monospace"
style={{
fontSize: '0.85rem',
lineHeight: '1.2',
maxWidth: '100%',
overflowWrap: 'break-word',
padding: '1rem',
whiteSpace: 'normal',
wordBreak: 'break-all',
}}
>
{toBase64(new Uint8Array(attestation.data)) || '(empty)'}
</div>
)}
</div>
);
}