forked from holaplex/holaplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign-metadata.ts
181 lines (150 loc) · 4.73 KB
/
sign-metadata.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
import amqplib from 'amqplib'
import singletons from '../../src/modules/singletons';
import { SCHEMAS } from '../../src/modules/singletons/json-schemas';
import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
import { Buffer } from 'buffer';
import { signingQueue } from '../../src/modules/metadata-signing'
import { buildSolana } from '../../src/modules/solana/buildSolana'
/** Adapted from metaplex/js/packages/common/src/actions/metadata.ts */
function signMetadata(
metadata: PublicKey,
creator: PublicKey,
tx: Transaction,
programId: PublicKey
) {
const data = Buffer.from([7]);
const keys = [
{
pubkey: metadata,
isSigner: false,
isWritable: true,
},
{
pubkey: creator,
isSigner: true,
isWritable: false,
},
];
tx.add(new TransactionInstruction({ keys, programId, data }));
}
const MAX_RETRIES = 5
async function consume() {
const connection = await amqplib.connect(process.env.CLOUDAMQP_URL || '');
const channel = await connection.createChannel()
channel.assertExchange(
"delayedDeadLetterExchange",
"x-delayed-message", {autoDelete: false, durable: true, arguments: {'x-delayed-type': "direct"}})
const primaryExchange = await channel.assertExchange("primaryExchange", "fanout" , {
durable: true,
autoDelete: false,
})
channel.assertExchange(
"deadLetterExchange",
"topic"
)
await channel.assertQueue("retryQueue",
{
durable: true,
autoDelete: false,
deadLetterExchange: primaryExchange.exchange,
deadLetterRoutingKey: 'dle-key'
})
await channel.assertQueue(signingQueue,
{
durable: true,
autoDelete: false,
deadLetterExchange: 'delayedDeadLetterExchange',
deadLetterRoutingKey: 'dle-key'
})
channel.bindQueue(
"retryQueue",
"delayedDeadLetterExchange",
"dle-key",
);
channel.consume("retryQueue", async function(msg) {
if (msg !== null) {
const causesOfDeaths = msg.properties.headers['x-death'] || []
const lastDeath = causesOfDeaths[causesOfDeaths.length - 1]
if (lastDeath.count < MAX_RETRIES) {
channel.reject(msg, false);
return;
}
// this is the ultimate death, sit in the dle forever.
channel.publish("deadLetterExchange", "dle", msg.content)
channel.ack(msg, false)
}
})
channel.bindQueue(signingQueue, primaryExchange.exchange, "dle-key");
channel.consume(signingQueue, async function(msg) {
if (msg !== null) {
let parsedMessage;
try {
parsedMessage = JSON.parse(msg.content.toString())
} catch(err) {
console.error(err, msg.toString(), 'could not parse message as json')
channel.reject(msg, false)
return;
}
const schemas = singletons.jsonSchemas;
const validateMessage = schemas.validator(SCHEMAS.signMetaParams);
if (!validateMessage(parsedMessage)) {
channel.reject(msg, false)
console.error(
`Invalid message body: ${validateMessage.errors?.map((e) => e.message).join(', ')}`
);
return;
}
const {
solanaEndpoint: clientSolEndpoint,
metadata: metadataStr,
metaProgramId: metaProgramIdStr,
} = parsedMessage;
const {
connection,
keypair,
endpoint
} = await buildSolana(process.env.SOLANA_SECRET_ID)({
endpoint: clientSolEndpoint,
secrets: singletons.secrets
})
if (!metaProgramIdStr.startsWith('meta')) {
console.error('Invalid program ID');
channel.reject(msg, false)
return;
}
if (clientSolEndpoint !== endpoint) {
console.warn('Mismatched Solana endpoint, ' + clientSolEndpoint);
}
let metadata;
let metaProgramId;
try {
metadata = new PublicKey(metadataStr);
metaProgramId = new PublicKey(metaProgramIdStr);
} catch {
console.error('Invalid public keys');
channel.reject(msg, false)
return;
}
try {
const tx = new Transaction();
signMetadata(metadata, keypair.publicKey, tx, metaProgramId);
tx.feePayer = keypair.publicKey;
tx.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
const signature = await connection.sendTransaction(tx, [keypair]);
const status = (await connection.confirmTransaction(signature)).value;
const err = status.err;
if (err !== null) {
channel.reject(msg, false)
console.error('Approval transaction failed', err);
return;
}
} catch(err) {
console.error(err)
channel.reject(msg, false)
return;
}
channel.ack(msg);
}
})
}
consume()