-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add native signer, implement signMessage and signTypedData
- Loading branch information
Showing
5 changed files
with
258 additions
and
25 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
account-kit/smart-contracts/src/ma-v2/account/nativeSMASigner.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,111 @@ | ||
import type { SmartAccountSigner } from "@aa-sdk/core"; | ||
import { | ||
hashMessage, | ||
hashTypedData, | ||
type Hex, | ||
type SignableMessage, | ||
type TypedData, | ||
type TypedDataDefinition, | ||
type Chain, | ||
type Address, | ||
} from "viem"; | ||
|
||
import { packUOSignature, pack1271Signature } from "../utils.js"; | ||
/** | ||
* Creates an object with methods for generating a dummy signature, signing user operation hashes, signing messages, and signing typed data. | ||
* | ||
* @example | ||
* ```ts | ||
* import { singleSignerMessageSigner } from "@account-kit/smart-contracts"; | ||
* import { LocalAccountSigner } from "@aa-sdk/core"; | ||
* | ||
* const MNEMONIC = "...": | ||
* | ||
* const account = createSMAV2Account({ config }); | ||
* | ||
* const signer = LocalAccountSigner.mnemonicToAccountSigner(MNEMONIC); | ||
* | ||
* const messageSigner = singleSignerMessageSigner(signer, chain); | ||
* ``` | ||
* | ||
* @param {TSigner} signer Signer to use for signing operations | ||
* @param {Chain} chain Chain object for the signer | ||
* @param {Address} accountAddress address of the smart account using this signer | ||
* @param {number} entityId the entity id of the signing validation | ||
* @returns {object} an object with methods for signing operations and managing signatures | ||
*/ | ||
export const nativeSMASigner = <TSigner extends SmartAccountSigner>( | ||
signer: TSigner, | ||
chain: Chain, | ||
accountAddress: Address, | ||
entityId: number | ||
) => { | ||
const apply712MessageWrap = async (digest: Hex): Promise<Hex> => { | ||
return hashTypedData({ | ||
domain: { | ||
chainId: Number(chain.id), | ||
verifyingContract: accountAddress, | ||
}, | ||
types: { | ||
ReplaySafeHash: [{ name: "digest", type: "bytes32" }], | ||
}, | ||
message: { | ||
digest, | ||
}, | ||
primaryType: "ReplaySafeHash", | ||
}); | ||
}; | ||
|
||
return { | ||
getDummySignature: (): Hex => { | ||
const dummyEcdsaSignature = | ||
"0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c"; | ||
|
||
return packUOSignature({ | ||
// orderedHookData: [], | ||
validationSignature: dummyEcdsaSignature, | ||
}); | ||
}, | ||
|
||
signUserOperationHash: (uoHash: Hex): Promise<Hex> => { | ||
return signer.signMessage({ raw: uoHash }).then((signature: Hex) => | ||
packUOSignature({ | ||
// orderedHookData: [], | ||
validationSignature: signature, | ||
}) | ||
); | ||
}, | ||
|
||
// we apply the expected 1271 packing here since the account contract will expect it | ||
async signMessage({ | ||
message, | ||
}: { | ||
message: SignableMessage; | ||
}): Promise<`0x${string}`> { | ||
const digest = await apply712MessageWrap(hashMessage(message)); | ||
|
||
return pack1271Signature({ | ||
validationSignature: await signer.signMessage({ raw: digest }), | ||
entityId, | ||
}); | ||
}, | ||
|
||
// we don't apply the expected 1271 packing since deferred sigs use typed data sigs and don't expect the 1271 packing | ||
signTypedData: async < | ||
const typedData extends TypedData | Record<string, unknown>, | ||
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData | ||
>( | ||
typedDataDefinition: TypedDataDefinition<typedData, primaryType> | ||
): Promise<Hex> => { | ||
// the accounts domain already gives replay protection across accounts for deferred actions, so we don't need to apply another wrapping | ||
const isDeferredAction = | ||
typedDataDefinition?.primaryType === "DeferredAction" && | ||
typedDataDefinition?.domain?.verifyingContract === accountAddress; | ||
const digest = isDeferredAction | ||
? hashTypedData(typedDataDefinition) | ||
: await apply712MessageWrap(hashTypedData(typedDataDefinition)); | ||
return signer.signMessage({ raw: digest }); | ||
}, | ||
}; | ||
}; |
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.