-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): simplified EIP-7702 support
- Loading branch information
1 parent
78a163c
commit ae8bee2
Showing
17 changed files
with
185 additions
and
372 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
27 changes: 27 additions & 0 deletions
27
packages/thirdweb/src/transaction/actions/eip7702/authorization.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,27 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { TEST_WALLET_B } from "~test/addresses.js"; | ||
import { TEST_ACCOUNT_A } from "~test/test-wallets.js"; | ||
import { signAuthorization } from "./authorization.js"; | ||
|
||
describe("signAuthorization", () => { | ||
it("should sign an authorization", async () => { | ||
const authorization = await signAuthorization({ | ||
account: TEST_ACCOUNT_A, | ||
request: { | ||
address: TEST_WALLET_B, | ||
chainId: 911867, | ||
nonce: 0n, | ||
}, | ||
}); | ||
expect(authorization).toMatchInlineSnapshot(` | ||
{ | ||
"address": "0x0000000000000000000000000000000000000002", | ||
"chainId": 911867, | ||
"nonce": 0n, | ||
"r": 3720526934953059641417422884731844424204826752871127418111522219225437830766n, | ||
"s": 23451045058292828843243765241045958975073226494910356096978666517928790374894n, | ||
"yParity": 1, | ||
} | ||
`); | ||
}); | ||
}); |
48 changes: 36 additions & 12 deletions
48
packages/thirdweb/src/transaction/actions/eip7702/authorization.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 |
---|---|---|
@@ -1,34 +1,58 @@ | ||
import type * as ox__Authorization from "ox/Authorization"; | ||
import type { Address } from "../../../utils/address.js"; | ||
import type { Account } from "../../../wallets/interfaces/wallet.js"; | ||
|
||
/** | ||
* An EIP-7702 authorization input object. | ||
* An EIP-7702 authorization object fully prepared and ready for signing. | ||
* | ||
* @beta | ||
* @transaction | ||
*/ | ||
export type Authorization = { | ||
export type AuthorizationRequest = { | ||
address: Address; | ||
chainId?: number; | ||
nonce?: bigint; | ||
chainId: number; | ||
nonce: bigint; | ||
}; | ||
|
||
/** | ||
* An EIP-7702 authorization object fully prepared and ready for signing. | ||
* Represents a signed EIP-7702 authorization object. | ||
* | ||
* @beta | ||
* @transaction | ||
*/ | ||
export type PreparedAuthorization = { | ||
address: Address; | ||
chainId: number; | ||
nonce: bigint; | ||
}; | ||
export type SignedAuthorization = ox__Authorization.ListSigned[number]; | ||
|
||
/** | ||
* Represents a signed EIP-7702 authorization object. | ||
* Sign the given EIP-7702 authorization object. | ||
* @param options - The options for `signAuthorization` | ||
* Refer to the type [`SignAuthorizationOptions`](https://portal.thirdweb.com/references/typescript/v5/SignAuthorizationOptions) | ||
* @returns The signed authorization object | ||
* | ||
* ```ts | ||
* import { signAuthorization } from "thirdweb"; | ||
* | ||
* const authorization = await signAuthorization({ | ||
* request: { | ||
* address: "0x...", | ||
* chainId: 911867, | ||
* nonce: 100n, | ||
* }, | ||
* account: myAccount, | ||
* }); | ||
* ``` | ||
* | ||
* @beta | ||
* @transaction | ||
*/ | ||
export type SignedAuthorization = ox__Authorization.ListSigned[number]; | ||
export async function signAuthorization(options: { | ||
account: Account; | ||
request: AuthorizationRequest; | ||
}): Promise<SignedAuthorization> { | ||
const { account, request } = options; | ||
if (typeof account.signAuthorization === "undefined") { | ||
throw new Error( | ||
"This account type does not yet support signing EIP-7702 authorizations", | ||
); | ||
} | ||
return account.signAuthorization(request); | ||
} |
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.