kokio-sdk 0.0.3 → 0.0.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kokio-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/esm/config.js",
@@ -25,7 +25,7 @@ import { _getChainSpecificConstants, ZERO } from "../constants.js";
25
25
  import { _add0x, _concatUint8Arrays, _remove0x, _shouldRemoveLeadingZero } from "../utils.js";
26
26
  import { P256Key, WebAuthnSignature } from "../../types.js";
27
27
  import { DeviceWallet, DeviceWalletFactory } from "../../abis/index.js";
28
- import { _signMessageWithTurnkey, _signTypedDataWithTurnkey } from "../services/turnkeyClient.js";
28
+ import { _signMessageWithTurnkey, _signTypedDataWithTurnkey, _stampAndSignMessageWithTurnkey, _stampAndSignTypedDataWithTurnkey } from "../services/turnkeyClient.js";
29
29
  import { alchemyGasManagerMiddleware } from "@account-kit/infra";
30
30
 
31
31
  const _encodeExecute = async (tx: AccountOp) => {
@@ -140,6 +140,7 @@ const getCounterFactualAddress = async (client: WalletClient, deviceUniqueIdenti
140
140
  const values = _getChainSpecificConstants(chainID, rpcURL);
141
141
  const deviceWalletFactoryAddress = values.factoryAddresses.DEVICE_WALLET_FACTORY;
142
142
 
143
+
143
144
  sender = sender? sender : values.factoryAddresses.SENDER_CREATOR;
144
145
  const uniqueSaltBytes32 = prepareSaltForCreate2(sender, salt);
145
146
  const initCodeHash = await getInitCodeHash(client, deviceUniqueIdentifier, deviceWalletOwnerKey);
@@ -207,7 +208,7 @@ const _encodeSignature = async (webAuthnSignature: WebAuthnSignature): Promise<H
207
208
 
208
209
  const _signMessage = async (message: SignableMessage, turnkeyClient: TurnkeyClient, organiationId: string, signWith: Address): Promise<Hex> => {
209
210
 
210
- const webAuthnSignature = await _signMessageWithTurnkey(turnkeyClient, organiationId, signWith, message);
211
+ const webAuthnSignature = await _stampAndSignMessageWithTurnkey(turnkeyClient, organiationId, signWith, message);
211
212
 
212
213
  return _encodeSignature(webAuthnSignature);
213
214
  }
@@ -217,7 +218,7 @@ const _signTypedData = async <
217
218
  primaryType extends keyof typedData | "EIP712Domain" = keyof typedData
218
219
  > (typedData: TypedDataDefinition<typedData, primaryType>, turnkeyClient: TurnkeyClient, organiationId: string, signWith: Address): Promise<Hex> => {
219
220
 
220
- const webAuthnSignature = await _signTypedDataWithTurnkey(turnkeyClient, organiationId, signWith, typedData);
221
+ const webAuthnSignature = await _stampAndSignTypedDataWithTurnkey(turnkeyClient, organiationId, signWith, typedData);
221
222
 
222
223
  return _encodeSignature(webAuthnSignature);
223
224
  }
@@ -1,6 +1,6 @@
1
- import { assertActivityCompleted, assertNonNull, TurnkeyClient } from "@turnkey/http"
1
+ import { assertActivityCompleted, assertNonNull, TurnkeyClient, TActivityResponse } from "@turnkey/http"
2
2
  import { Address, hashMessage, hashTypedData, SignableMessage, toHex, TypedData, TypedDataDefinition } from "viem";
3
- import { decodeAttestationObject, decodeClientDataJSON } from "@simplewebauthn/server/helpers";
3
+ import { decodeAttestationObject, decodeClientDataJSON, parseAuthenticatorData } from "@simplewebauthn/server/helpers";
4
4
  import { base64UrlToBuffer } from "../utils.js";
5
5
  import { WebAuthnSignature } from "../../types.js";
6
6
 
@@ -78,3 +78,91 @@ export const _signTypedDataWithTurnkey = async <
78
78
  s: BigInt(signature.s)
79
79
  }
80
80
  }
