-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.ts
191 lines (174 loc) · 3.44 KB
/
sign.ts
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
// Types/constants
export const chainIdToName = {
1: 'Ethereum',
137: 'Polygon',
204: 'opBNB Mainnet',
7000: 'ZetaChain',
8453: 'Base',
'arweave': 'Arweave',
'ipfs': 'IPFS',
} as const
type Paginated<T> = {
total: number,
rows: T,
page: number,
size: number,
}
export type SchemaSummary = {
id: string,
mode: string,
name: string,
description: string,
registrant: `0x${string}`,
chainType: 'offchain' | 'onchain',
chainId: keyof typeof chainIdToName,
schemaId: string,
transactionHash: string,
attestationCount: number,
}
export type Schema = {
id: string,
mode: string,
name: string,
description: string,
registrant: `0x${string}`,
registerTimestamp: number,
chainType: 'offchain' | 'onchain',
chainId: keyof typeof chainIdToName,
schemaId: string,
revocable: boolean,
maxValidFor: number,
transactionHash: string,
dataLocation: 'arweave',
originalData: string,
hook: string,
data: {
name: string,
type: string,
}[],
}
export type AttestationSummary = {
id: string,
mode: string,
chainType: string,
chainId: string,
attestationId: `0x${string}`
schemaId: `0x${string}`,
attester: `0x${string}`,
attestTimestamp: number,
recipients: `0x${string}`[],
}
export type Attestation = {
id: string,
mode: string,
chainType: string,
chainId: string,
attestationId: `0x${string}`
schemaId: `0x${string}`,
fullSchemaId: `0x${string}`,
linkedAttestation: string,
attester: `0x${string}`,
from: `0x${string}`,
attestTimestamp: number,
validUntil: string,
revoked: boolean,
revokeTimestamp: null,
revokeReason: null,
revokeTransactionHash: null,
data: string,
dataLocation: string,
recipients: `0x${string}`[],
schema: {
id: string,
schemaId: string,
name: string,
data: {
name: string,
type: string,
}[],
},
}
// Functions
const get = async <T>(
endpoint: string,
params?: Record<string, string | number>,
) => (
await fetch(`https://scan.sign.global/api/scan/${endpoint}?${new URLSearchParams(params as Record<string, string>)}`)
.then((response) => response.json())
.then(result => {
if(result.message !== 'ok') {
throw new Error(result.message)
}
return result.data as T
})
)
// API
export const getTopSchemas = async () => (
await get<Paginated<SchemaSummary[]>>(`top-schemas`)
.then(data => data.rows)
)
export const getTrendingSchemas = async () => (
await get<Paginated<SchemaSummary[]>>(`trending-schemas`)
.then(data => data.rows)
)
export const getSchemas = async ({
page,
}: {
page: number
}) => (
await get<
{
rows: SchemaSummary[],
}
>(`schemas`, {
page,
})
.then(data => data.rows)
)
export const getSchema = async (
schemaId: string
) => (
await get<Schema>(`schemas/${schemaId}`)
)
export const getAttestations = async ({
schemaId,
page,
}: {
schemaId: string,
page: number,
}) => (
await get<Paginated<AttestationSummary>>(`attestations`, {
schemaId,
page,
})
.then(data => data.rows)
)
export const getAttestation = async ({
attestationId,
}: {
attestationId: string,
}) => (
await get<Attestation>(`attestations/${attestationId}`)
)
export const getSchemasForAddress = async ({
address,
page,
}: {
address: `0x${string}`,
page: number,
}) => (
await get<Paginated<SchemaSummary>>(`addresses/${address}/schemas`, {
page,
})
)
export const getAttestationsForAddress = async ({
address,
page,
}: {
address: `0x${string}`,
page: number,
}) => (
await get<Paginated<AttestationSummary>>(`addresses/${address}/attestations`, {
page,
})
)