-
-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * polish * format * exports * chore: changeset * fix: import * tests: fix * pr review * pr review
- Loading branch information
Showing
21 changed files
with
914 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"viem": patch | ||
--- | ||
|
||
Added support for EIP-3668 CCIP Read. |
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,71 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity ^0.8.13; | ||
|
||
contract OffchainLookupExample { | ||
error OffchainLookup( | ||
address sender, | ||
string[] urls, | ||
bytes callData, | ||
bytes4 callbackFunction, | ||
bytes extraData | ||
); | ||
|
||
string[] public urls; | ||
|
||
constructor(string[] memory urls_) { | ||
urls = urls_; | ||
} | ||
|
||
function getAddress(string calldata name) public view returns (address) { | ||
revert OffchainLookup( | ||
address(this), | ||
urls, | ||
abi.encode(name), | ||
OffchainLookupExample.getAddressWithProof.selector, | ||
abi.encode(name) | ||
); | ||
} | ||
|
||
function getAddressWithProof( | ||
bytes calldata result, | ||
bytes calldata extraData | ||
) external view returns (address) { | ||
(address owner, bytes32 hash, bytes memory sig) = abi.decode(result, (address, bytes32, bytes)); | ||
address signer = recoverSigner(hash, sig); | ||
require(signer == owner, "invalid signature"); | ||
return signer; | ||
} | ||
|
||
function recoverSigner( | ||
bytes32 _ethSignedMessageHash, | ||
bytes memory _signature | ||
) public pure returns (address) { | ||
(bytes32 r, bytes32 s, uint8 v) = splitSignature(_signature); | ||
|
||
return ecrecover(_ethSignedMessageHash, v, r, s); | ||
} | ||
|
||
function splitSignature( | ||
bytes memory sig | ||
) public pure returns (bytes32 r, bytes32 s, uint8 v) { | ||
require(sig.length == 65, "invalid signature length"); | ||
|
||
assembly { | ||
/* | ||
First 32 bytes stores the length of the signature | ||
add(sig, 32) = pointer of sig + 32 | ||
effectively, skips first 32 bytes of signature | ||
mload(p) loads next 32 bytes starting at the memory address p into memory | ||
*/ | ||
|
||
// first 32 bytes, after the length prefix | ||
r := mload(add(sig, 32)) | ||
// second 32 bytes | ||
s := mload(add(sig, 64)) | ||
// final byte (first byte of the next 32 bytes) | ||
v := byte(0, mload(add(sig, 96))) | ||
} | ||
} | ||
} |
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,39 @@ | ||
import { | ||
decodeAbiParameters, | ||
encodeAbiParameters, | ||
parseAbiParameters, | ||
} from '../utils/abi/index.js' | ||
import { sign } from '../accounts/utils/sign.js' | ||
import { signatureToHex } from '../accounts/utils/signatureToHex.js' | ||
import { stringToHex } from '../utils/encoding/index.js' | ||
import { keccak256 } from '../utils/hash/index.js' | ||
import { accounts } from './constants.js' | ||
import { createHttpServer } from './utils.js' | ||
import type { Hex } from '../types/index.js' | ||
|
||
export function createCcipServer() { | ||
return createHttpServer(async (req, res) => { | ||
res.writeHead(200, { | ||
'Content-Type': 'application/json', | ||
}) | ||
const signature = signatureToHex( | ||
await sign({ | ||
hash: keccak256(stringToHex('jxom.viem')), | ||
privateKey: accounts[0].privateKey, | ||
}), | ||
) | ||
|
||
const data = req.url?.split('/')[2]! as Hex | ||
const [name] = decodeAbiParameters(parseAbiParameters('string'), data) | ||
|
||
res.end( | ||
JSON.stringify({ | ||
data: encodeAbiParameters(parseAbiParameters('address,bytes32,bytes'), [ | ||
accounts[0].address, | ||
keccak256(stringToHex(name)), | ||
signature, | ||
]), | ||
}), | ||
) | ||
}) | ||
} |
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
Oops, something went wrong.
41bc9e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
viem – ./
viem.vercel.app
www.viem.sh
viem-site.vercel.app
viem-wagmi-dev.vercel.app
viem-git-main-wagmi-dev.vercel.app
viem.sh