Skip to content

Commit

Permalink
Merge pull request #957 from hirosystems/develop
Browse files Browse the repository at this point in the history
add new recipes
  • Loading branch information
ryanwaits authored Feb 10, 2025
2 parents 859a411 + cf3bd94 commit 84b8a36
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions content/_recipes/create-sha256-hash-clarity.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Create a SHA-256 hash using Clarity

A scripts in Clarity that creates a sha256 hash
that can be created in javascript as well.

## Use cases

- validate a secret vote

## Example usage

```typescript
const hash = createSha256Hash("deadbeef"); //b462657d5547c972e675cfd7ee2e313eb033e6aa74f668ca7bd1d79a056c9fea
```

```clarity
;; vote
(define-map votes principal (buff 32))
(map-set votes tx-sender 0xb462657d5547c972e675cfd7ee2e313eb033e6aa74f668ca7bd1d79a056c9fea)
;; validate vote
(define-read-only (validate-vote (vote (buff 8)))
(is-eq (map-get? votes tx-sender) (some (create-sha256-hash vote))))
(validate-vote 0xdeadbeef)
```

## Resources

**Recipes**

- [Create a SHA-256 hash using Stacks.js](/cookbook/create-sha256-hash-stacks-js)
33 changes: 33 additions & 0 deletions content/_recipes/create-sha256-hash-stacks-js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Create a SHA-256 hash using Stacks.js

A scripts in javascript that creates a sha256 hash
that can be created in Clarity as well.

## Use cases

- validate a secret vote

## Example usage

```typescript
const hash = createSha256Hash("deadbeef"); //b462657d5547c972e675cfd7ee2e313eb033e6aa74f668ca7bd1d79a056c9fea
```

```clarity
;; vote
(define-map votes principal (buff 32))
(map-set votes tx-sender 0xb462657d5547c972e675cfd7ee2e313eb033e6aa74f668ca7bd1d79a056c9fea)
;; validate vote
(define-read-only (validate-vote (vote (buff 8)))
(is-eq (map-get? votes tx-sender) (some (create-sha256-hash vote))))
(validate-vote 0xdeadbeef)
```

## Resources

**Recipes**

- [Create a SHA-256 hash using Clarity](/cookbook/create-sha256-hash-clarity)

0 comments on commit 84b8a36

Please sign in to comment.