Copy const { Signer, DataHexString, utils, Wallet } = require("ethers");
import { BytesLike } from '@ethersproject/bytes';
export const signAttributes = async (
accountAddress: string,
issuer: typeof Signer,
attrKeys: BytesLike[],
attrValues: BytesLike[],
verifiedAt: number,
issuedAt: number,
fee: any,
passportAddress: string,
chainId: number,
did: BytesLike = utils.constants.HashZero,
): Promise<typeof DataHexString> => {
const attrKeys: string[] = [];
const attrValues: string[] = [];
const hash = utils.keccak256(
utils.defaultAbiCoder.encode(
[
"address",
"bytes32[]",
"bytes32[]",
"bytes32",
"uint256",
"uint256",
"uint256",
"uint256",
"address",
],
[
accountAddress,
attrKeys,
attrValues,
did,
verifiedAt,
issuedAt,
fee,
chainId,
passportAddress,
]
)
);
const sig = await issuer.signMessage(utils.arrayify(hash));
return sig;
};
const accountAddress = "0x......"; // Wallet address of the user
const issuer = new Wallet(ISSUER_PRIVATE_KEY); // Loading issuer account from their private key
const attrTypes = [utils.id("COUNTRY"), utils.id("AML")]; // List of attributes types
const attrKeys = computeAttrKeys(accountAddress, attrTypes); // List of attributes Identifiers
const attrValues = [utils.id("US"), utils.hexZeroPad("0x01", 32)]; // List of attributes values
const verifiedAt = Math.floor(new Date().getTime() / 1000) - 60; // we substract 60 seconds to avoid errors during minting passport with long block time
const issuedAt = Math.floor(new Date().getTime() / 1000) - 60; // we substract 60 seconds to avoid errors during minting passport with long block time
const passportAddress = "0x2e779749c40CC4Ba1cAB4c57eF84d90755CC017d";
const chainId = 1; // 1 for Ethereum Mainnet
const fee = utils.parseEther("0.001"); // Fee paid by the user to the issuer in then native blokcchain currency (ex: ETH for Ethereum, MATIC for Polygon)
const signature = await signAttributes(
accountAddress,
issuer,
attrKeys,
attrValues,
verifiedAt,
issuedAt,
fee,
passportAddress,
chainId
);