Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
//data = data2;
// eth_util.toBuffer
//var msg_hash = eth_sig_utils.typedSignatureHash(data.message);
//console.log("MSGHASH", msg_hash);
var msg_hash = utils.hashStruct(data.primaryType, data.message, data.types);
console.log('MSG_HASH = ', eth_util.bufferToHex(msg_hash));
var msg_sig = eth_sig_utils.signTypedData(key, {data: data})
console.log("Ok, signed typed data using " + account.address)
console.log("SIGNATURE = " + msg_sig);
var signer = eth_sig_utils.recoverTypedSignature({data: data, sig: msg_sig});
signer = w3_utils.toChecksumAddress(signer);
if (signer === account.address) {
console.log("Ok, verified signature was signed by " + signer);
} else {
console.log("ERROR: signature verification failed");
}
// const typedData = {
// types: {
// EIP712Domain: [
// { name: 'name', type: 'string' },
// { name: 'version', type: 'string' },
// { name: 'chainId', type: 'uint256' },
// { name: 'verifyingContract', type: 'address' },
// ],
'amount': new BigNumber('35000000000000000000'),
'balance': 2000,
},
}
//console.log(data);
//var key = eth_util.toBuffer(buyer_key_bytes);
var key = eth_util.toBuffer(buyer_key);
// eth_util.toBuffer
var sig = eth_sig_utils.signTypedData(key, {data: data})
console.log("Ok, signed typed data using " + account.address)
console.log("SIGNATURE = " + sig);
var signer = eth_sig_utils.recoverTypedSignature({data, sig});
signer = w3_utils.toChecksumAddress(signer);
if (signer === account.address) {
console.log("Ok, verified signature was signed by " + signer);
} else {
console.log("ERROR: signature verification failed");
}
async verifyListingSignature(listing, signer) {
// grab the raw ipfs hash
const ipfs_response = await this.ipfsService.loadFile(listing.ipfs.hash)
const ipfs_data = await ipfs_response.json()
const signature = ipfs_data.signature
delete ipfs_data.signature
listing.raw_ipfs_hash = this.contractService.web3.utils.sha3(stringify(ipfs_data))
const signData = await this.contractService.getSignListingData(listing)
const recoveredAddress = recoverTypedSignature({ data: signData, sig: signature })
delete listing.raw_ipfs_hash
if (recoveredAddress == signer.toLowerCase()) {
return true
}
console.log('Signature verification failed:', signData, ' recovered address:', recoveredAddress, ' signer:', signer)
}
it('returns the expected value', async () => {
const typedData = {
types: {
EIP712Domain: []
},
domain: {},
primaryType: 'EIP712Domain',
message: {}
}
await keyring.deserialize([privKeyHex])
const sig = await keyring.signTypedData_v4(address, typedData)
const restored = sigUtil.recoverTypedSignature({ data: typedData, sig: sig })
assert.equal(restored, address, 'recovered address')
})
})
it('returns the expected value', async () => {
const typedData = {"data":{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"}]},"primaryType":"Mail","domain":{"name":"Ether Mail","version":"1","chainId":1,"verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"},"message":{"from":{"name":"Cow","wallet":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"},"to":{"name":"Bob","wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"},"contents":"Hello, Bob!"}}}
await keyring.deserialize([privKeyHex])
const addresses = await keyring.getAccounts()
const address = addresses[0]
const sig = await keyring.signTypedData_v3(address, typedData.data)
assert.equal(sig, expectedSig, 'verified signature')
const signedData = Object.create(typedData)
signedData.sig = sig
const restored = sigUtil.recoverTypedSignature(signedData)
assert.equal(restored, address, 'recovered address')
})
})
types: {
EIP712Domain: []
},
domain: {},
primaryType: 'EIP712Domain',
message: {}
}
await keyring.deserialize({
mnemonic: sampleMnemonic,
numberOfAccounts: 1,
})
const addresses = await keyring.getAccounts()
const address = addresses[0]
const sig = await keyring.signTypedData_v3(address, typedData)
const restored = sigUtil.recoverTypedSignature({ data: typedData, sig: sig })
assert.equal(restored, address, 'recovered address')
})
})
const extractTypeDataSignee = (header: string, body: any) => {
const decodedSignature = Base64.decode(header)
try {
return sigUtil.recoverTypedSignature({
data: body,
sig: decodedSignature,
})
} catch {
return null
}
}
function recover_eip712_signer (channel_adr, channel_seq, balance, is_final, signature) {
const msg = _create_eip712_data(verifying_adr, channel_adr, channel_seq, balance, is_final);
const signer = eth_sig_utils.recoverTypedSignature({msg, signature});
return w3_utils.toChecksumAddress(signer);
}
confirmTypedDataSignMessage(message, signature) {
const typedData = [{
type: 'string',
name: 'banner',
value: this.options.banner
}, {
type: 'string',
name: 'message',
value: this.options.prefix + message,
}]
const recoveredAddress = sigUtil.recoverTypedSignature({
data: typedData,
sig: signature
})
const storedMessage = cache.get(recoveredAddress.toLowerCase())
if (storedMessage === message) {
cache.del(recoveredAddress.toLowerCase())
return recoveredAddress
}
return false
}
}