-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathnfts-metaplex-upload.spec.js
More file actions
194 lines (164 loc) · 6.43 KB
/
nfts-metaplex-upload.spec.js
File metadata and controls
194 lines (164 loc) · 6.43 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
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
import assert from 'assert'
import {
createTestUserWithFixedToken,
DBTestClient,
rawClient,
} from './scripts/helpers.js'
import { fixtures } from './scripts/fixtures.js'
import { createCar } from './scripts/car.js'
describe('Metaplex Upload', () => {
/** @type{DBTestClient} */
let client
before(async () => {
const user = await createTestUserWithFixedToken({
token: 'metaplex-test-token',
})
client = new DBTestClient(user)
})
it('should upload a single CAR file with a CID-specific token', async () => {
const { root, car } = await createCar('hello world car')
// expected CID for the above data
const cid = 'bafkreifeqjorwymdmh77ars6tbrtno74gntsdcvqvcycucidebiri2e7qy'
assert.strictEqual(root.toString(), cid, 'car file has correct root')
const fixture = fixtures.metaplexAuth.v1[cid]
assert.notEqual(fixture, null, 'no fixture for cid ' + cid)
const res = await fetch('metaplex/upload', {
method: 'POST',
headers: {
'x-web3auth': `Metaplex ${fixture.token}`,
'Content-Type': 'application/vnd.ipld.car',
},
body: car,
})
assert(res, 'Server responded')
assert(res.ok, 'Server response ok')
const { ok, value } = await res.json()
assert(ok, 'Server response payload has `ok` property')
assert.strictEqual(value.cid, cid, 'Server responded with expected CID')
assert.strictEqual(
value.type,
'application/vnd.ipld.car',
'type should match blob mime-type'
)
const { data } = await rawClient
.from('upload')
.select('*, content(*)')
.match({ source_cid: cid, user_id: client.userId })
.single()
// @ts-ignore
assert.equal(data.source_cid, cid)
assert.equal(data.deleted_at, null)
assert.equal(data.content.dag_size, 15, 'correct dag size')
const expectedMeta = {
iss: fixture.meta.iss,
rootCID: fixture.meta.req.put.rootCID,
solanaCluster: fixture.meta.req.put.tags.solanaCluster,
mintingAgent: fixture.meta.req.put.tags.mintingAgent,
agentVersion: fixture.meta.req.put.tags.agentVersion,
}
assert.deepEqual(data.meta, expectedMeta, 'metadata matches jwt payload')
})
it('should support payloads without mintingAgent tag', async () => {
const { root, car } = await createCar('hello world car')
// expected CID for the above data
const cid = 'bafkreifeqjorwymdmh77ars6tbrtno74gntsdcvqvcycucidebiri2e7qy'
assert.strictEqual(root.toString(), cid, 'car file has correct root')
const fixture = fixtures.metaplexAuth.v0[cid]
assert.notEqual(fixture, null, 'no fixture for cid ' + cid)
const res = await fetch('metaplex/upload', {
method: 'POST',
headers: {
'x-web3auth': `Metaplex ${fixture.token}`,
'Content-Type': 'application/vnd.ipld.car',
},
body: car,
})
assert(res, 'Server responded')
assert(res.ok, 'Server response ok')
const { ok, value } = await res.json()
assert(ok, 'Server response payload has `ok` property')
assert.strictEqual(value.cid, cid, 'Server responded with expected CID')
assert.strictEqual(
value.type,
'application/vnd.ipld.car',
'type should match blob mime-type'
)
const { data } = await rawClient
.from('upload')
.select('*, content(*)')
.match({ source_cid: cid, user_id: client.userId })
.single()
// @ts-ignore
assert.equal(data.source_cid, cid)
assert.equal(data.deleted_at, null)
assert.equal(data.content.dag_size, 15, 'correct dag size')
const expectedMeta = {
iss: fixture.meta.iss,
rootCID: fixture.meta.req.put.rootCID,
solanaCluster: fixture.meta.req.put.tags['solana-cluster'],
mintingAgent: 'unknown',
}
assert.deepEqual(data.meta, expectedMeta, 'metadata matches jwt payload')
})
it('should fail if token has an invalid signature', async () => {
const { root, car } = await createCar('hello world car')
// expected CID for the above data
const cid = 'bafkreifeqjorwymdmh77ars6tbrtno74gntsdcvqvcycucidebiri2e7qy'
assert.strictEqual(root.toString(), cid, 'car file has correct root')
const fixture = fixtures.metaplexAuth.v1[cid]
assert.notEqual(fixture, null, 'no fixture for cid ' + cid)
const tokenParts = fixture.token.split('.')
tokenParts[2] = tokenParts[2].replace('0', '1')
const alteredToken = tokenParts.join('.')
const res = await fetch('metaplex/upload', {
method: 'POST',
headers: {
'x-web3auth': `Metaplex ${alteredToken}`,
'Content-Type': 'application/vnd.ipld.car',
},
body: car,
})
assert(res, 'Server responded')
assert.equal(res.status, 401, 'Expected auth error, but response was ok')
})
it('should fail if token payload is modified', async () => {
const { root, car } = await createCar('hello world car')
// expected CID for the above data
const cid = 'bafkreifeqjorwymdmh77ars6tbrtno74gntsdcvqvcycucidebiri2e7qy'
assert.strictEqual(root.toString(), cid, 'car file has correct root')
const fixture = fixtures.metaplexAuth.v1[cid]
assert.notEqual(fixture, null, 'no fixture for cid ' + cid)
const tokenParts = fixture.token.split('.')
tokenParts[1] = tokenParts[1].replace('0', '1')
const alteredToken = tokenParts.join('.')
const res = await fetch('metaplex/upload', {
method: 'POST',
headers: {
'x-web3auth': `Metaplex ${alteredToken}`,
'Content-Type': 'application/vnd.ipld.car',
},
body: car,
})
assert(res, 'Server responded')
assert.equal(res.status, 401, 'Expected auth error, but response was ok')
})
it('should fail if uploaded CAR has different CID than specified in token', async () => {
// cid for the "hello world car". we have a valid token saved for this CID as a fixture
const signedCID =
'bafkreifeqjorwymdmh77ars6tbrtno74gntsdcvqvcycucidebiri2e7qy'
const fixture = fixtures.metaplexAuth.v1[signedCID]
assert.notEqual(fixture, null, 'no fixture for cid ' + signedCID)
const { root, car } = await createCar('a different car')
assert.notEqual(root.toString(), signedCID)
const res = await fetch('metaplex/upload', {
method: 'POST',
headers: {
'x-web3auth': `Metaplex ${fixture.token}`,
'Content-Type': 'application/vnd.ipld.car',
},
body: car,
})
assert(res, 'Server responded')
assert.equal(res.status, 401, 'Expected auth error, but response was ok')
})
})