#!/bin/zsh

source config
NAME=$1
OPERATION=$2
INVITE_ARID=$3

REGISTRY=demo/$NAME/registry.json

dkg() {
    section "🔑 Retrive invite for $NAME's DKG group from Hubert '$STORAGE'..."

    INVITE=$(\
        frost dkg participant receive \
        --info \
        --storage $STORAGE \
        --timeout $TIMEOUT \
        --registry "$REGISTRY" \
        "${INVITE_ARID}"
    )

    section "🔑 Post round 1 response for $NAME's DKG group..."

    frost dkg participant round1 \
        --storage $STORAGE \
        --registry "$REGISTRY" \
        "${INVITE_ARID}"

    section "🔑 Fetch Round 2 request, run FROST DKG part2 with Round 1 secret and all Round 1 packages, post Round 2 packages..."

    GROUP_ID=$(jq -r '.groups | keys[0]' $REGISTRY)

    frost dkg participant round2 \
        --storage $STORAGE \
        --timeout $TIMEOUT \
        --registry "$REGISTRY" \
        "${GROUP_ID}"

    section "🔑 Finalize DKG for $NAME's DKG group on Hubert '$STORAGE'..."

    frost dkg participant finalize \
        --storage $STORAGE \
        --timeout $TIMEOUT \
        --registry "$REGISTRY" \
        "${GROUP_ID}"

    section "🔑 DKG Complete!"
}

sign() {
    section "📝 Retrieve signing invite for $NAME from Hubert '$STORAGE'..."

    INFO=$(frost sign participant receive \
        --info \
        --storage $STORAGE \
        --timeout $TIMEOUT \
        --registry "$REGISTRY" \
        "${INVITE_ARID}" \
    )
    echo $INFO

    SESSION_ID=$(echo "$INFO" | tail -n 1)

    section "📝 Post Round 1 response for $NAME..."

    frost sign participant round1 \
        --storage $STORAGE \
        --registry "$REGISTRY" \
        "${SESSION_ID}"

    section "📝 Fetch Round 2 request and post signature share..."

    frost sign participant round2 \
        --storage $STORAGE \
        --timeout $TIMEOUT \
        --registry "$REGISTRY" \
        "${SESSION_ID}"

    section "📝 Finalize and attach aggregated signature..."

    frost sign participant finalize \
        --storage $STORAGE \
        --timeout $TIMEOUT \
        --registry "$REGISTRY" \
        "${SESSION_ID}"

    section "📝 Signing Complete!"
}

case "$OPERATION" in
    dkg)
        dkg
        ;;
    sign)
        sign
        ;;
    *)
        echo "Error: Invalid operation '$OPERATION'. Use 'dkg' or 'sign'." >&2
        exit 1
        ;;
esac
