kokio-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/.env.example +1 -0
  2. package/.github/workflows/npm_publish.yaml +19 -0
  3. package/LICENSE +21 -0
  4. package/package.json +45 -0
  5. package/src/abis/BeaconProxy.json +985 -0
  6. package/src/abis/DeviceWallet.json +907 -0
  7. package/src/abis/DeviceWalletFactory.json +739 -0
  8. package/src/abis/ESIMWallet.json +486 -0
  9. package/src/abis/ESIMWalletFactory.json +467 -0
  10. package/src/abis/LazyWalletRegistry.json +734 -0
  11. package/src/abis/P256Verifier.json +73 -0
  12. package/src/abis/Registry.json +811 -0
  13. package/src/abis/RegistryHelper.json +382 -0
  14. package/src/abis/index.ts +21 -0
  15. package/src/config.ts +46 -0
  16. package/src/interface/P256VerifierClass.ts +19 -0
  17. package/src/interface/constantsClass.ts +35 -0
  18. package/src/interface/deviceWalletClass.ts +62 -0
  19. package/src/interface/deviceWalletFactoryClass.ts +35 -0
  20. package/src/interface/eSIMWalletClass.ts +55 -0
  21. package/src/interface/eSIMWalletFactoryClass.ts +36 -0
  22. package/src/interface/lazyWalletRegistryClass.ts +34 -0
  23. package/src/interface/smartAccountClass.ts +26 -0
  24. package/src/interface/utilsClass.ts +21 -0
  25. package/src/logic/P256Verifier.ts +31 -0
  26. package/src/logic/account-kit/createSmartAccount.ts +282 -0
  27. package/src/logic/constants.ts +188 -0
  28. package/src/logic/deviceWallet.ts +175 -0
  29. package/src/logic/deviceWalletFactory.ts +70 -0
  30. package/src/logic/eSIMWallet.ts +149 -0
  31. package/src/logic/eSIMWalletFactory.ts +78 -0
  32. package/src/logic/lazyWalletRegistry.ts +98 -0
  33. package/src/logic/services/turnkeyClient.ts +80 -0
  34. package/src/logic/utils.ts +86 -0
  35. package/src/types.ts +38 -0
  36. package/tsconfig.json +114 -0
