Skip to content

Commit

Permalink
feat(headless): tracking & CDN wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
red8paw committed Dec 17, 2024
1 parent 267bda6 commit dabab39
Show file tree
Hide file tree
Showing 14 changed files with 538 additions and 39 deletions.
2 changes: 2 additions & 0 deletions packages/waypoint/common/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Generated by genversion.
export const version = "3.2.2"
24 changes: 23 additions & 1 deletion packages/waypoint/headless/action/backup-shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CreateBackupParamsSchema,
} from "../proto/backup"
import { Frame, FrameSchema, Type } from "../proto/rpc"
import { createTracker, HeadlessEventName } from "../track/track"
import { bytesToBase64 } from "../utils/convertor"
import { encryptShard } from "./encrypt-shard"
import { getSecretFromShard } from "./get-address"
Expand Down Expand Up @@ -88,7 +89,7 @@ const signChallenge = (signChallenge: Uint8Array, secret: Uint8Array) => {
}
}

export const backupShard = async (params: BackupShardParams): Promise<string> => {
const _backupShard = async (params: BackupShardParams): Promise<string> => {
const {
waypointToken,
clientShard,
Expand Down Expand Up @@ -134,3 +135,24 @@ export const backupShard = async (params: BackupShardParams): Promise<string> =>
socket.close()
}
}

export const backupShard = async (params: BackupShardParams): Promise<string> => {
const { recoveryPassword, waypointToken, wsUrl } = params
const tracker = createTracker({
event: HeadlessEventName.backupShard,
waypointToken,
productionFactor: wsUrl,
})

try {
const result = await _backupShard(params)
tracker.trackOk({
request: { isWeakPassword: recoveryPassword === "" },
})

return result
} catch (error) {
tracker.trackError(error)
throw error
}
}
23 changes: 22 additions & 1 deletion packages/waypoint/headless/action/keygen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HeadlessClientError, HeadlessClientErrorCode } from "../error/client"
import { createTracker, HeadlessEventName } from "../track/track"
import { bytesToJson } from "../utils/convertor"
import { type ActionHandler, type KeygenHandlerDoResponse } from "../wasm/types"
import { sendAuthenticate, toAuthenticateData } from "./helpers/authenticate"
Expand Down Expand Up @@ -33,7 +34,7 @@ export type KeygenParams = {
wsUrl: string
}

export const keygen = async (params: KeygenParams): Promise<string> => {
const _keygen = async (params: KeygenParams): Promise<string> => {
const {
waypointToken,

Expand Down Expand Up @@ -88,3 +89,23 @@ export const keygen = async (params: KeygenParams): Promise<string> => {
socket.close()
}
}

export const keygen = async (params: KeygenParams): Promise<string> => {
const { waypointToken, wasmUrl, wsUrl } = params
const tracker = createTracker({
event: HeadlessEventName.keygen,
waypointToken,
productionFactor: wsUrl,
wasmUrl,
})

try {
const result = await _keygen(params)
tracker.trackOk({})

return result
} catch (error) {
tracker.trackError(error)
throw error
}
}
48 changes: 33 additions & 15 deletions packages/waypoint/headless/action/personal-sign.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Hex, type SignableMessage, toPrefixedMessage, verifyMessage } from "viem"

import { HeadlessClientError, HeadlessClientErrorCode } from "../error/client"
import { createTracker, HeadlessEventName } from "../track/track"
import { getAddressFromShard } from "./get-address"
import { _sign } from "./sign"

Expand All @@ -15,21 +16,38 @@ export type PersonalSignParams = {
}

export const personalSign = async (params: PersonalSignParams): Promise<Hex> => {
const { message, ...restParams } = params
const address = getAddressFromShard(params.clientShard)
const prefixedMessage = toPrefixedMessage(message)

const signature = await _sign({ ...restParams, rawMessage: prefixedMessage })
const isValid = await verifyMessage({
address: address,
message,
signature,
const tracker = createTracker({
event: HeadlessEventName.personalSign,
waypointToken: params.waypointToken,
productionFactor: params.wsUrl,
wasmUrl: params.wasmUrl,
})

if (isValid) return signature
throw new HeadlessClientError({
cause: undefined,
code: HeadlessClientErrorCode.InvalidSignatureError,
message: `Unable to verify the signature="${signature}" with the given address="${address}".`,
})
try {
const { message, ...restParams } = params
const address = getAddressFromShard(params.clientShard)
const prefixedMessage = toPrefixedMessage(message)

const signature = await _sign({ ...restParams, rawMessage: prefixedMessage })
const isValid = await verifyMessage({
address: address,
message,
signature,
})

if (!isValid)
throw new HeadlessClientError({
cause: undefined,
code: HeadlessClientErrorCode.InvalidSignatureError,
message: `Unable to verify the signature="${signature}" with the given address="${address}".`,
})

tracker.trackOk({
request: { message },
})
return signature
} catch (error) {
tracker.trackError(error)
throw error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { keccak256 } from "viem"
import { HeadlessClientError, HeadlessClientErrorCode } from "../../error/client"
import { toServerError } from "../../error/server"
import { Type } from "../../proto/rpc"
import { createTracker, HeadlessEventName } from "../../track/track"
import { getAddressFromShard } from "../get-address"
import { sendAuthenticate, toAuthenticateData } from "../helpers/authenticate"
import { wasmGetSignHandler } from "../helpers/get-sign-handler"
Expand All @@ -19,7 +20,7 @@ import { serializeLegacyTransaction, toTransactionInServerFormat } from "./prepa
import { sendTransactionRequest } from "./send-tx-request"
import { toTxHash } from "./to-tx-hash"

export const sendLegacyTransaction = async (
const _sendLegacyTransaction = async (
params: SendTransactionParams,
): Promise<SendTransactionResult> => {
const {
Expand Down Expand Up @@ -121,3 +122,29 @@ export const sendLegacyTransaction = async (
socket.close()
}
}

export const sendLegacyTransaction = async (
params: SendTransactionParams,
): Promise<SendTransactionResult> => {
const { chain, transaction, wasmUrl, waypointToken, wsUrl } = params
const tracker = createTracker({
event: HeadlessEventName.sendLegacyTransaction,
waypointToken,
productionFactor: wsUrl,
wasmUrl,
})

try {
const result = await _sendLegacyTransaction(params)
const { txHash } = result
tracker.trackOk({
request: { transaction, chain },
response: { txHash },
})

return result
} catch (error) {
tracker.trackError(error)
throw error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { HeadlessClientError, HeadlessClientErrorCode } from "../../error/client
import { toServerError } from "../../error/server"
import { MessageSchema } from "../../proto/message"
import { Frame, Type } from "../../proto/rpc"
import { createTracker, HeadlessEventName } from "../../track/track"
import { getAddressFromShard } from "../get-address"
import { sendAuthenticate, toAuthenticateData } from "../helpers/authenticate"
import { wasmGetSignHandler } from "../helpers/get-sign-handler"
Expand Down Expand Up @@ -38,7 +39,7 @@ export const toSerializedSponsoredTransaction = (frame: Frame): Uint8Array => {
}
}

export const sendSponsoredTransaction = async (
const _sendSponsoredTransaction = async (
params: SendTransactionParams,
): Promise<SendTransactionResult> => {
const {
Expand Down Expand Up @@ -141,3 +142,29 @@ export const sendSponsoredTransaction = async (
socket.close()
}
}

export const sendSponsoredTransaction = async (
params: SendTransactionParams,
): Promise<SendTransactionResult> => {
const { chain, transaction, wasmUrl, waypointToken, wsUrl } = params
const tracker = createTracker({
event: HeadlessEventName.sendSponsoredTransaction,
waypointToken,
productionFactor: wsUrl,
wasmUrl,
})

try {
const result = await _sendSponsoredTransaction(params)
const { txHash } = result
tracker.trackOk({
request: { transaction, chain },
response: { txHash },
})

return result
} catch (error) {
tracker.trackError(error)
throw error
}
}
48 changes: 33 additions & 15 deletions packages/waypoint/headless/action/sign-typed-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Hex, type TypedDataDefinition, verifyTypedData } from "viem"

import { HeadlessClientError, HeadlessClientErrorCode } from "../error/client"
import { createTracker, HeadlessEventName } from "../track/track"
import { getAddressFromShard } from "./get-address"
import { prepareTypedData } from "./helpers/prepare-typed-data"
import { _sign } from "./sign"
Expand All @@ -16,21 +17,38 @@ export type SignTypedDataParams = {
}

export const signTypedData = async (params: SignTypedDataParams): Promise<Hex> => {
const { typedData, ...restParams } = params
const address = getAddressFromShard(params.clientShard)
const rawMessage = prepareTypedData(typedData)

const signature = await _sign({ ...restParams, rawMessage: rawMessage })
const isValid = await verifyTypedData({
...typedData,
address: address,
signature,
const tracker = createTracker({
event: HeadlessEventName.signTypedData,
waypointToken: params.waypointToken,
productionFactor: params.wsUrl,
wasmUrl: params.wasmUrl,
})

if (isValid) return signature
throw new HeadlessClientError({
cause: undefined,
code: HeadlessClientErrorCode.InvalidSignatureError,
message: `Unable to verify the signature="${signature}" with the given address="${address}".`,
})
try {
const { typedData, ...restParams } = params
const address = getAddressFromShard(params.clientShard)
const rawMessage = prepareTypedData(typedData)

const signature = await _sign({ ...restParams, rawMessage: rawMessage })
const isValid = await verifyTypedData({
...typedData,
address: address,
signature,
})

if (!isValid)
throw new HeadlessClientError({
cause: undefined,
code: HeadlessClientErrorCode.InvalidSignatureError,
message: `Unable to verify the signature="${signature}" with the given address="${address}".`,
})

tracker.trackOk({
request: { typedData },
})
return signature
} catch (error) {
tracker.trackError(error)
throw error
}
}
2 changes: 2 additions & 0 deletions packages/waypoint/headless/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ export {
} from "./error/client"
export { ServerError, type ServerErrorType } from "./error/server"

// utils
export { validateToken } from "./utils/token"
export { WASM_URL } from "./wasm/cdn"
Loading

0 comments on commit dabab39

Please sign in to comment.