Skip to content

Commit

Permalink
fix: 721 permit from signTyped vs static
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Feb 25, 2024
1 parent 8693e52 commit 6a57f38
Showing 1 changed file with 49 additions and 61 deletions.
110 changes: 49 additions & 61 deletions scripts/typescript/generate-erc721-permit-url.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,75 @@
import { MaxUint256, PermitTransferFrom } from "@uniswap/permit2-sdk";
import * as dotenv from "dotenv";
import { ethers } from "ethers";
import { ethers } from "ethers";
import { log } from "./utils";
import { solidityKeccak256 } from "ethers/lib/utils";
dotenv.config();

const NFT_REWARDS_ANVIL_DEPLOYMENT = "0x38A70c040CA5F5439ad52d0e821063b0EC0B52b6"; // Address when using Anvil acc 1 on forked Gnosis
const ANVIL_ACC_1_PRIVATE_KEY = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";
// const NFT_ADDRESS = "0xAa1bfC0e51969415d64d6dE74f27CDa0587e645b"; // Real Gnosis address

/**
* hardcoded digest signature for the below txData721.request object
* anvil --chain-id 31337 --fork-url https//...
* nft-rewards repo: forge script ./script/Deploy001_NftReward.s.sol --rpc-url http://127.0.0.1:8545 --broadcast
*
* string[] memory values = new string[](5);
* values[0] = "ubiquity";
* values[1] = "pay.ubq.fi";
* values[2] = "1";
* values[3] = "testing";
* values[4] = "issue";
*
* bytes32[] memory keys = new bytes32[](5);
* keys[0] = 0x1c474488c03c83ad98714cfe3a60c752036f92ab8378227adfcb13585f115c5c;
* keys[1] = 0x0480f34997cc2d3abc2dafd652b25652ba845458fc3e3692f28acbd4920a37ea;
* keys[2] = 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6;
* keys[3] = 0x5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02;
* keys[4] = 0xa33d0fabbfddf3db8e1458550edc0ab7e061990c85809d25341eab0885973d7d;
*
* MintRequest memory _mintRequest = MintRequest({
* beneficiary: ANVIL_ACC_1, // (0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266),
* deadline: type(uint256).max,
* keys: keys,
* nonce: 31337,
* values: values
* });
*
* `Signed not by minter` error thrown trying to generate it on the fly
* SignatureTransfer.getPermitData() expects the permitTransferFrom details, not the request object
* so myWallet._signTypedData(domain, types, values) signs the permitTransferFrom details
*
* whereas the nftRewards contract recovers against the signed request digest
* which is typedDataHashed within the contract, so we are only signing a hash
* which from a claim portal standpoint shouldn't need to change the signature
* admittedly, I'm not sure how to generate it on the fly so hardcoded for now
*/
const NFT_REWARDS_ANVIL_DEPLOYMENT = "0x38a70c040ca5f5439ad52d0e821063b0ec0b52b6";
const ANVIL_ACC_2_PRIVATE_KEY = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";
const ANVIL_ACC_1_ADDRESS = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
const NFT_ADDRESS = "0xAa1bfC0e51969415d64d6dE74f27CDa0587e645b";

export async function generateERC721Permit() {
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_PROVIDER_URL);
const myWallet = new ethers.Wallet(ANVIL_ACC_1_PRIVATE_KEY, provider);
const myWallet = new ethers.Wallet(ANVIL_ACC_2_PRIVATE_KEY, provider);

const CHAIN_ID = Number(process.env.CHAIN_ID);
const network = CHAIN_ID === 1 ? "mainnet" : CHAIN_ID === 100 ? "gnosis" : CHAIN_ID === 31337 ? "localhost" : "unknown";

const SIGNING_DOMAIN_NAME = "NftReward-Domain";
const SIGNING_DOMAIN_VERSION = "1";
const VERIFYING_CONTRACT_ADDRESS = network == "localhost" ? NFT_REWARDS_ANVIL_DEPLOYMENT : NFT_ADDRESS;
const GITHUB_CONTRIBUTION_TYPE = "issue";
const GITHUB_ISSUE_ID = "1";
const GITHUB_ORGANIZATION_NAME = "ubiquity";
const GITHUB_REPOSITORY_NAME = "pay.ubq.fi";
const GITHUB_USERNAME = "testing";
const ANVIL_ACC_1 = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";

