Skip to content

Instantly share code, notes, and snippets.

@caike
caike / db-sync.sql
Last active November 21, 2023 15:27
Query all known addresses for all active delegators of a Cardano Stake Pool
View db-sync.sql
SELECT DISTINCT address
FROM utxo_view WHERE stake_address_id IN (
SELECT DISTINCT sa.id
FROM stake_address sa
JOIN (
SELECT d.addr_id, MAX(d.active_epoch_no) AS max_active_epoch_no
FROM delegation d
GROUP BY d.addr_id
) max_epochs ON sa.id = max_epochs.addr_id
JOIN stake_registration sr ON (sr.addr_id = sa.id)
@caike
caike / script.tsx
Last active August 29, 2023 14:31
Lucid and CIP-68
View script.tsx
import type { NextPage } from 'next'
import Head from 'next/head'
import WalletConnect from '../components/WalletConnect'
import { useStoreActions, useStoreState } from "../utils/store"
import Link from 'next/link'
import { useState, useEffect } from 'react'
import { getAssets } from "../utils/cardano";
import NftGrid from "../components/NftGrid";
import initLucid from '../utils/lucid'
import { Lucid, Credential, TxHash, Lovelace, Constr, SpendingValidator, Data, fromText, Unit, MintingPolicy, PolicyId, Address, UTxO, applyParamsToScript, Assets, ScriptHash, Redeemer, paymentCredentialOf, KeyHash, generatePrivateKey, getAddressDetails, toUnit } from 'lucid-cardano'
@caike
caike / emulate_always_succeeds.ts
Last active July 14, 2023 16:05
Lucid Emulator
View emulate_always_succeeds.ts
// $ deno run -A always_succeeds.ts
import {
Address,
Data,
Emulator,
generatePrivateKey,
Lovelace,
Lucid,
PrivateKey,
SpendingValidator,
View seed-to-keys.md

From seed phrase to keys:

cat recovery_phrase.txt | cardano-wallet key from-recovery-phrase Shelley > root.prv

cat root.prv | cardano-address key inspect

cardano-wallet key child 1852H/1815H/0H/0/0 < root.prv > payment.prv

cardano-cli key convert-cardano-address-key --shelley-payment-key
@caike
caike / calculate.sh
Created April 15, 2023 14:13
Script for calculating time left for current Cardano epoch
View calculate.sh
#!/bin/bash
getEpoch(){
CCLI=$(which cardano-cli)
"$CCLI" query tip --mainnet | sed -n '3 p'| sed 's/[^0-9]*//g'
}
# Description : Calculation of days, hours, minutes and seconds from time in seconds
timeLeft() {
local T=$1
@caike
caike / run.exs
Created March 29, 2023 13:28
Elixir Protocols in Practice
View run.exs
##########################################
# Example of using Protocols in Elixir
#
# cat .tool-versions
# elixir 1.14.3
# erlang 24.3.4
#
# Run via: elixir run.exs
#
############################
@caike
caike / validator.hs
Created December 16, 2022 20:12
Plutus Pioneers Week03 Homework
View validator.hs
{-# INLINABLE mkValidator #-}
-- This should validate if either beneficiary1 has signed the transaction and the current slot is before or at the deadline
-- or if beneficiary2 has signed the transaction and the deadline has passed.
mkValidator :: VestingDatum -> () -> ScriptContext -> Bool
mkValidator datum _ sc = traceIfFalse "cannot grab" (beneficiary1Grab || beneficiary2Grab)
where
info :: TxInfo
info = scriptContextTxInfo sc
beneficiary1Grab :: Bool
@caike
caike / file.sh
Last active November 30, 2022 23:52
Addresses and Keys for Smart Contract demo
View file.sh
cardano-cli address key-gen --verification-key-file 02.vkey --signing-key-file 02.skey
cardano-cli address build \
--payment-verification-key-file 02.vkey \
--out-file 02.addr --testnet-magic 2
# Optional for debugging
## generates key files for staking address
cardano-cli stake-address key-gen \
--verification-key-file 01-stake.vkey \
--signing-key-file 01-stake.skey
@caike
caike / about.md
Last active October 17, 2022 00:27
Node.js streams 1 vs. 2
View about.md

NodeJS Streams

  • Current implementation is known as streams2.
  • Introduced in node v0.10.
  • "suck" streams instead of "spew" streams.
  • Instead of data events spewing, call read() to pull data from source.
  • When there isn't any data to consume, then read() will return undefined.
  • Adding a data event listener will switch the Readable stream into "old mode", where data is emitted as soon as it is available rather than waiting for you to call read() to consume it. This requires you to handle backpressure problems manually.
  • The pipe method helps write less code and handles back-pressure.
  • If you add an end listener and don't ever read() or pipe(), it'll never emit end.
@caike
caike / attack.md
Last active October 2, 2022 14:37
XSS attack demo with innerHTML
View attack.md

Tested with Chrome, Firefox and Safari.

The following code will not trigger an alert. target.innerHTML = "<script> alert('XSS Attack'); </script>";

The following code will trigger an alert. target.innerHTML = "";