Created
March 23, 2022 14:17
-
-
Save fforbeck/885a2caf81213cdd0ce6083e09c7fba9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as sigUtil from 'eth-sig-util'; | |
type CouponType = 'coupon-kyc'; | |
type PrimaryType = 'Message' | 'EIP712Domain'; | |
type CouponData = { | |
type: CouponType; | |
kycedMember: string; | |
}; | |
function getMessageDomainType(chainId, verifyingContract, actionId) { | |
return { | |
name: 'Snapshot Message', | |
version: '4', | |
chainId, | |
verifyingContract, | |
actionId, | |
}; | |
} | |
function getDomainType() { | |
return [ | |
{name: 'name', type: 'string'}, | |
{name: 'version', type: 'string'}, | |
{name: 'chainId', type: 'uint256'}, | |
{name: 'verifyingContract', type: 'address'}, | |
{name: 'actionId', type: 'address'}, | |
]; | |
} | |
function getKycDomainDefinition(verifyingContract, actionId, chainId) { | |
const domain = getMessageDomainType(chainId, verifyingContract, actionId); | |
const types = { | |
Message: [{name: 'kycedMember', type: 'address'}], | |
EIP712Domain: getDomainType(), | |
}; | |
return {domain, types}; | |
} | |
function SigUtilSigner(privateKeyStr) { | |
return function ( | |
m: CouponData, | |
verifyingContract: string, | |
actionId: string, | |
chainId: number | |
) { | |
const message: CouponData = m; | |
if (privateKeyStr.indexOf('0x') === 0) { | |
privateKeyStr = privateKeyStr.slice(2); | |
} | |
const {domain, types} = getKycDomainDefinition( | |
verifyingContract, | |
actionId, | |
chainId | |
); | |
return sigUtil.signTypedData_v4(Buffer.from(privateKeyStr, 'hex'), {data: { | |
domain, | |
message, | |
primaryType: 'Message' as PrimaryType, | |
types, | |
}}); | |
}; | |
} | |
function createKycSignature = async ({ | |
kycOnboardingAdapterAddress, | |
daoRegistryAddress, | |
memberAddress, | |
signerPrivateKey, | |
}: CreateCouponSignatureParams): string => { | |
try { | |
const couponData: CouponData = { | |
type: 'coupon-kyc' as CouponType, | |
kycedMember: memberAddress, | |
}; | |
const chainId: number = x;// set the ethereum chainId | |
// Set the signer private key | |
const signature = SigUtilSigner(signerPrivateKey)( | |
couponData, | |
daoRegistryAddress, | |
kycOnboardingAdapterAddress, | |
chainId | |
); | |
return signature; | |
} catch (error) { | |
console.error(error); | |
throw new Error(error.message); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment