Skip to content

Commit

Permalink
Updated generateOnChainIdentifier to accept an object and addressed P…
Browse files Browse the repository at this point in the history
…R feedback.
  • Loading branch information
DaniSomoza committed Dec 10, 2024
1 parent 851e75e commit a80d468
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 36 deletions.
14 changes: 8 additions & 6 deletions packages/protocol-kit/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import { Hash, Hex, SendTransactionParameters } from 'viem'
import getPasskeyOwnerAddress from './utils/passkeys/getPasskeyOwnerAddress'
import createPasskeyDeploymentTransaction from './utils/passkeys/createPasskeyDeploymentTransaction'
import generateOnChainIdentifier from './utils/on-chain-tracking/generateOnChainIdentifier'
import getProtocolKitVersion from './utils/getProtocolKitVersion'

const EQ_OR_GT_1_4_1 = '>=1.4.1'
const EQ_OR_GT_1_3_0 = '>=1.3.0'
Expand Down Expand Up @@ -134,12 +135,13 @@ class Safe {
const { provider, signer, isL1SafeSingleton, contractNetworks, onchainAnalitics } = config

if (onchainAnalitics?.project) {
this.#onchainIdentifier = generateOnChainIdentifier(
onchainAnalitics.project,
onchainAnalitics.platform,
'protocol-kit',
'5.0.4'
)
const { project, platform } = onchainAnalitics
this.#onchainIdentifier = generateOnChainIdentifier({
project,
platform,
tool: 'protocol-kit',
toolVersion: getProtocolKitVersion()
})
}

this.#safeProvider = await SafeProvider.init({
Expand Down
7 changes: 7 additions & 0 deletions packages/protocol-kit/src/utils/getProtocolKitVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import packageJson from '../../package.json'

function getProtocolKitVersion(): string {
return packageJson.version
}

export default getProtocolKitVersion
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,38 @@ export function generateHash(input: string, size: number): string {
return toHex(fullHash.slice(-size)).replace('0x', '') // Take the last X bytes
}

export type OnChainIdentifierParamsType = {
project: string
platform?: string
tool: string
toolVersion: string
}

/**
* Generates an on-chain identifier for tracking transactions on the blockchain.
* This identifier includes hased metadata such as the project name, platform, tool, and tool version.
* This identifier includes hashed metadata such as the project name, platform, tool, and tool version.
*
* @param {string} project - The name of the project initiating the transaction.
* @param {string} [platform='Web'] - The platform from which the transaction originates (e.g., "Web", "Mobile", "Safe App", "Widget"...).
* @param {string} tool - The tool used to generate the transaction (e.g., "protocol-kit").
* @param {string} toolVersion - The version of the tool used to generate the transaction.
* @param {Object} params - An object containing the metadata for generating the on-chain identifier.
* @param {string} params.project - The name of the project initiating the transaction.
* @param {string} [params.platform='Web'] - The platform from which the transaction originates (e.g., "Web", "Mobile", "Safe App", "Widget"...).
* @param {string} params.tool - The tool used to generate the transaction (e.g., "protocol-kit").
* @param {string} params.toolVersion - The version of the tool used to generate the transaction.
* @returns {string} A string representing the on-chain identifier, composed of multiple hashed segments.
*
* @example
* const identifier = generateOnChainIdentifier({
* project: 'MyProject',
* platform: 'Mobile',
* tool: 'protocol-kit',
* toolVersion: '4.0.0'
* })
*/
function generateOnChainIdentifier(
project: string,
platform: string = 'Web',
tool: string,
toolVersion: string
): string {
function generateOnChainIdentifier({
project,
platform = 'Web',
tool,
toolVersion
}: OnChainIdentifierParamsType): string {
const identifierPrefix = '5afe'
const identifierVersion = '00' // first version
const projectHash = generateHash(project, 20) // Take the last 20 bytes
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-kit/tests/e2e/onChainIdentifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('On-chain analytics', () => {
const tool = 'protocol-kit'
const toolVersion = '1.0.0'

const onChainIdentifier = generateOnChainIdentifier(project, platform, tool, toolVersion)
const onChainIdentifier = generateOnChainIdentifier({ project, platform, tool, toolVersion })

const identifierPrefix = '5afe'
const identifierVersion = '00'
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-kit/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"composite": true,
"outDir": "dist"
},
"include": ["src/**/*"]
"include": ["src/**/*", "package.json"]
}
2 changes: 1 addition & 1 deletion packages/protocol-kit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"composite": true,
"outDir": "dist"
},
"include": ["src/**/*", "tests/**/*", "hardhat/**/*", "hardhat.config.ts"]
"include": ["package.json", "src/**/*", "tests/**/*", "hardhat/**/*", "hardhat.config.ts"]
}
14 changes: 8 additions & 6 deletions packages/relay-kit/src/packs/safe-4337/Safe4337Pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
} from './utils'
import { entryPointToSafeModules, EQ_OR_GT_0_3_0 } from './utils/entrypoint'
import { PimlicoFeeEstimator } from './estimators/PimlicoFeeEstimator'
import getRelayKitVersion from './utils/getRelayKitVersion'

const MAX_ERC20_AMOUNT_TO_APPROVE =
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn
Expand Down Expand Up @@ -117,12 +118,13 @@ export class Safe4337Pack extends RelayKitBasePack<{
this.#SAFE_WEBAUTHN_SHARED_SIGNER_ADDRESS = safeWebAuthnSharedSignerAddress || '0x'

if (onchainAnalitics?.project) {
this.#onchainIdentifier = generateOnChainIdentifier(
onchainAnalitics.project,
onchainAnalitics.platform,
'relay-kit',
'3.2.4'
)
const { project, platform } = onchainAnalitics
this.#onchainIdentifier = generateOnChainIdentifier({
project,
platform,
tool: 'relay-kit',
toolVersion: getRelayKitVersion()
})
}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/relay-kit/src/packs/safe-4337/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export type Safe4337InitOptions = {
}
options: ExistingSafeOptions | PredictedSafeOptions
paymasterOptions?: PaymasterOptions
// on-chain analitics
onchainAnalitics?: OnchainAnaliticsProps
}

Expand All @@ -67,7 +66,6 @@ export type Safe4337Options = {
entryPointAddress: string
safe4337ModuleAddress: string
safeWebAuthnSharedSignerAddress?: string
// on-chain analitics
onchainAnalitics?: OnchainAnaliticsProps
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import packageJson from '../../../../package.json'

function getRelayKitVersion(): string {
return packageJson.version
}

export default getRelayKitVersion
2 changes: 1 addition & 1 deletion packages/relay-kit/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"composite": true,
"outDir": "dist"
},
"include": ["src/**/*"],
"include": ["src/**/*", "package.json"],
"exclude": ["src/**/*.test.ts", "src/**/*.test-d.ts"]
}
2 changes: 1 addition & 1 deletion packages/relay-kit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"composite": true,
"outDir": "dist"
},
"include": ["src/**/*"]
"include": ["src/**/*", "package.json"]
}
7 changes: 1 addition & 6 deletions playground/protocol-kit/deploy-safe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as dotenv from 'dotenv'
import Safe, { SafeAccountConfig, getSafeAddressFromDeploymentTx } from '@safe-global/protocol-kit'
import { SafeVersion } from '@safe-global/types-kit'

Expand All @@ -8,10 +7,6 @@ import { sepolia } from 'viem/chains'
import { waitForTransactionReceipt } from 'viem/actions'
import semverSatisfies from 'semver/functions/satisfies'

dotenv.config()

const { SIGNER_ADDRESS_PRIVATE_KEY } = process.env

// This file can be used to play around with the Safe Core SDK

interface Config {
Expand All @@ -27,7 +22,7 @@ interface Config {

const config: Config = {
RPC_URL: sepolia.rpcUrls.default.http[0],
DEPLOYER_ADDRESS_PRIVATE_KEY: SIGNER_ADDRESS_PRIVATE_KEY!,
DEPLOYER_ADDRESS_PRIVATE_KEY: '<DEPLOYER_ADDRESS_PRIVATE_KEY>',
DEPLOY_SAFE: {
OWNERS: ['OWNER_ADDRESS'],
THRESHOLD: 1, // <SAFE_THRESHOLD>
Expand Down

0 comments on commit a80d468

Please sign in to comment.