UNPKG

@f5i23q999d/cow-sdk

Version:

<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>

95 lines 5.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CowShedHooks = exports.COW_SHED_712_TYPES = exports.COW_SHED_PROXY_INIT_CODE = void 0; const contracts_1 = require("@cowprotocol/contracts"); const utils_1 = require("ethers/lib/utils"); const common_1 = require("../../common/index.js"); const utils_2 = require("./utils.js"); exports.COW_SHED_PROXY_INIT_CODE = '0x60a034608e57601f61037138819003918201601f19168301916001600160401b038311848410176093578084926040948552833981010312608e57604b602060458360a9565b920160a9565b6080527f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc556040516102b490816100bd8239608051818181608f01526101720152f35b600080fd5b634e487b7160e01b600052604160045260246000fd5b51906001600160a01b0382168203608e5756fe60806040526004361015610018575b3661019457610194565b6000803560e01c908163025b22bc1461003b575063f851a4400361000e5761010d565b3461010a5760207ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261010a5773ffffffffffffffffffffffffffffffffffffffff60043581811691828203610106577f0000000000000000000000000000000000000000000000000000000000000000163314600014610101577f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b8280a280f35b61023d565b8380fd5b80fd5b346101645760007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc360112610164576020610146610169565b73ffffffffffffffffffffffffffffffffffffffff60405191168152f35b600080fd5b333003610101577f000000000000000000000000000000000000000000000000000000000000000090565b60ff7f68df44b1011761f481358c0f49a711192727fb02c377d697bcb0ea8ff8393ac0541615806101ef575b1561023d5760046040517ff92ee8a9000000000000000000000000000000000000000000000000000000008152fd5b507f400ada75000000000000000000000000000000000000000000000000000000007fffffffff000000000000000000000000000000000000000000000000000000006000351614156101c0565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546000808092368280378136915af43d82803e1561027a573d90f35b3d90fdfea2646970667358221220c7c26ff3040b96a28e96d6d27b743972943aeaef81cc821544c5fe1e24f9b17264736f6c63430008190033'; exports.COW_SHED_712_TYPES = { ExecuteHooks: [ { type: 'Call[]', name: 'calls' }, { type: 'bytes32', name: 'nonce' }, { type: 'uint256', name: 'deadline' }, ], Call: [ { type: 'address', name: 'target' }, { type: 'uint256', name: 'value' }, { type: 'bytes', name: 'callData' }, { type: 'bool', name: 'allowFailure' }, { type: 'bool', name: 'isDelegateCall' }, ], }; class CowShedHooks { chainId; customOptions; constructor(chainId, customOptions) { this.chainId = chainId; this.customOptions = customOptions; } proxyOf(user) { const salt = utils_1.defaultAbiCoder.encode(['address'], [user]); const initCodeHash = (0, utils_1.solidityKeccak256)(['bytes', 'bytes'], [ this.proxyCreationCode(), utils_1.defaultAbiCoder.encode(['address', 'address'], [this.getImplementationAddress(), user]), ]); return (0, utils_1.getCreate2Address)(this.getFactoryAddress(), salt, initCodeHash); } encodeExecuteHooksForFactory(calls, nonce, deadline, user, signature) { return (0, utils_2.getCoWShedFactoryInterface)().encodeFunctionData('executeHooks', [calls, nonce, deadline, user, signature]); } async signCalls(calls, nonce, deadline, signer, signingScheme) { const user = await signer.getAddress(); const proxy = this.proxyOf(user); const { domain, types, message } = this.infoToSign(calls, nonce, deadline, proxy); return await ecdsaSignTypedData(signingScheme, signer, domain, types, message); } infoToSign(calls, nonce, deadline, proxy) { const message = { calls, nonce, deadline, }; return { domain: this.getDomain(proxy), types: exports.COW_SHED_712_TYPES, message }; } getDomain(proxy) { return { name: 'COWShed', version: '1.0.0', chainId: this.chainId, verifyingContract: proxy, }; } proxyCreationCode() { return this.customOptions?.proxyCreationCode ?? exports.COW_SHED_PROXY_INIT_CODE; } getFactoryAddress() { return this.customOptions?.factoryAddress ?? common_1.COW_SHED_FACTORY; } getImplementationAddress() { return this.customOptions?.implementationAddress ?? common_1.COW_SHED_IMPLEMENTATION; } } exports.CowShedHooks = CowShedHooks; // code copied from @cow/contract (not exported there) async function ecdsaSignTypedData(scheme, owner, domain, types, data) { let signature = null; switch (scheme) { case contracts_1.SigningScheme.EIP712: if (!(0, contracts_1.isTypedDataSigner)(owner)) { throw new Error('signer does not support signing typed data'); } signature = await owner._signTypedData(domain, types, data); break; case contracts_1.SigningScheme.ETHSIGN: signature = await owner.signMessage((0, utils_1.arrayify)((0, contracts_1.hashTypedData)(domain, types, data))); break; default: throw new Error('invalid signing scheme'); } // Passing the signature through split/join to normalize the `v` byte. // Some wallets do not pad it with `27`, which causes a signature failure // `splitSignature` pads it if needed, and `joinSignature` simply puts it back together return (0, utils_1.joinSignature)((0, utils_1.splitSignature)(signature)); } //# sourceMappingURL=CoWShedHooks.js.map