const erc721TransferFromData: PermitTransferFrom = {
permitted: {
token: NFT_REWARDS_ANVIL_DEPLOYMENT, // anvil instance via nft-rewards repo
amount: 1,
token: network == "localhost" ? NFT_REWARDS_ANVIL_DEPLOYMENT : NFT_ADDRESS,
amount: 1,
},
spender: ANVIL_ACC_1, // anvil acc 1
nonce: 31337,
spender: network == "localhost" ? NFT_REWARDS_ANVIL_DEPLOYMENT : NFT_ADDRESS,
nonce: 3133748,
deadline: MaxUint256,
};

const domain = {
name: SIGNING_DOMAIN_NAME,
version: SIGNING_DOMAIN_VERSION,
verifyingContract: VERIFYING_CONTRACT_ADDRESS,
chainId: CHAIN_ID,
};

const types = {
MintRequest: [
{ name: "beneficiary", type: "address" },
{ name: "deadline", type: "uint256" },
{ name: "keys", type: "bytes32[]" },
{ name: "nonce", type: "uint256" },
{ name: "values", type: "string[]" },
],
};

const valueBytes = [
solidityKeccak256(["string"], [GITHUB_ORGANIZATION_NAME]),
solidityKeccak256(["string"], [GITHUB_REPOSITORY_NAME]),
solidityKeccak256(["string"], [GITHUB_ISSUE_ID]),
solidityKeccak256(["string"], [GITHUB_USERNAME]),
solidityKeccak256(["string"], [GITHUB_CONTRIBUTION_TYPE])
]
solidityKeccak256(["string"], [GITHUB_CONTRIBUTION_TYPE]),
];

const mintRequest = {
beneficiary: network == "localhost" ? ANVIL_ACC_1_ADDRESS : myWallet.address,
deadline: MaxUint256,
keys: valueBytes,
nonce: 3133748,
values: [GITHUB_ORGANIZATION_NAME, GITHUB_REPOSITORY_NAME, GITHUB_ISSUE_ID, GITHUB_USERNAME, GITHUB_CONTRIBUTION_TYPE],
};

const sig = "0x900f4c98c1829255903fbf57f46d7b2f9ad4e79f0abbd58b9921136319e7001d173e26d3560fe5dc674782d78f670f9c18d2b7356ced073df7410678f0981c411c";
const signature = await myWallet._signTypedData(domain, types, mintRequest);

const txData721 = [
{
Expand All @@ -93,27 +87,21 @@ export async function generateERC721Permit() {
requestedAmount: erc721TransferFromData.permitted.amount.toString(),
},
owner: myWallet.address,
signature: sig,
networkId: Number(process.env.CHAIN_ID), // fork gnosis with --chain-id 31337
signature: signature,
networkId: CHAIN_ID,
nftMetadata: {
GITHUB_ORGANIZATION_NAME,
GITHUB_REPOSITORY_NAME,
GITHUB_ISSUE_ID,
GITHUB_USERNAME,
GITHUB_CONTRIBUTION_TYPE
GITHUB_CONTRIBUTION_TYPE,
},
request: {
beneficiary: ANVIL_ACC_1,
beneficiary: ANVIL_ACC_1_ADDRESS,
deadline: erc721TransferFromData.deadline.toString(),
keys: valueBytes,
nonce: erc721TransferFromData.nonce.toString(),
values: [
GITHUB_ORGANIZATION_NAME,
GITHUB_REPOSITORY_NAME,
GITHUB_ISSUE_ID,
GITHUB_USERNAME,
GITHUB_CONTRIBUTION_TYPE
]
values: [GITHUB_ORGANIZATION_NAME, GITHUB_REPOSITORY_NAME, GITHUB_ISSUE_ID, GITHUB_USERNAME, GITHUB_CONTRIBUTION_TYPE],
},
},
];
Expand Down

0 comments on commit 6a57f38

Please sign in to comment.