-
Notifications
You must be signed in to change notification settings - Fork 36
feat(core): SignerCkbAlwaysSuccess
#449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| "@ckb-ccc/core": minor | ||
| --- | ||
|
|
||
| feat(core): `SignerCkbAlwaysSuccess` | ||
|
|
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
packages/core/src/signer/ckb/signerCkbAlwaysSuccess.test.ts
This file contains hidden or 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,56 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { ccc } from "../../index.js"; | ||
|
|
||
| describe("SignerCkbAlwaysSuccess", () => { | ||
| const scriptInfo = ccc.ScriptInfo.from({ | ||
| codeHash: | ||
| "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
| hashType: "data1", | ||
| cellDeps: [ | ||
| { | ||
| cellDep: { | ||
| outPoint: { | ||
| txHash: | ||
| "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
| index: 0, | ||
| }, | ||
| depType: "code", | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| const client = { | ||
| addressPrefix: "ckt", | ||
| getKnownScript: async () => scriptInfo, | ||
| getCellDeps: async () => scriptInfo.cellDeps.map(({ cellDep }) => cellDep), | ||
| } as unknown as ccc.Client; | ||
| const signer = new ccc.SignerCkbAlwaysSuccess(client); | ||
|
|
||
| it("uses the always-success script with empty args", async () => { | ||
| const addresses = await signer.getAddressObjs(); | ||
|
|
||
| expect(addresses).toHaveLength(1); | ||
| expect(signer.type).toBe(ccc.SignerType.CKB); | ||
| expect(signer.signType).toBe(ccc.SignerSignType.Unknown); | ||
| expect(await signer.isConnected()).toBe(true); | ||
| expect(addresses[0].script).toEqual( | ||
| ccc.Script.from({ | ||
| codeHash: scriptInfo.codeHash, | ||
| hashType: scriptInfo.hashType, | ||
| args: "0x", | ||
| }), | ||
| ); | ||
| }); | ||
|
Hanssen0 marked this conversation as resolved.
|
||
|
|
||
| it("adds the always-success cell dep while signing", async () => { | ||
| const tx = ccc.Transaction.default(); | ||
|
|
||
| const signed = await signer.signTransaction(tx); | ||
|
|
||
| expect(signed.cellDeps).toHaveLength(1); | ||
| expect(signed.cellDeps[0].eq(scriptInfo.cellDeps[0].cellDep)).toBe(true); | ||
|
|
||
| const signedAgain = await signer.signTransaction(signed); | ||
| expect(signedAgain.cellDeps).toHaveLength(1); | ||
| }); | ||
|
Hanssen0 marked this conversation as resolved.
|
||
| }); | ||
This file contains hidden or 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 @@ | ||
| import { Address } from "../../address/index.js"; | ||
| import { Transaction, TransactionLike } from "../../ckb/index.js"; | ||
| import { Client, KnownScript } from "../../client/index.js"; | ||
| import { Signer, SignerSignType, SignerType } from "../signer/index.js"; | ||
|
|
||
| /** | ||
| * A signer for the well-known always-success lock script. | ||
| * | ||
| * The always-success script does not require a signature, so signing a | ||
| * transaction only needs to add its cell dependency. | ||
| * | ||
| * @public | ||
| */ | ||
| export class SignerCkbAlwaysSuccess extends Signer { | ||
| get type(): SignerType { | ||
| return SignerType.CKB; | ||
| } | ||
|
|
||
| get signType(): SignerSignType { | ||
| return SignerSignType.Unknown; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an instance of SignerCkbAlwaysSuccess. | ||
| * | ||
| * @param client - The client instance used for communication and resolving | ||
| * the always-success deployment. | ||
| */ | ||
| constructor(client: Client) { | ||
| super(client); | ||
| } | ||
|
|
||
| async connect(): Promise<void> {} | ||
|
|
||
| async isConnected(): Promise<boolean> { | ||
| return true; | ||
| } | ||
|
|
||
| async getInternalAddress(): Promise<string> { | ||
| return this.getRecommendedAddress(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the address of the always-success lock script. | ||
| */ | ||
| async getAddressObjs(): Promise<Address[]> { | ||
| return [ | ||
| await Address.fromKnownScript( | ||
| this.client, | ||
| KnownScript.AlwaysSuccess, | ||
| "0x", | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the always-success cell dependency to a transaction. | ||
| */ | ||
| async prepareTransaction(txLike: TransactionLike): Promise<Transaction> { | ||
| const tx = Transaction.from(txLike); | ||
| await tx.addCellDepsOfKnownScripts(this.client, KnownScript.AlwaysSuccess); | ||
| return tx; | ||
| } | ||
|
|
||
| /** | ||
| * The always-success script requires no signature. | ||
| */ | ||
| async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> { | ||
| return Transaction.from(txLike); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.