@@ -0,0 +1,175 @@
1
+ import { Address, createPublicClient, encodeFunctionData, getContract, PublicClient, WalletClient } from "viem";
2
+ import { SmartAccountClient } from "@aa-sdk/core";
3
+ import { DeviceWallet } from "../abis/index.js";
4
+ import { _getChainSpecificConstants, customErrors } from "./constants.js";
5
+ import { P256Key } from "../types.js";
6
+
7
+ export const _deployESIMWallet = async (client: SmartAccountClient, address: Address, hasAccessToETH: boolean, salt: bigint) => {
8
+
9
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET);
10
+
11
+ // UserOp
12
+ return client.sendUserOperation({
13
+ account: client.account,
14
+ uo:{
15
+ target: address,
16
+ data: encodeFunctionData({
17
+ abi: DeviceWallet,
18
+ functionName: "deployESIMWallet",
19
+ args: [hasAccessToETH, salt]
20
+ })
21
+ }
22
+ });
23
+ }
24
+
25
+ export const _setESIMUniqueIdentifierForAnESIMWallet = async (
26
+ client: SmartAccountClient,
27
+ address: Address,
28
+ eSIMWalletAddress: Address,
29
+ eSIMUniqueIdentifier: string
30
+ ) => {
31
+
32
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET);
33
+
34
+ // UserOp
35
+ return client.sendUserOperation({
36
+ account: client.account,
37
+ uo:{
38
+ target: address,
39
+ data: encodeFunctionData({
40
+ abi: DeviceWallet,
41
+ functionName: "setESIMUniqueIdentifierForAnESIMWallet",
42
+ args: [eSIMWalletAddress, eSIMUniqueIdentifier]
43
+ })
44
+ }
45
+ });
46
+ // const contract = (await getContractInstance(client)).deviceWallet(address);
47
+
48
+ // return contract.write.setESIMUniqueIdentifierForAnESIMWallet([eSIMWalletAddress, eSIMUniqueIdentifier]);
49
+ }
50
+
51
+ export const _payETHForDataBundles = async (client: SmartAccountClient, address: Address, amount: bigint) => {
52
+
53
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
54
+
55
+ // UserOp
56
+ return client.sendUserOperation({
57
+ account: client.account,
58
+ uo:{
59
+ target: address,
60
+ data: encodeFunctionData({
61
+ abi: DeviceWallet,
62
+ functionName: "payETHForDataBundles",
63
+ args: [amount]
64
+ })
65
+ }
66
+ });
67
+ }
68
+
69
+ export const _pullETH = async (client: SmartAccountClient, address: Address, amount: bigint) => {
70
+
71
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET);
72
+
73
+ // UserOp
74
+ return client.sendUserOperation({
75
+ account: client.account,
76
+ uo:{
77
+ target: address,
78
+ data: encodeFunctionData({
79
+ abi: DeviceWallet,
80
+ functionName: "pullETH",
81
+ args: [address, amount]
82
+ })
83
+ }
84
+ });
85
+ }
86
+
87
+ export const _getVaultAddress = async (client: SmartAccountClient, address: Address) => {
88
+
89
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET);
90
+
91
+ // UserOp
92
+ return client.sendUserOperation({
93
+ account: client.account,
94
+ uo:{
95
+ target: address,
96
+ data: encodeFunctionData({
97
+ abi: DeviceWallet,
98
+ functionName: "getVaultAddress",
99
+ args: [address]
100
+ })
101
+ }
102
+ });
103
+ }
104
+
105
+ export const _toggleAccessToETH = async (client: SmartAccountClient, address: Address, eSIMWalletAddress: Address, hasAccessToETH: boolean) => {
106
+
107
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET);
108
+
109
+ // UserOp
110
+ return client.sendUserOperation({
111
+ account: client.account,
112
+ uo:{
113
+ target: address,
114
+ data: encodeFunctionData({
115
+ abi: DeviceWallet,
116
+ functionName: "toggleAccessToETH",
117
+ args: [address, eSIMWalletAddress, hasAccessToETH]
118
+ })
119
+ }
120
+ });
121
+ }
122
+
123
+ export const _addESIMWallet = async (client: SmartAccountClient, address: Address, eSIMWalletAddress: Address, hasAccessToETH: boolean) => {
124
+
125
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET);
126
+
127
+ // UserOp
128
+ return client.sendUserOperation({
129
+ account: client.account,
130
+ uo:{
131
+ target: address,
132
+ data: encodeFunctionData({
133
+ abi: DeviceWallet,
134
+ functionName: "addESIMWallet",
135
+ args: [address, eSIMWalletAddress, hasAccessToETH]
136
+ })
137
+ }
138
+ });
139
+ }
140
+
141
+ export const _removeESIMWallet = async (client: SmartAccountClient, address: Address, eSIMWalletAddress: Address, hasAccessToETH: boolean) => {
142
+
143
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET);
144
+
145
+ // UserOp
146
+ return client.sendUserOperation({
147
+ account: client.account,
148
+ uo:{
149
+ target: address,
150
+ data: encodeFunctionData({
151
+ abi: DeviceWallet,
152
+ functionName: "removeESIMWallet",
153
+ args: [address, eSIMWalletAddress, hasAccessToETH]
154
+ })
155
+ }
156
+ });
157
+ }
158
+
159
+ export const _getOwner = async (client: WalletClient, address: Address) => {
160
+
161
+ const chainID = await client.getChainId();
162
+ const values = _getChainSpecificConstants(chainID);
163
+
164
+ const contract = getContract({
165
+ abi: DeviceWallet,
166
+ address: address,
167
+ client
168
+ })
169
+
170
+ const x = await contract.read.owner([0]);
171
+ const y = await contract.read.owner([1]);
172
+
173
+ const owner = [x,y];
174
+ return owner as P256Key;
175
+ }
@@ -0,0 +1,70 @@
1
+ import { encodeFunctionData, WalletClient } from "viem";
2
+ import { SmartAccountClient } from "@aa-sdk/core";
3
+ import { _extractChainID, _getChainSpecificConstants, customErrors } from "./constants.js";
4
+ import { DeviceWalletFactory } from "../abis/index.js";
5
+
6
+ export const _createAccountWithEOA = async (
7
+ client: WalletClient,
8
+ deviceUniqueIdentifier: string,
9
+ deviceWalletOwnerKey: string,
10
+ salt: bigint,
11
+ depositAmount: bigint
12
+ ) => {
13
+
14
+ const chainID = await client.getChainId();
15
+ const values = _getChainSpecificConstants(chainID);
16
+
17
+ if (!client.account) throw new Error(customErrors.MISSING_EOA_WALLET);
18
+
19
+ return client.writeContract({
20
+ address: values.factoryAddresses.DEVICE_WALLET_FACTORY,
21
+ chain: values.chain,
22
+ account: client.account.address,
23
+ abi: DeviceWalletFactory,
24
+ functionName: 'createAccount',
25
+ args: [deviceUniqueIdentifier, deviceWalletOwnerKey, salt, depositAmount]
26
+ });
27
+ }
28
+
29
+ export const _getAddress = async (
30
+ client: SmartAccountClient,
31
+ deviceUniqueIdentifier: string,
32
+ deviceWalletOwnerKey: string,
33
+ salt: bigint,
34
+ ) => {
35
+
36
+ const values = _getChainSpecificConstants(await client.getChainId());
37
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
38
+
39
+ // UserOp
40
+ return client.sendUserOperation({
41
+ account: client.account,
42
+ uo:{
43
+ target: values.factoryAddresses.DEVICE_WALLET_FACTORY,
44
+ data: encodeFunctionData({
45
+ abi: DeviceWalletFactory,
46
+ functionName: "getAddress",
47
+ args: [deviceUniqueIdentifier, deviceWalletOwnerKey, salt]
48
+ })
49
+ }
50
+ });
51
+ }
52
+
53
+ export const _getCurrentDeviceWalletImplementation = async (client: SmartAccountClient) => {
54
+
55
+ const values = _getChainSpecificConstants(await client.getChainId());
56
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
57
+
58
+ // UserOp
59
+ return client.sendUserOperation({
60
+ account: client.account,
61
+ uo:{
62
+ target: values.factoryAddresses.DEVICE_WALLET_FACTORY,
63
+ data: encodeFunctionData({
64
+ abi: DeviceWalletFactory,
65
+ functionName: "getCurrentDeviceWalletImplementation",
66
+ args: []
67
+ })
68
+ }
69
+ });
70
+ }
@@ -0,0 +1,149 @@
1
+ import { Address, encodeFunctionData, WalletClient } from "viem"
2
+ import { DataBundleDetails } from "../types.js";
3
+ import { SmartAccountClient } from "@aa-sdk/core";
4
+ import { customErrors } from "./constants.js";
5
+ import { ESIMWallet } from "../abis/index.js";
6
+
7
+
8
+ export const _setESIMUniqueIdentifier = async (client: SmartAccountClient, address: Address, eSIMUniqueIdentifier: string) => {
9
+
10
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
11
+
12
+ // UserOp
13
+ return client.sendUserOperation({
14
+ account: client.account,
15
+ uo:{
16
+ target: address,
17
+ data: encodeFunctionData({
18
+ abi: ESIMWallet,
19
+ functionName: "setESIMUniqueIdentifier",
20
+ args: [eSIMUniqueIdentifier]
21
+ })
22
+ }
23
+ });
24
+ }
25
+
26
+ export const _buyDataBundle = async (client: SmartAccountClient, address: Address, dataBundleDetails: DataBundleDetails) => {
27
+
28
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
29
+
30
+ // UserOp
31
+ return client.sendUserOperation({
32
+ account: client.account,
33
+ uo:{
34
+ target: address,
35
+ data: encodeFunctionData({
36
+ abi: ESIMWallet,
37
+ functionName: "payETHForDataBundles",
38
+ args: [dataBundleDetails]
39
+ })
40
+ }
41
+ });
42
+ }
43
+
44
+ export const _populateHistory = async (client: SmartAccountClient, address: Address, dataBundleDetails: Array<DataBundleDetails>) => {
45
+
46
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
47
+
48
+ // UserOp
49
+ return client.sendUserOperation({
50
+ account: client.account,
51
+ uo:{
52
+ target: address,
53
+ data: encodeFunctionData({
54
+ abi: ESIMWallet,
55
+ functionName: "populateHistory",
56
+ args: [dataBundleDetails]
57
+ })
58
+ }
59
+ });
60
+ }
61
+
62
+ export const _owner = async (client: SmartAccountClient, address: Address) => {
63
+
64
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
65
+
66
+ // UserOp
67
+ return client.sendUserOperation({
68
+ account: client.account,
69
+ uo:{
70
+ target: address,
71
+ data: encodeFunctionData({
72
+ abi: ESIMWallet,
73
+ functionName: "ow",
74
+ args: []
75
+ })
76
+ }
77
+ });
78
+ }
79
+
80
+ export const _requestTransferOwnership = async (client: SmartAccountClient, address: Address, newOwner: Address) => {
81
+
82
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
83
+
84
+ // UserOp
85
+ return client.sendUserOperation({
86
+ account: client.account,
87
+ uo:{
88
+ target: address,
89
+ data: encodeFunctionData({
90
+ abi: ESIMWallet,
91
+ functionName: "requestTransferOwnership",
92
+ args: [newOwner]
93
+ })
94
+ }
95
+ });
96
+ }
97
+
98
+ export const _acceptOwnershipTransfer = async (client: SmartAccountClient, address: Address) => {
99
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
100
+
101
+ // UserOp
102
+ return client.sendUserOperation({
103
+ account: client.account,
104
+ uo:{
105
+ target: address,
106
+ data: encodeFunctionData({
107
+ abi: ESIMWallet,
108
+ functionName: "acceptOwnershipTransfer",
109
+ args: []
110
+ })
111
+ }
112
+ });
113
+ }
114
+
115
+ export const _sendETHToDeviceWallet = async (client: SmartAccountClient, address: Address, amount: bigint) => {
116
+
117
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
118
+
119
+ // UserOp
120
+ return client.sendUserOperation({
121
+ account: client.account,
122
+ uo:{
123
+ target: address,
124
+ data: encodeFunctionData({
125
+ abi: ESIMWallet,
126
+ functionName: "sendETHToDeviceWallet",
127
+ args: [amount]
128
+ })
129
+ }
130
+ });
131
+ }
132
+
133
+ export const _transferOwnership = async (client: SmartAccountClient, address: Address, amount: bigint) => {
134
+
135
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
136
+
137
+ // UserOp
138
+ return client.sendUserOperation({
139
+ account: client.account,
140
+ uo:{
141
+ target: address,
142
+ data: encodeFunctionData({
143
+ abi: ESIMWallet,
144
+ functionName: "transferOwnership",
145
+ args: [amount]
146
+ })
147
+ }
148
+ });
149
+ }
@@ -0,0 +1,78 @@
1
+ import { Address, encodeFunctionData, WalletClient } from "viem"
2
+ import { _getChainSpecificConstants, customErrors } from "./constants.js";
3
+ import { SmartAccountClient } from "@aa-sdk/core";
4
+ import { ESIMWalletFactory } from "../abis/index.js";
5
+
6
+ export const _addRegistryAddress = async (client: SmartAccountClient, registryContractAddress: Address) => {
7
+
8
+ const values = _getChainSpecificConstants(await client.getChainId());
9
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
10
+
11
+ // UserOp
12
+ return client.sendUserOperation({
13
+ account: client.account,
14
+ uo:{
15
+ target: values.factoryAddresses.ESIM_WALLET_FACTORY,
16
+ data: encodeFunctionData({
17
+ abi: ESIMWalletFactory,
18
+ functionName: "addRegistryAddress",
19
+ args: [registryContractAddress]
20
+ })
21
+ }
22
+ });
23
+ }
24
+
25
+ export const _deployESIMWalletWithEOA = async (client: WalletClient, deviceWalletAddress: Address, salt: bigint) => {
26
+
27
+ const chainID = await client.getChainId();
28
+ const values = _getChainSpecificConstants(chainID);
29
+
30
+ if (!client.account) throw new Error(customErrors.MISSING_EOA_WALLET);
31
+
32
+ return client.writeContract({
33
+ address: values.factoryAddresses.ESIM_WALLET_FACTORY,
34
+ chain: values.chain,
35
+ account: client.account.address,
36
+ abi: ESIMWalletFactory,
37
+ functionName: 'deployESIMWallet',
38
+ args: [deviceWalletAddress, salt]
39
+ });
40
+ }
41
+
42
+ export const _deployESIMWalletWithUserOp = async (client: SmartAccountClient, deviceWalletAddress: Address, salt: bigint) => {
43
+
44
+ const values = _getChainSpecificConstants(await client.getChainId());
45
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
46
+
47
+ // UserOp
48
+ return client.sendUserOperation({
49
+ account: client.account,
50
+ uo:{
51
+ target: values.factoryAddresses.ESIM_WALLET_FACTORY,
52
+ data: encodeFunctionData({
53
+ abi: ESIMWalletFactory,
54
+ functionName: "deployESIMWallet",
55
+ args: [deviceWalletAddress, salt]
56
+ })
57
+ }
58
+ });
59
+ }
60
+
61
+ export const _getCurrentESIMWalletImplementation = async (client: SmartAccountClient) => {
62
+
63
+ const values = _getChainSpecificConstants(await client.getChainId());
64
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
65
+
66
+ // UserOp
67
+ return client.sendUserOperation({
68
+ account: client.account,
69
+ uo:{
70
+ target: values.factoryAddresses.ESIM_WALLET_FACTORY,
71
+ data: encodeFunctionData({
72
+ abi: ESIMWalletFactory,
73
+ functionName: "getCurrentESIMWalletImplementation",
74
+ args: []
75
+ })
76
+ }
77
+ });
78
+ }
@@ -0,0 +1,98 @@
1
+ import { encodeFunctionData, Hex, WalletClient } from "viem";
2
+ import { DataBundleDetails } from "../types.js";
3
+ import { _getChainSpecificConstants, customErrors } from "./constants.js";
4
+ import { SmartAccountClient } from "@aa-sdk/core";
5
+ import { LazyWalletRegistry } from "../abis/index.js";
6
+
7
+
8
+ export const _isLazyWalletDeployed = async (client: SmartAccountClient, deviceUniqueIdentifier: string) => {
9
+
10
+ const values = _getChainSpecificConstants(await client.getChainId());
11
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
12
+
13
+ // UserOp
14
+ return client.sendUserOperation({
15
+ account: client.account,
16
+ uo:{
17
+ target: values.factoryAddresses.LAZY_WALLET_REGISTRY,
18
+ data: encodeFunctionData({
19
+ abi: LazyWalletRegistry,
20
+ functionName: "isLazyWalletDeployed",
21
+ args: [deviceUniqueIdentifier]
22
+ })
23
+ }
24
+ });
25
+ }
26
+
27
+ export const _batchPopulateHistory = async (
28
+ client: SmartAccountClient,
29
+ deviceUniqueIdentifiers: Array<string>,
30
+ eSIMUniqueIdentifiers: Array<Array<string>>,
31
+ dataBundleDetails: Array<Array<DataBundleDetails>>
32
+ ) => {
33
+
34
+ const values = _getChainSpecificConstants(await client.getChainId());
35
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
36
+
37
+ // UserOp
38
+ return client.sendUserOperation({
39
+ account: client.account,
40
+ uo:{
41
+ target: values.factoryAddresses.LAZY_WALLET_REGISTRY,
42
+ data: encodeFunctionData({
43
+ abi: LazyWalletRegistry,
44
+ functionName: "batchPopulateHistory",
45
+ args: [deviceUniqueIdentifiers, eSIMUniqueIdentifiers, dataBundleDetails]
46
+ })
47
+ }
48
+ });
49
+ }
50
+
51
+ export const _deployLazyWalletAndSetESIMIdentifier = async (
52
+ client: SmartAccountClient,
53
+ deviceOwnerPublicKey: Hex[2],
54
+ deviceUniqueIdentifier: string,
55
+ salt: bigint,
56
+ depositAmount: bigint
57
+ ) => {
58
+
59
+ const values = _getChainSpecificConstants(await client.getChainId());
60
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
61
+
62
+ // UserOp
63
+ return client.sendUserOperation({
64
+ account: client.account,
65
+ uo:{
66
+ target: values.factoryAddresses.LAZY_WALLET_REGISTRY,
67
+ data: encodeFunctionData({
68
+ abi: LazyWalletRegistry,
69
+ functionName: "deployLazyWalletAndSetESIMIdentifier",
70
+ args: [deviceOwnerPublicKey, deviceUniqueIdentifier, salt, depositAmount]
71
+ })
72
+ }
73
+ });
74
+ }
75
+
76
+ export const _switchESIMIdentifierToNewDeviceIdentifier = async (
77
+ client: SmartAccountClient,
78
+ eSIMIdentifier: string,
79
+ oldDeviceIdentifier: string,
80
+ newDeviceIdentifier: string
81
+ ) => {
82
+
83
+ const values = _getChainSpecificConstants(await client.getChainId());
84
+ if(!client.account) throw new Error(customErrors.MISSING_SMART_WALLET)
85
+
86
+ // UserOp
87
+ return client.sendUserOperation({
88
+ account: client.account,
89
+ uo:{
90
+ target: values.factoryAddresses.LAZY_WALLET_REGISTRY,
91
+ data: encodeFunctionData({
92
+ abi: LazyWalletRegistry,
93
+ functionName: "switchESIMIdentifierToNewDeviceIdentifier",
94
+ args: [eSIMIdentifier, oldDeviceIdentifier, newDeviceIdentifier]
95
+ })
96
+ }
97
+ });
98
+ }
@@ -0,0 +1,80 @@
1
+ import { assertActivityCompleted, assertNonNull, TurnkeyClient } from "@turnkey/http"
2
+ import { Address, hashMessage, hashTypedData, SignableMessage, toHex, TypedData, TypedDataDefinition } from "viem";
3
+ import { decodeAttestationObject, decodeClientDataJSON } from "@simplewebauthn/server/helpers";
4
+ import { base64UrlToBuffer } from "../utils.js";
5
+ import { WebAuthnSignature } from "../../types.js";
6
+
7
+ export const _signMessageWithTurnkey = async (client: TurnkeyClient, organizationId: string, signWith: Address, message: SignableMessage): Promise<WebAuthnSignature> => {
8
+
9
+ const payload = hashMessage(message);
10
+
11
+ const { activity } = await client.signRawPayload({
12
+ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2",
13
+ organizationId: organizationId,
14
+ parameters: {
15
+ signWith: signWith,
16
+ payload: payload,
17
+ encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
18
+ hashFunction: "HASH_FUNCTION_NO_OP",
19
+ },
20
+ timestampMs: String(Date.now()), // millisecond timestamp
21
+ });
22
+
23
+ assertActivityCompleted(activity);
24
+
25
+ const attestationObject = assertNonNull(activity.intent.createOrganizationIntent?.rootAuthenticator.attestation.response.attestationObject);
26
+ const decodedAttestationObj = decodeAttestationObject(base64UrlToBuffer(attestationObject))
27
+
28
+ const clientDataJson = assertNonNull(activity.intent.createOrganizationIntent?.rootAuthenticator.attestation.response.clientDataJson);
29
+ const decodedClientDataJson = decodeClientDataJSON(clientDataJson);
30
+
31
+ const signature = assertNonNull(activity.result.signRawPayloadResult);
32
+
33
+ return {
34
+ authenticatorData: toHex(decodedAttestationObj.get("authData")),
35
+ clientDataJSON: JSON.stringify(decodedClientDataJson),
36
+ challengeIndex: BigInt(23),
37
+ typeIndex: BigInt(1),
38
+ r: BigInt(signature.r),
39
+ s: BigInt(signature.s)
40
+ }
41
+ }
42
+
43
+ export const _signTypedDataWithTurnkey = async <
44
+ const typedData extends TypedData | Record<string, unknown>,
45
+ primaryType extends keyof typedData | "EIP712Domain" = keyof typedData
46
+ > (client: TurnkeyClient, organizationId: string, signWith: Address, typedData: TypedDataDefinition<typedData, primaryType>): Promise<WebAuthnSignature> => {
47
+
48
+ const payload = hashTypedData(typedData);
49
+
50
+ const { activity } = await client.signRawPayload({
51
+ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2",
52
+ organizationId: organizationId,
53
+ parameters: {
54
+ signWith: signWith,
55
+ payload: payload,
56
+ encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
57
+ hashFunction: "HASH_FUNCTION_NO_OP",
58
+ },
59
+ timestampMs: String(Date.now()), // millisecond timestamp
60
+ });
61
+
62
+ assertActivityCompleted(activity);
63
+
64
+ const attestationObject = assertNonNull(activity.intent.createOrganizationIntent?.rootAuthenticator.attestation.response.attestationObject);
65
+ const decodedAttestationObj = decodeAttestationObject(base64UrlToBuffer(attestationObject))
66
+
67
+ const clientDataJson = assertNonNull(activity.intent.createOrganizationIntent?.rootAuthenticator.attestation.response.clientDataJson);
68
+ const decodedClientDataJson = decodeClientDataJSON(clientDataJson);
69
+
70
+ const signature = assertNonNull(activity.result.signRawPayloadResult);
71
+
72
+ return {
73
+ authenticatorData: toHex(decodedAttestationObj.get("authData")),
74
+ clientDataJSON: JSON.stringify(decodedClientDataJson),
75
+ challengeIndex: BigInt(23),
76
+ typeIndex: BigInt(1),
77
+ r: BigInt(signature.r),
78
+ s: BigInt(signature.s)
79
+ }
80
+ }