-
Notifications
You must be signed in to change notification settings - Fork 0
/
erc721_signature_minting.go
379 lines (337 loc) · 12.7 KB
/
erc721_signature_minting.go
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
package web3sdkio
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
signerTypes "github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/google/uuid"
"github.com/web3sdkio/go-sdk/v2/abi"
)
// You can access this interface from the NFT Collection contract under the
// signature interface.
type ERC721SignatureMinting struct {
abi *abi.TokenERC721
helper *contractHelper
storage storage
}
func newERC721SignatureMinting(provider *ethclient.Client, address common.Address, privateKey string, storage storage) (*ERC721SignatureMinting, error) {
if contractAbi, err := abi.NewTokenERC721(address, provider); err != nil {
return nil, err
} else if helper, err := newContractHelper(address, provider, privateKey); err != nil {
return nil, err
} else {
return &ERC721SignatureMinting{
contractAbi,
helper,
storage,
}, nil
}
}
// Mint a token with the data in given payload.
//
// signedPayload: the payload signed by the minters private key being used to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// // Learn more about how to craft a payload in the Generate() function
// signedPayload, err := contract.Signature.Generate(payload)
// tx, err := contract.Signature.Mint(context.Background(), signedPayload)
func (signature *ERC721SignatureMinting) Mint(ctx context.Context, signedPayload *SignedPayload721) (*types.Transaction, error) {
message, err := signature.mapPayloadToContractStruct(ctx, signedPayload.Payload)
if err != nil {
return nil, err
}
txOpts, err := signature.helper.getTxOptions(ctx)
if err != nil {
return nil, err
}
setErc20Allowance(
signature.helper,
message.Price,
message.Currency.String(),
txOpts,
)
tx, err := signature.abi.MintWithSignature(txOpts, *message, signedPayload.Signature)
if err != nil {
return nil, err
}
return signature.helper.awaitTx(tx.Hash())
}
// Mint a batch of token with the data in given payload.
//
// signedPayload: the list of payloads signed by the minters private key being used to mint
//
// returns: the transaction receipt of the batch mint
//
// Example
//
// // Learn more about how to craft multiple payloads in the GenerateBatch() function
// signedPayloads, err := contract.Signature.GenerateBatch(payloads)
// tx, err := contract.Signature.MintBatch(context.Background(), signedPayloads)
func (signature *ERC721SignatureMinting) MintBatch(ctx context.Context, signedPayloads []*SignedPayload721) (*types.Transaction, error) {
contractPayloads := []*abi.ITokenERC721MintRequest{}
for _, signedPayload := range signedPayloads {
if signedPayload.Payload.Price > 0 {
return nil, fmt.Errorf("Can only batch free mints. For mints with a price, use the Mint() function.")
}
payload, err := signature.mapPayloadToContractStruct(ctx, signedPayload.Payload)
if err != nil {
return nil, err
}
contractPayloads = append(contractPayloads, payload)
}
encoded := [][]byte{}
for i, payload := range contractPayloads {
txOpts, err := signature.helper.getEncodedTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := signature.abi.MintWithSignature(txOpts, *payload, signedPayloads[i].Signature)
if err != nil {
return nil, err
}
encoded = append(encoded, tx.Data())
}
txOpts, err := signature.helper.getTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := signature.abi.Multicall(txOpts, encoded)
if err != nil {
return nil, err
}
return signature.helper.awaitTx(tx.Hash())
}
// Verify that a signed payload is valid
//
// signedPayload: the payload to verify
//
// returns: true if the payload is valid, otherwise false.
//
// Example
//
// // Learn more about how to craft a payload in the Generate() function
// signedPayload, err := contract.Signature.Generate(payload)
// isValid, err := contract.Signature.Verify(signedPayload)
func (signature *ERC721SignatureMinting) Verify(ctx context.Context, signedPayload *SignedPayload721) (bool, error) {
mintRequest := signedPayload.Payload
mintSignature := signedPayload.Signature
message, err := signature.mapPayloadToContractStruct(ctx, mintRequest)
if err != nil {
return false, err
}
verification, _, err := signature.abi.Verify(&bind.CallOpts{Context: ctx}, *message, mintSignature)
return verification, err
}
// Generate a new payload from the given data
//
// payloadToSign: the payload containing the data for the signature mint
//
// returns: the payload signed by the minter's private key
//
// Example
//
// payload := &web3sdkio.Signature721PayloadInput{
// To: "0x9e1b8A86fFEE4a7175DAE4bDB1cC12d111Dcb3D6", // address to mint to
// Price: 0, // cost of minting
// CurrencyAddress: "0x0000000000000000000000000000000000000000", // currency to pay in order to mint
// MintStartTime: 0, // time where minting is allowed to start (epoch seconds)
// MintEndTime: 100000000000000, // time when this signature expires (epoch seconds)
// PrimarySaleRecipient: "0x0000000000000000000000000000000000000000", // address to receive the primary sales of this mint
// Metadata: &web3sdkio.NFTMetadataInput{ // metadata of the NFT to mint
// Name: "ERC721 Sigmint!",
// },
// RoyaltyRecipient: "0x0000000000000000000000000000000000000000", // address to receive royalties of this mint
// RoyaltyBps: 0, // royalty cut of this mint in basis points
// }
//
// signedPayload, err := contract.Signature.Generate(payload)
func (signature *ERC721SignatureMinting) Generate(ctx context.Context, payloadToSign *Signature721PayloadInput) (*SignedPayload721, error) {
payload, err := signature.GenerateBatch(ctx, []*Signature721PayloadInput{payloadToSign})
if err != nil {
return nil, err
}
return payload[0], nil
}
// Generate a batch of new payload from the given data
//
// payloadToSign: the payloads containing the data for the signature mint
//
// returns: the payloads signed by the minter's private key
//
// Example
//
// payload := []*web3sdkio.Signature721PayloadInput{
// &web3sdkio.Signature721PayloadInput{
// To: "0x9e1b8A86fFEE4a7175DAE4bDB1cC12d111Dcb3D6",
// Price: 0,
// CurrencyAddress: "0x0000000000000000000000000000000000000000",
// MintStartTime: 0,
// MintEndTime: 100000000000000,
// PrimarySaleRecipient: "0x0000000000000000000000000000000000000000",
// Metadata: &web3sdkio.NFTMetadataInput{
// Name: "ERC721 Sigmint!",
// Image: imageFile,
// },
// RoyaltyRecipient: "0x0000000000000000000000000000000000000000",
// RoyaltyBps: 0,
// },
// &web3sdkio.Signature721PayloadInput{
// To: "0x9e1b8A86fFEE4a7175DAE4bDB1cC12d111Dcb3D6",
// Price: 0,
// CurrencyAddress: "0x0000000000000000000000000000000000000000",
// MintStartTime: 0,
// MintEndTime: 100000000000000,
// PrimarySaleRecipient: "0x0000000000000000000000000000000000000000",
// Metadata: &web3sdkio.NFTMetadataInput{
// Name: "ERC721 Sigmint!",
// Image: imageFile,
// },
// RoyaltyRecipient: "0x0000000000000000000000000000000000000000",
// RoyaltyBps: 0,
// },
// }
//
// signedPayload, err := contract.Signature.GenerateBatch(context.Background(), payload)
func (signature *ERC721SignatureMinting) GenerateBatch(ctx context.Context, payloadsToSign []*Signature721PayloadInput) ([]*SignedPayload721, error) {
// TODO: Verify roles and return error
metadatas := []*NFTMetadataInput{}
for _, payload := range payloadsToSign {
metadatas = append(metadatas, payload.Metadata)
}
uris, err := uploadOrExtractUris(metadatas, signature.storage)
if err != nil {
return nil, err
}
chainId, err := signature.helper.GetChainID()
if err != nil {
return nil, err
}
signedPayloads := []*SignedPayload721{}
for i, uri := range uris {
p := payloadsToSign[i]
generatedId := uuid.New()
id := [32]byte{}
for i := 0; i < 16; i++ {
id[16+i] = generatedId[i]
}
payload := &Signature721PayloadOutput{
To: p.To,
Price: p.Price,
CurrencyAddress: p.CurrencyAddress,
MintStartTime: p.MintStartTime,
MintEndTime: p.MintEndTime,
PrimarySaleRecipient: p.PrimarySaleRecipient,
Metadata: p.Metadata,
RoyaltyRecipient: p.RoyaltyRecipient,
RoyaltyBps: p.RoyaltyBps,
Uri: uri,
Uid: id,
}
mappedPayload, err := signature.generateMessage(ctx, payload)
if err != nil {
return nil, err
}
typedData := signerTypes.TypedData{
Types: signerTypes.Types{
"MintRequest": []signerTypes.Type{
{Name: "to", Type: "address"},
{Name: "royaltyRecipient", Type: "address"},
{Name: "royaltyBps", Type: "uint256"},
{Name: "primarySaleRecipient", Type: "address"},
{Name: "uri", Type: "string"},
{Name: "price", Type: "uint256"},
{Name: "currency", Type: "address"},
{Name: "validityStartTimestamp", Type: "uint128"},
{Name: "validityEndTimestamp", Type: "uint128"},
{Name: "uid", Type: "bytes32"},
},
"EIP712Domain": []signerTypes.Type{
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint256"},
{Name: "verifyingContract", Type: "address"},
},
},
PrimaryType: "MintRequest",
Domain: signerTypes.TypedDataDomain{
Name: "TokenERC721",
Version: "1",
ChainId: math.NewHexOrDecimal256(chainId.Int64()),
VerifyingContract: signature.helper.getAddress().String(),
},
Message: mappedPayload,
}
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
if err != nil {
return nil, err
}
typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
if err != nil {
return nil, err
}
rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
sigHash := crypto.Keccak256(rawData)
privateKey := signature.helper.GetPrivateKey()
signatureHash, err := crypto.Sign(sigHash, privateKey)
if err != nil {
return nil, err
}
// We need this to correct v = 0,1 to v = 27,28 - or else all will break
if signatureHash[64] == 0 || signatureHash[64] == 1 {
signatureHash[64] += 27
}
signedPayloads = append(signedPayloads, &SignedPayload721{
Payload: payload,
Signature: signatureHash,
})
}
return signedPayloads, nil
}
func (signature *ERC721SignatureMinting) generateMessage(ctx context.Context, mintRequest *Signature721PayloadOutput) (signerTypes.TypedDataMessage, error) {
provider := signature.helper.GetProvider()
price, err := normalizePriceValue(ctx, provider, mintRequest.Price, mintRequest.CurrencyAddress)
if err != nil {
return nil, err
}
message := signerTypes.TypedDataMessage{
"to": mintRequest.To,
"royaltyRecipient": mintRequest.RoyaltyRecipient,
"royaltyBps": fmt.Sprintf("%v", mintRequest.RoyaltyBps),
"primarySaleRecipient": mintRequest.PrimarySaleRecipient,
"uri": mintRequest.Uri,
"price": fmt.Sprintf("%v", int(price.Int64())),
"currency": mintRequest.CurrencyAddress,
"validityStartTimestamp": fmt.Sprintf("%v", mintRequest.MintStartTime),
"validityEndTimestamp": fmt.Sprintf("%v", mintRequest.MintEndTime),
"uid": mintRequest.Uid[:],
}
return message, nil
}
func (signature *ERC721SignatureMinting) mapPayloadToContractStruct(ctx context.Context, mintRequest *Signature721PayloadOutput) (*abi.ITokenERC721MintRequest, error) {
provider := signature.helper.GetProvider()
price, err := normalizePriceValue(ctx, provider, mintRequest.Price, mintRequest.CurrencyAddress)
if err != nil {
return nil, err
}
return &abi.ITokenERC721MintRequest{
To: common.HexToAddress(mintRequest.To),
RoyaltyRecipient: common.HexToAddress(mintRequest.RoyaltyRecipient),
RoyaltyBps: big.NewInt(int64(mintRequest.RoyaltyBps)),
PrimarySaleRecipient: common.HexToAddress(mintRequest.PrimarySaleRecipient),
Uri: mintRequest.Uri,
Price: price,
Currency: common.HexToAddress(mintRequest.CurrencyAddress),
ValidityStartTimestamp: big.NewInt(int64(mintRequest.MintStartTime)),
ValidityEndTimestamp: big.NewInt(int64(mintRequest.MintEndTime)),
Uid: mintRequest.Uid,
}, nil
}