-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove nameprep * encode generic response instread of typed userProfile * decodeCalldata based on sig * handle request based on sig * add decode addr * resolve the address of a given name * clean up ccip Gateway * reame decodeCalldata to decodeRequest * change port * switch port
- Loading branch information
Showing
23 changed files
with
447 additions
and
277 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/lib/src/offchainResolver/encoding/decode/decodeAddr.ts
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,21 @@ | ||
import { ethers } from 'ethers'; | ||
import { decodeDnsName } from '../../dns/decodeDnsName'; | ||
|
||
/** | ||
Decodes the call data of addr(bytes 32) | ||
@param ensName - The ENS name to be decoded. | ||
@param data - The data containing the namehash. | ||
@returns An object containing the name. | ||
@throws An error if the namehash doesn't match the ENS name. | ||
*/ | ||
export function decodeAddr(ensName: string, data: ethers.utils.Result) { | ||
const [nameHash] = data; | ||
|
||
const name = decodeDnsName(ensName); | ||
|
||
if (ethers.utils.namehash(name) !== nameHash) { | ||
throw Error("Namehash doesn't match"); | ||
} | ||
|
||
return { name }; | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/lib/src/offchainResolver/encoding/decode/decodeText.ts
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,21 @@ | ||
import { ethers } from 'ethers'; | ||
import { decodeDnsName } from '../../dns/decodeDnsName'; | ||
|
||
/** | ||
Decodes the text record of a given ENS name and returns an object containing the name and the record. | ||
@param ensName - The ENS name to be decoded. | ||
@param data - The data containing the namehash and the record. | ||
@returns An object containing the name and the record. | ||
@throws An error if the namehash doesn't match the ENS name. | ||
*/ | ||
export function decodeText(ensName: string, data: ethers.utils.Result) { | ||
const [nameHash, record] = data; | ||
|
||
const name = decodeDnsName(ensName); | ||
|
||
if (ethers.utils.namehash(name) !== nameHash) { | ||
throw Error("Namehash doesn't match"); | ||
} | ||
|
||
return { name, record }; | ||
} |
39 changes: 0 additions & 39 deletions
39
packages/lib/src/offchainResolver/encoding/decodeCalldata.test.ts
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
packages/lib/src/offchainResolver/encoding/decodeCalldata.ts
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
packages/lib/src/offchainResolver/encoding/decodeRequest.test.ts
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,68 @@ | ||
import { ethers } from 'ethers'; | ||
import { decodeRequest } from './decodeRequest'; | ||
import { getResolverInterface } from './getResolverInterface'; | ||
import { encodeEnsName } from '../dns/encodeEnsName'; | ||
|
||
describe('decodeRequest', () => { | ||
describe('decodeText', () => { | ||
it('decodes valid calldata', () => { | ||
const textData = getResolverInterface().encodeFunctionData('text', [ | ||
ethers.utils.namehash('foo.dm3.eth'), | ||
'dm3.profile', | ||
]); | ||
|
||
const calldata = getResolverInterface().encodeFunctionData( | ||
'resolve', | ||
[encodeEnsName('foo.dm3.eth'), textData], | ||
); | ||
const { request } = decodeRequest(calldata); | ||
|
||
expect(request.record).toBe('dm3.profile'); | ||
expect(request.name).toBe('foo.dm3.eth'); | ||
}); | ||
|
||
it('throws if namehash does not matched encoded ens.name', () => { | ||
const textData = getResolverInterface().encodeFunctionData('text', [ | ||
ethers.utils.namehash('FOOO'), | ||
'dm3.profile', | ||
]); | ||
|
||
const calldata = getResolverInterface().encodeFunctionData( | ||
'resolve', | ||
[encodeEnsName('foo.dm3.eth'), textData], | ||
); | ||
|
||
expect(() => decodeRequest(calldata)).toThrowError( | ||
"Namehash doesn't match", | ||
); | ||
}); | ||
}); | ||
describe('decodeAddr', () => { | ||
it('decodes valid calldata', () => { | ||
const textData = getResolverInterface().encodeFunctionData('addr', [ | ||
ethers.utils.namehash('foo.dm3.eth'), | ||
]); | ||
|
||
const calldata = getResolverInterface().encodeFunctionData( | ||
'resolve', | ||
[encodeEnsName('foo.dm3.eth'), textData], | ||
); | ||
const { request } = decodeRequest(calldata); | ||
expect(request.name).toBe('foo.dm3.eth'); | ||
}); | ||
it('throws if namehash does not matched encoded ens.name', () => { | ||
const textData = getResolverInterface().encodeFunctionData('addr', [ | ||
ethers.utils.namehash('FOOO'), | ||
]); | ||
|
||
const calldata = getResolverInterface().encodeFunctionData( | ||
'resolve', | ||
[encodeEnsName('foo.dm3.eth'), textData], | ||
); | ||
|
||
expect(() => decodeRequest(calldata)).toThrowError( | ||
"Namehash doesn't match", | ||
); | ||
}); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/lib/src/offchainResolver/encoding/decodeRequest.ts
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 { log } from '../../shared/log'; | ||
import { DecodedCcipRequest } from '../types'; | ||
import { decodeAddr } from './decode/decodeAddr'; | ||
import { decodeText } from './decode/decodeText'; | ||
import { getResolverInterface } from './getResolverInterface'; | ||
|
||
/** | ||
Decodes a given calldata string and returns a DecodedCcipRequest object containing the signature and request. | ||
@param calldata - The calldata string to be decoded. | ||
@returns A {@see DecodedCcipRequest} object containing the signature and request. | ||
@throws An error if the calldata cannot be decoded or if the signature is not supported. | ||
*/ | ||
export function decodeRequest(calldata: string): DecodedCcipRequest { | ||
try { | ||
const textResolver = getResolverInterface(); | ||
|
||
//Parse the calldata returned by a contra | ||
const [ensName, data] = textResolver.parseTransaction({ | ||
data: calldata, | ||
}).args; | ||
|
||
const { signature, args } = textResolver.parseTransaction({ | ||
data, | ||
}); | ||
|
||
switch (signature) { | ||
case 'text(bytes32,string)': | ||
return { signature, request: decodeText(ensName, args) }; | ||
case 'addr(bytes32)': | ||
return { signature, request: decodeAddr(ensName, args) }; | ||
default: | ||
throw Error(`${signature} is not supported`); | ||
} | ||
} catch (err: any) { | ||
log("[Decode Calldata] Can't decode calldata "); | ||
log(err); | ||
throw err; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
/** | ||
* @param name the ENS name after after decoding | ||
* @param record the name of the record that should be queried | ||
* @param request the decoded function params of the request. i.E {name:string,record:string} | ||
* @param signature the signature of the function the request should query | ||
*/ | ||
export interface DecodedCcipRequest { | ||
name: string; | ||
record: string; | ||
request: any; | ||
signature: string; | ||
} |
Oops, something went wrong.