Skip to content

Feature request: allow hashing functions to hash other Clarity types #2693

@MarvinJanssen

Description

@MarvinJanssen

Is your feature request related to a problem? Please describe.
The ability to hash any Clarity value could open up a lot of DeFi/DeX potential. This suggestion is to have a counterpart to Ethereum's EIP712. The hashes can be signed by the user and then passed to off-chains apps to be used in contract calls later.

Some use-cases:

  • Simple address ownership proofs without having to hit the chain.
  • Signing orders for off-chain order books.
  • Creation of pseudo-transactions that can be stored and broadcast later without having to worry about nonces.
  • Limitless signature fun.

Describe the solution you'd like
CVs could just be serialised to the SIP wire format and then hashed. We might also want to add the chain ID to prevent collisions across forks. A super quick example can be found in this branch: https://github.com/MarvinJanssen/clarity-repl/tree/feat/hash-structured-data.

Working examples:

Javascript:

const {
	listCV,
	uintCV,
	tupleCV,
	bufferCVFromString,
	serializeCV
	} = require('@stacks/transactions');

const {createHash} = require('crypto');

const sha256CV = input => createHash('sha256').update(serializeCV(input)).digest('hex');

let list = listCV([uintCV(3), uintCV(4), uintCV(5)]);

console.log(sha256CV(list)); // -> 6f3a852f0d216d0012c109b67fd021e34a8873d4c788c69ea79926562d38e1e0

let tuple = tupleCV({num: uintCV(5), buff: bufferCVFromString("buffer string")});

console.log(sha256CV(tuple)); // -> 9aa9471d690ba4844b1282ae8c9387b175557c71f61293660f4a72bd2361299e

Clarity:

(sha256 (list u3 u4 u5))
;; -> 0x6f3a852f0d216d0012c109b67fd021e34a8873d4c788c69ea79926562d38e1e0

(sha256 {num: u5, buff: 0x62756666657220737472696e67})
;; -> 0x9aa9471d690ba4844b1282ae8c9387b175557c71f61293660f4a72bd2361299e

buff, uint, and int types remain as is to be backwards compatible.

Additional context
Provides a solution to: leather-io/extension#1051

This is what signing CVs could look like in the Web Extension:
sign structured data concept

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions