-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cff4b5
commit de4f1de
Showing
14 changed files
with
382 additions
and
15 deletions.
There are no files selected for viewing
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
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,7 @@ | ||
import packageJson from '../../package.json' | ||
|
||
function getProtocolKitVersion(): string { | ||
return packageJson.version | ||
} | ||
|
||
export default getProtocolKitVersion |
57 changes: 57 additions & 0 deletions
57
packages/protocol-kit/src/utils/on-chain-tracking/generateOnChainIdentifier.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,57 @@ | ||
import { keccak256, toHex } from 'viem' | ||
|
||
/** | ||
* Generates a hash from the given input string and truncates it to the specified size. | ||
* | ||
* @param {string} input - The input string to be hashed. | ||
* @param {number} size - The number of bytes to take from the end of the hash. | ||
* @returns {string} A hexadecimal string representation of the truncated hash, without the `0x` prefix. | ||
*/ | ||
export function generateHash(input: string, size: number): string { | ||
const fullHash = keccak256(toHex(input)) | ||
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 hashed metadata such as the project name, platform, tool, and tool version. | ||
* | ||
* @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, | ||
platform = 'Web', | ||
tool, | ||
toolVersion | ||
}: OnChainIdentifierParamsType): string { | ||
const identifierPrefix = '5afe' | ||
const identifierVersion = '00' // first version | ||
const projectHash = generateHash(project, 20) // Take the last 20 bytes | ||
const platformHash = generateHash(platform, 3) // Take the last 3 bytes | ||
const toolHash = generateHash(tool, 3) // Take the last 3 bytes | ||
const toolVersionHash = generateHash(toolVersion, 3) // Take the last 3 bytes | ||
|
||
return `${identifierPrefix}${identifierVersion}${projectHash}${platformHash}${toolHash}${toolVersionHash}` | ||
} | ||
|
||
export default generateOnChainIdentifier |
Oops, something went wrong.