-
Notifications
You must be signed in to change notification settings - Fork 138
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
fix: local account signers were not correctly handling hex inputs #42
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
6 changes: 4 additions & 2 deletions
6
packages/accounts/src/kernel-zerodev/__tests__/mocks/mock-signer-validator.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,18 +1,20 @@ | ||
import { Address, Hex, SmartAccountSigner } from "@alchemy/aa-core"; | ||
import type { Address, Hex, SmartAccountSigner } from "@alchemy/aa-core"; | ||
|
||
export class MockSignerValidator implements SmartAccountSigner { | ||
getAddress(): Promise<Address> { | ||
return Promise.resolve("0xabcfC3DB1e0f5023F5a4f40c03D149f316E6A5cc"); | ||
} | ||
|
||
signMessage(msg: Uint8Array | Hex | string): Promise<Hex> { | ||
async signMessage(msg: Uint8Array | Hex | string): Promise<Hex> { | ||
switch (msg) { | ||
case "0xabcfC3DB1e0f5023F5a4f40c03D149f316E6A5cc": | ||
return "0x64e29e4786b3740ceffc2c1a932124ee74c29b552957ea3bde8913753d1763af4f03362e387d2badb33932e8fc4f7b3411a0a5ade32a5b708aa48c171632a6211b"; | ||
case "icanbreakthistestcase": | ||
return "0xabd26de022c2785a7d86c5c388f4adef5d93358b39fbb757463bc9edc78b7b86566cb1ab8c7ff3a52b10d98de6398aacc7b48aec92a3e280065a47b9698209541b"; | ||
case "0xbc7299170f076afcbafe11da04482e72e3beccabcd82de0cd2797500e81b76c4": | ||
return "0x6c21c7271c8403452c5a812c9ba776b33b12733953f154d36d989d379c92ec632b7a1997ca16203a7ef5fcd639bcaa3d5420b65e0774a8bca7fad6d1437024661c"; | ||
default: | ||
throw new Error("Invalid message"); | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/accounts/src/kernel-zerodev/__tests__/mocks/mock-signer.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
This file was deleted.
Oops, something went wrong.
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,35 +1,7 @@ | ||
import type { Address } from "abitype"; | ||
import type { HDAccount, Hex } from "viem"; | ||
import { mnemonicToAccount } from "viem/accounts"; | ||
import type { SmartAccountSigner } from "./types"; | ||
import type { HDAccount } from "viem"; | ||
import { LocalAccountSigner } from "./local-account"; | ||
|
||
export class HdAccountSigner implements SmartAccountSigner { | ||
owner: HDAccount; | ||
|
||
constructor(owner: HDAccount) { | ||
this.owner = owner; | ||
} | ||
|
||
signMessage(msg: Uint8Array | Hex | string): Promise<Hex> { | ||
if (typeof msg === "string") { | ||
return this.owner.signMessage({ | ||
message: msg, | ||
}); | ||
} else { | ||
return this.owner.signMessage({ | ||
message: { | ||
raw: msg, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
getAddress(): Promise<Address> { | ||
return Promise.resolve(this.owner.address); | ||
} | ||
|
||
static mnemonicToAccountSigner(key: Hex): HdAccountSigner { | ||
const owner = mnemonicToAccount(key); | ||
return new HdAccountSigner(owner); | ||
} | ||
} | ||
/** | ||
* @deprecated use LocalAccountSigner instead | ||
*/ | ||
export class HdAccountSigner extends LocalAccountSigner<HDAccount> {} |
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,72 @@ | ||
import { LocalAccountSigner } from "./local-account"; | ||
|
||
describe("Local Account Signer Tests", () => { | ||
describe("Using HD Account", () => { | ||
const dummyMnemonic = | ||
"test test test test test test test test test test test test"; | ||
const signer = LocalAccountSigner.mnemonicToAccountSigner(dummyMnemonic); | ||
|
||
it("should sign a hex message properly", async () => { | ||
expect( | ||
await signer.signMessage("0xabcfC3DB1e0f5023F5a4f40c03D149f316E6A5cc") | ||
).toMatchInlineSnapshot( | ||
'"0x35761512143ffd8da07c93c5a0136424fe935b48e77076f501a57745c16268bf0d9a5d6209b12d5f8b62f96f0991372e046092fd6b1e3bfa610eb51607a28f7e1b"' | ||
); | ||
}); | ||
|
||
it("should sign a string message properly", async () => { | ||
expect( | ||
await signer.signMessage("icanbreakthistestcase") | ||
).toMatchInlineSnapshot( | ||
'"0x6b2efa82d72558efad294b727741e1eb9ff4800e2d66ce5f0373454ebdd13573630985612c1d618f49f173f65cf83fc131f798cb65ae66f90f8f0b9d4fdbca881b"' | ||
); | ||
|
||
expect( | ||
await signer.signMessage("i will definately break this test case") | ||
).toMatchInlineSnapshot( | ||
'"0x1d9a20cfd63c179daab241d9e64a2549e1a47c1ca7c465df6f5b8c5720cee7266050b89b3874b384c88cd8325601572e79b3b626dc756a6aec5fff05d4ce5efc1b"' | ||
); | ||
}); | ||
|
||
it("should sign a byte array correctly", async () => { | ||
expect( | ||
await signer.signMessage(new TextEncoder().encode("hello, I'm moldy")) | ||
).toEqual(await signer.signMessage("hello, I'm moldy")); | ||
}); | ||
}); | ||
|
||
describe("Using Private Key Account", () => { | ||
const dummyPrivateKey = | ||
"0x022430a80f723d8789f0d4fb346bdd013b546e4b96fcacf8aceca2b1a65a19dc"; | ||
const signer = | ||
LocalAccountSigner.privateKeyToAccountSigner(dummyPrivateKey); | ||
|
||
it("should sign a hex message properly", async () => { | ||
expect( | ||
await signer.signMessage("0xabcfC3DB1e0f5023F5a4f40c03D149f316E6A5cc") | ||
).toMatchInlineSnapshot( | ||
'"0x91b6680c8f442f46ca71fee15cdd8c9e25693baeb4006d1908a453fd145315ce21a5e7f2ce9760fc993d65e8450fa5225d8dee12972886bdacbb989ca0b09c6c1b"' | ||
); | ||
}); | ||
|
||
it("should sign a string message properly", async () => { | ||
expect( | ||
await signer.signMessage("icanbreakthistestcase") | ||
).toMatchInlineSnapshot( | ||
'"0xabd26de022c2785a7d86c5c388f4adef5d93358b39fbb757463bc9edc78b7b86566cb1ab8c7ff3a52b10d98de6398aacc7b48aec92a3e280065a47b9698209541b"' | ||
); | ||
|
||
expect( | ||
await signer.signMessage("i will definately break this test case") | ||
).toMatchInlineSnapshot( | ||
'"0x907004e990bb1bca76d9fed6bf4a6b614a8d11b6430657cb99ae83fae55c9bc60571876d8c01832e4661dd4312fa08b799b6f59c9b9abddd67e181abc6aef17e1c"' | ||
); | ||
}); | ||
|
||
it("should sign a byte array correctly", async () => { | ||
expect( | ||
await signer.signMessage(new TextEncoder().encode("hello, I'm moldy")) | ||
).toEqual(await signer.signMessage("hello, I'm moldy")); | ||
}); | ||
}); | ||
}); |
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,44 @@ | ||
import { isHex, type HDAccount, type Hex, type PrivateKeyAccount } from "viem"; | ||
import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts"; | ||
import type { SmartAccountSigner } from "./types.js"; | ||
|
||
export class LocalAccountSigner<T extends HDAccount | PrivateKeyAccount> | ||
implements SmartAccountSigner | ||
{ | ||
private owner: T; | ||
constructor(owner: T) { | ||
this.owner = owner; | ||
} | ||
|
||
readonly signMessage: (msg: string | Uint8Array) => Promise<`0x${string}`> = ( | ||
msg | ||
) => { | ||
if (typeof msg === "string" && !isHex(msg)) { | ||
return this.owner.signMessage({ | ||
message: msg, | ||
}); | ||
} else { | ||
return this.owner.signMessage({ | ||
message: { | ||
raw: msg, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
readonly getAddress: () => Promise<`0x${string}`> = async () => { | ||
return this.owner.address; | ||
}; | ||
|
||
static mnemonicToAccountSigner(key: string): LocalAccountSigner<HDAccount> { | ||
const owner = mnemonicToAccount(key); | ||
return new LocalAccountSigner(owner); | ||
} | ||
|
||
static privateKeyToAccountSigner( | ||
key: Hex | ||
): LocalAccountSigner<PrivateKeyAccount> { | ||
const owner = privateKeyToAccount(key); | ||
return new LocalAccountSigner(owner); | ||
} | ||
} |
Oops, something went wrong.
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.
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.
the tests in this file were moved to
local-account.test.ts