Skip to content

Commit

Permalink
Add a script to create a dummy anchor (#110)
Browse files Browse the repository at this point in the history
# Motivation

Going through the steps of creating an anchor and confirming the backup
device and seed phrase gets tedious and slows down e2e tests.

# Changes

1. `bin/dfx-ii-create-dummy-anchor` script which adds a dummy identity
anchor.
2. Call the script at the end of `bin/dfx-snsdemo`.
3. `bin/dfx-nns-import` which makes necessary change to dfx.json to be
able to call internet_identity and exports
declarations/internet_identity.did
4. Call `bin/dfx-nns-import` from `bin/dfx-snsdemo` right after it calls
`dfx nns import`.

# Testing

Ran the script manually and confirmed that I could sign in by using
existing anchor 10000.

---------

Co-authored-by: Formatting Committer <[email protected]>
Co-authored-by: Max <[email protected]>
Co-authored-by: Max Murphy-Skvorzov <[email protected]>
  • Loading branch information
4 people authored Mar 31, 2023
1 parent 2e99193 commit 5937eeb
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 0 deletions.
64 changes: 64 additions & 0 deletions bin/dfx-ii-create-dummy-anchor
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

set -euo pipefail

SOURCE_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"

print_help() {
cat <<-EOF
Creates a dummy internet identity with a registered backup device and seed
phrase. This makes it easy to sign in with the fewer number of clicks.
EOF
}

# Source the clap.bash file ---------------------------------------------------
source "$SOURCE_DIR/clap.bash"
# Define options
clap.define short=n long=network desc="The dfx network to use" variable=DFX_NETWORK default="local"
clap.define short=i long=identity desc="The dfx identity to use" variable=DFX_IDENTITY default="$(dfx identity whoami)"
# Source the output file ----------------------------------------------------------
source "$(clap.build)"

IDENTITY_PUB_KEY_FILE="$(mktemp)"

clean_up() {
rm "$IDENTITY_PUB_KEY_FILE"
}

trap clean_up EXIT

DECLARATIONS_DIR="$(realpath "$SOURCE_DIR/../declarations")"
II_DID_FILE="$DECLARATIONS_DIR/internet_identity.did"

dfx identity export "$DFX_IDENTITY" | openssl ec -out "$IDENTITY_PUB_KEY_FILE" -pubout -outform der 2>/dev/null

echo "Fetching captcha."
CHALLENGE_KEY="$(dfx canister call internet_identity create_challenge --candid "$II_DID_FILE" --network "$DFX_NETWORK" | idl2json | jq -r .challenge_key)"

PUB_KEY_BLOB=$(hexdump -ve '1/1 "%.2x"' "$IDENTITY_PUB_KEY_FILE" | sed 's/../\\&/g')

echo "Creating anchor."
ANCHOR="$(dfx canister call internet_identity register "(record{pubkey=blob \"${PUB_KEY_BLOB}\";alias=\"dfx test key\";purpose=variant{authentication};key_type=variant{unknown};protection=variant{unprotected};},record{key=\"${CHALLENGE_KEY}\";chars=\"a\"})" --candid "$II_DID_FILE" --network "$DFX_NETWORK" | idl2json | jq -r .registered.user_number)"

# This matches the dummy key pair generated by Internet Identity here:
# https://github.com/dfinity/internet-identity/blob/1ce860dd0601312f6521d433c4e273215a1abc5d/src/frontend/src/utils/iiConnection.ts#L57
DUMMY_PUB_KEY_BLOB='0*0\05\06\03+ep\03!\00;j\27\bc\ce\b6\a4-b\a3\a8\d0*o\0dse2\15w\1d\e2C\a6:\c0H\a1\8bY\da)'

# This is arbitrary as long as it's different from the public key above.
# We won't actually use the seed phrase. We just want to skip the warning that
# we don't have a key phrase.
SEED_PHRASE_PUB_KEY_BLOB='00000000000000000000000000000000000000000000'

add_device() {
DEVICE_PUB_KEY_BLOB="$1"
DEVICE_KEY_TYPE="$2"
DEVICE_PURPOSE="$3"
set dfx canister call internet_identity add "(${ANCHOR}, record { alias = \"\"; protection = variant { unprotected }; pubkey = blob \"${DEVICE_PUB_KEY_BLOB}\"; key_type = variant { ${DEVICE_KEY_TYPE} }; purpose = variant { ${DEVICE_PURPOSE} }; })" --candid "$II_DID_FILE" --network "$DFX_NETWORK"
"$@" >/dev/null
}

echo "Adding backup device for anchor $ANCHOR."
add_device "$DUMMY_PUB_KEY_BLOB" unknown authentication
echo "Adding seed phrase for anchor $ANCHOR."
add_device "$SEED_PHRASE_PUB_KEY_BLOB" seed_phrase recovery
Loading

0 comments on commit 5937eeb

Please sign in to comment.