81
+
82
+ export const _stampAndSignMessageWithTurnkey = async (client: TurnkeyClient, organizationId: string, signWith: Address, message: SignableMessage): Promise<WebAuthnSignature> => {
83
+
84
+ const payload = hashMessage(message);
85
+
86
+ const signedRequest = await client.stampSignRawPayload({
87
+ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2",
88
+ organizationId: organizationId,
89
+ parameters: {
90
+ signWith: signWith,
91
+ payload: payload,
92
+ encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
93
+ hashFunction: "HASH_FUNCTION_NO_OP",
94
+ },
95
+ timestampMs: String(Date.now()), // millisecond timestamp
96
+ });
97
+
98
+ if (!signedRequest) throw new Error('Error: Failed to get signed request')
99
+
100
+ const stampedHeaderValue = JSON.parse(signedRequest.stamp.stampHeaderValue);
101
+
102
+ const response:TActivityResponse = await client.request('/public/v1/submit/sign_raw_payload', signedRequest)
103
+
104
+ if (!response || !response.activity) throw new Error('Error: Failed to sign payload');
105
+
106
+ const activity = response.activity;
107
+
108
+ assertActivityCompleted(activity);
109
+
110
+ const authenticatorData = base64UrlToBuffer(stampedHeaderValue.authenticatorData);
111
+ const decodedClientDataJson = decodeClientDataJSON(stampedHeaderValue.clientDataJson);
112
+
113
+ const signature = assertNonNull(activity.result.signRawPayloadResult);
114
+
115
+ return {
116
+ authenticatorData: toHex(authenticatorData),
117
+ clientDataJSON: JSON.stringify(decodedClientDataJson),
118
+ challengeIndex: BigInt(23),
119
+ typeIndex: BigInt(1),
120
+ r: BigInt(signature.r),
121
+ s: BigInt(signature.s)
122
+ }
123
+ }
124
+
125
+ export const _stampAndSignTypedDataWithTurnkey = async <
126
+ const typedData extends TypedData | Record<string, unknown>,
127
+ primaryType extends keyof typedData | "EIP712Domain" = keyof typedData
128
+ > (client: TurnkeyClient, organizationId: string, signWith: Address, typedData: TypedDataDefinition<typedData, primaryType>): Promise<WebAuthnSignature> => {
129
+
130
+ const payload = hashTypedData(typedData);
131
+
132
+ const signedRequest = await client.stampSignRawPayload({
133
+ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2",
134
+ organizationId: organizationId,
135
+ parameters: {
136
+ signWith: signWith,
137
+ payload: payload,
138
+ encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
139
+ hashFunction: "HASH_FUNCTION_NO_OP",
140
+ },
141
+ timestampMs: String(Date.now()), // millisecond timestamp
142
+ });
143
+
144
+ if (!signedRequest) throw new Error('Error: Failed to get signed request')
145
+
146
+ const stampedHeaderValue = JSON.parse(signedRequest.stamp.stampHeaderValue);
147
+
148
+ const response:TActivityResponse = await client.request('/public/v1/submit/sign_raw_payload', signedRequest)
149
+
150
+ if (!response || !response.activity) throw new Error('Error: Failed to sign payload');
151
+
152
+ const activity = response.activity;
153
+
154
+ assertActivityCompleted(activity);
155
+
156
+ const authenticatorData = base64UrlToBuffer(stampedHeaderValue.authenticatorData);
157
+ const decodedClientDataJson = decodeClientDataJSON(stampedHeaderValue.clientDataJson);
158
+ const signature = assertNonNull(activity.result.signRawPayloadResult);
159
+
160
+ return {
161
+ authenticatorData: toHex(authenticatorData),
162
+ clientDataJSON: JSON.stringify(decodedClientDataJson),
163
+ challengeIndex: BigInt(23),
164
+ typeIndex: BigInt(1),
165
+ r: BigInt(signature.r),
166
+ s: BigInt(signature.s)
167
+ }
168
+ }