Giter Club home page Giter Club logo

prf-wallet's Introduction

Overview

This is a basice Vite application based off the PRF Explainer that shows the basics of creating a seed from a discoverable Passkey/PublicKeyCredential

Get started

Clone the repository

git clone [email protected]:awesome-algorand/prf-wallet.git

Install Dependencies

npm install

Run the Demo

npm run dev

Full Example

import './style.css'
import typescriptLogo from './typescript.svg'
import viteLogo from '/vite.svg'
import algosdk from 'algosdk'
document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
  <div>
    <a href="https://vitejs.dev" target="_blank">
      <img src="${viteLogo}" class="logo" alt="Vite logo" />
    </a>
    <a href="https://www.typescriptlang.org/" target="_blank">
      <img src="${typescriptLogo}" class="logo vanilla" alt="TypeScript logo" />
    </a>
    <h1>Vite + TypeScript</h1>
    <div class="card">
        <button id="create" type="button">create</button>
        <button id="get" type="button">get</button>
    </div>
    <p class="read-the-docs">
      Click on the Vite and TypeScript logos to learn more
    </p>
    
  </div>
`

function setupCreate(button: HTMLButtonElement){
    button.addEventListener('click', async () => {
        const credential = await navigator.credentials.create({
            publicKey: {
                challenge: new Uint8Array(32),
                pubKeyCredParams: [
                    { type: 'public-key', alg: -7 },
                    { type: 'public-key', alg: -8 },
                    { type: 'public-key', alg: -257 },
                ],
                rp: {
                    id: 'localhost',
                    name: 'Example',
                },
                extensions: {
                    prf: {},
                },
                authenticatorSelection: {
                    authenticatorAttachment: "cross-platform",
                    userVerification: 'preferred',
                    requireResidentKey: true,
                    residentKey: 'required',
                },
                user: {
                    id: new Uint8Array(32),
                    name: '',
                    displayName: '',
                }
            }
        })
        if(!credential){
            return
        }
        localStorage['credential'] = JSON.stringify([...new Uint8Array(credential.rawId)])
        const extensions: AuthenticationExtensionsClientOutputs & {prf?: {enabled?: boolean}} = (credential as PublicKeyCredential).getClientExtensionResults();
        if(extensions?.prf?.enabled){
            alert('prf enabled')
        }
    })
}
function setupGet(button: HTMLButtonElement){
    button.addEventListener('click', async () => {
        let rawId = localStorage['credential']
        if (!rawId) {
            return
        } else {
            rawId = new Uint8Array(JSON.parse(rawId))
        }
        const credential = await navigator.credentials.get({
            publicKey: {
                challenge: new Uint8Array(32),
                allowCredentials: [
                    {
                        type: 'public-key',
                        id: rawId,
                    },
                ],
                extensions: {
                    prf: {eval: {first: new TextEncoder().encode("Foo encryption key")}}
                },
                userVerification: 'preferred',
            }
        })
        if(!credential){
            return
        }
        const results = (credential as PublicKeyCredential).getClientExtensionResults();
        globalThis.mneomnic = algosdk.secretKeyToMnemonic(new Uint8Array(results.prf.results.first))
        globalThis.account = algosdk.mnemonicToSecretKey(globalThis.mneomnic)
        alert(`mneomnic: \n ${globalThis.mneomnic} \n account: ${globalThis.account.addr}`)
    })
}
setupCreate(document.querySelector<HTMLButtonElement>('#create')!)
setupGet(document.querySelector<HTMLButtonElement>('#get')!)

prf-wallet's People

Contributors

phearzero avatar

Watchers

 avatar Al avatar Andrew Funk avatar

Forkers

acfunk

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.