-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e76df27
commit 1763f1e
Showing
6 changed files
with
266 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { keccak256 } from 'ethereumjs-util' | ||
import encode from 'ethereumjs-abi' | ||
|
||
const PERMIT_TYPEHASH = keccak256('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)') // 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9 | ||
|
||
export function getStructHash({ owner, spender, value, nonce, deadline }) { | ||
return keccak256( | ||
encode( | ||
['bytes32', 'address', 'address', 'uint256', 'uint256', 'uint256'], | ||
[PERMIT_TYPEHASH, owner, spender, value, nonce, deadline] | ||
) | ||
) | ||
} | ||
|
||
export function getTypedDataHash(DOMAIN_SEPARATOR, permitData) { | ||
return keccak256(encode(['bytes', 'bytes32', 'bytes32'], ['\x19\x01', DOMAIN_SEPARATOR, getStructHash(permitData)])) | ||
} | ||
|
||
const cache = new Map() | ||
|
||
export async function getPermitDigest(owner, spender, value, token, deadline) { | ||
let { name, version } = cache.get(token.address) || {} | ||
if (!name || !version) { | ||
;[name, version] = await Promise.all([token.name(), token.version()]) | ||
cache.set(token.address, { name, version }) | ||
} | ||
return [ | ||
{ | ||
name: 'POL', | ||
version: '1.1.0', | ||
chainId: (await token.provider._network).chainId, | ||
verifyingContract: token.address | ||
}, | ||
{ | ||
Permit: [ | ||
{ | ||
name: 'owner', | ||
type: 'address' | ||
}, | ||
{ | ||
name: 'spender', | ||
type: 'address' | ||
}, | ||
{ | ||
name: 'value', | ||
type: 'uint256' | ||
}, | ||
{ | ||
name: 'nonce', | ||
type: 'uint256' | ||
}, | ||
{ | ||
name: 'deadline', | ||
type: 'uint256' | ||
} | ||
] | ||
}, | ||
{ | ||
owner, | ||
spender, | ||
value, | ||
nonce: await token.nonces(owner), | ||
deadline | ||
} | ||
] | ||
} |