From db6ea52411dd078d4fb985b37bc5c03aff26cb00 Mon Sep 17 00:00:00 2001 From: Blake Regalia Date: Sun, 21 May 2023 23:33:34 -0700 Subject: [PATCH 1/9] dev: working --- app/src/env.d.ts | 1 + app/src/main.ts | 35 +++++-- app/vite.config.ts | 33 +++++- applibs/rollup.config.ts | 10 +- applibs/src/{interface.ts => interface.d.ts} | 7 +- applibs/src/storage/main.ts | 42 +++++--- bootloader/rollup.config.ts | 34 ++---- bootloader/src/_autoboot.ts | 94 ++++++++--------- bootloader/src/bootloader.ts | 105 +++++++++++-------- bootloader/src/env.ts | 25 +++++ bootloader/src/global.ts | 53 ---------- build.mjs | 6 +- package.json | 21 ++-- tsconfig.json | 6 +- 14 files changed, 250 insertions(+), 222 deletions(-) create mode 100644 app/src/env.d.ts rename applibs/src/{interface.ts => interface.d.ts} (90%) create mode 100644 bootloader/src/env.ts delete mode 100644 bootloader/src/global.ts diff --git a/app/src/env.d.ts b/app/src/env.d.ts new file mode 100644 index 0000000..be5af80 --- /dev/null +++ b/app/src/env.d.ts @@ -0,0 +1 @@ +/// diff --git a/app/src/main.ts b/app/src/main.ts index 022d33d..663b430 100644 --- a/app/src/main.ts +++ b/app/src/main.ts @@ -1,10 +1,21 @@ -import {create_svg, create_html} from '@nfps.dev/runtime'; +import type {SecretBech32} from '@solar-republic/neutrino'; + +import {base64_to_buffer} from '@blake.regalia/belt'; +import {create_svg, create_html, ls_write, load_script} from '@nfps.dev/runtime'; +import {safe_json, Wallet, gen_sk} from '@solar-republic/neutrino'; // import App from './app.svelte'; import type {} from '../../applibs/src/interface'; -import {safe_json, Wallet, type SecretBech32, gen_sk} from '@solar-republic/neutrino'; -import {base64_to_buffer} from '@blake.regalia/belt'; + +import { + A_TOKEN_LOCATION, + G_QUERY_PERMIT, + K_CONTRACT, + P_LCD, + SH_VIEWING_KEY, + ls_read, +} from 'nfpx:bootloader'; const dm_root = document.documentElement; @@ -30,24 +41,24 @@ dm_pause.onclick = () => { dm_pause.textContent = dm_root.classList.toggle('paused')? 'Resume': 'Pause'; }; -const si_storage_token_owner_addr = 'toa:'+loc.join(':'); +const si_storage_token_owner_addr = 'toa:'+A_TOKEN_LOCATION.join(':'); // fetch token owner address -toa = lsgs(si_storage_token_owner_addr) as SecretBech32 - || lsss(si_storage_token_owner_addr, prompt('Please enter the account address that owns this token') || ''); +const SA_OWNER = ls_read(si_storage_token_owner_addr) as SecretBech32 + || ls_write(si_storage_token_owner_addr, prompt('Please enter the account address that owns this token') || ''); (async() => { // get or create private key - const sh_sk = lsgs('sk'); + const sh_sk = ls_read('sk'); const atu8_sk = sh_sk? base64_to_buffer(sh_sk): gen_sk(); // create wallet - const k_wallet = Wallet(atu8_sk, loc[0], lcd); + const k_wallet = Wallet(atu8_sk, A_TOKEN_LOCATION[0], P_LCD); // fetch library - const dm_script = await load('storage.js', { + const dm_script = await load_script('storage.js', { tag: '1.x', - }, sc, loc, qp || [vk!]); + }, K_CONTRACT, A_TOKEN_LOCATION, G_QUERY_PERMIT || [SH_VIEWING_KEY, SA_OWNER]); // request succeded if(dm_script) { @@ -71,3 +82,7 @@ toa = lsgs(si_storage_token_owner_addr) as SecretBech32 dm_root.append(dm_script); } })(); + +export { + SA_OWNER, +}; diff --git a/app/vite.config.ts b/app/vite.config.ts index 2e24eaa..0659e47 100644 --- a/app/vite.config.ts +++ b/app/vite.config.ts @@ -1,6 +1,10 @@ import {resolve} from 'path'; import {microWeb} from '@nfps.dev/rollup-plugin-microweb'; +import {nfpModule, nfpxWindow} from '@nfps.dev/sdk'; +import {svelte} from '@sveltejs/vite-plugin-svelte'; +import sveltePreprocess from 'svelte-preprocess'; + import {defineConfig} from 'vite'; export default defineConfig(({mode}) => { @@ -27,13 +31,38 @@ export default defineConfig(({mode}) => { }, }, + plugins: [ - microWeb({ + nfpModule({ + id: 'app', include: 'app/src/**/*.ts', compilerOptions: { - sourceMap: B_DEV, + sourceMap: false, }, }), + + // // nfp window imports + // nfpxWindow({ + // id: 'app', + // }), + + // // build svelte components + // svelte({ + // compilerOptions: { + // immutable: true, + // css: 'injected', + // cssHash: ({hash, css, name, filename}) => `sv${hash(css)}`, + // // namespace: 'svg', + // }, + // preprocess: sveltePreprocess({}), + // }), + + // microWeb({ + // include: 'app/src/**/*.ts', + // compilerOptions: { + // sourceMap: B_DEV, + // }, + // }), ], }; }); diff --git a/applibs/rollup.config.ts b/applibs/rollup.config.ts index 87e64cc..5dce597 100644 --- a/applibs/rollup.config.ts +++ b/applibs/rollup.config.ts @@ -1,6 +1,6 @@ import type {RollupOptions} from 'rollup'; -import {microWeb} from '@nfps.dev/rollup-plugin-microweb'; +import {nfpModule} from '@nfps.dev/sdk'; import {defineConfig} from 'rollup'; @@ -11,18 +11,18 @@ export default defineConfig([ output: { format: 'iife', dir: 'dist', - entryFileNames: `${sr_lib}${'development' === process.env['NODE_ENV']? '.dev': ''}.js`, + entryFileNames: `${sr_lib}${'development' === process.env['NFP_ENV']? '.dev': ''}.js`, inlineDynamicImports: false, }, plugins: [ - microWeb({ + nfpModule({ + id: sr_lib, include: [ `applibs/src/${sr_lib}/**/*.ts`, - 'bootloader/src/global.ts', ], compilerOptions: { sourceMap: false, - declaration: false, + // declaration: false, }, }), ], diff --git a/applibs/src/interface.ts b/applibs/src/interface.d.ts similarity index 90% rename from applibs/src/interface.ts rename to applibs/src/interface.d.ts index 9f6d6c5..8c3fd36 100644 --- a/applibs/src/interface.ts +++ b/applibs/src/interface.d.ts @@ -1,3 +1,5 @@ +/// + /* eslint-disable @typescript-eslint/naming-convention,no-var */ import type {L} from 'ts-toolbelt'; @@ -5,9 +7,6 @@ import type { Dict, } from '@blake.regalia/belt'; -import type { - SecretBech32, -} from '@solar-republic/neutrino'; declare global { /** @@ -25,4 +24,6 @@ declare global { var writeOwner: (h_write: Dict) => Promise<0 | 1>; } + + export {}; diff --git a/applibs/src/storage/main.ts b/applibs/src/storage/main.ts index 46ebf8a..462145d 100644 --- a/applibs/src/storage/main.ts +++ b/applibs/src/storage/main.ts @@ -1,5 +1,7 @@ import type {} from '../interface'; +import type {L} from 'ts-toolbelt'; + import type { StorageData, } from '@nfps.dev/runtime'; @@ -8,15 +10,31 @@ import { ofe, } from '@blake.regalia/belt'; -Object.assign(window, { - // eslint-disable-next-line @typescript-eslint/naming-convention - async readOwner(a_keys) { - // perform query on contract - const [g_storage,, s_error] = await qci(sc, 'storage_owner_get', { - keys: a_keys, - }, qp || [vk!, toa]); - - // restructure response - return g_storage? ofe((g_storage.data || []).map(g => [g.key, g.value])): alert(s_error); - }, -} as Pick); +import { + SA_OWNER, +} from 'nfpx:app'; + +import { + G_QUERY_PERMIT, + K_CONTRACT, + SH_VIEWING_KEY, + query_contract_infer, +} from 'nfpx:bootloader'; + + +// eslint-disable-next-line @typescript-eslint/naming-convention +export async function readOwner< + a_keys extends string[], +>(a_keys: a_keys): Promise, string> | void | undefined> { + // perform query on contract + const [g_storage,, s_error] = await query_contract_infer(K_CONTRACT, 'storage_owner_get', { + keys: a_keys, + }, G_QUERY_PERMIT || [SH_VIEWING_KEY, SA_OWNER]); + + // restructure response + return g_storage? ofe((g_storage.data || []).map(g => [g.key, g.value])): alert(s_error); +} + +// } as Pick); + + diff --git a/bootloader/rollup.config.ts b/bootloader/rollup.config.ts index 0f9dba0..90ad7f3 100644 --- a/bootloader/rollup.config.ts +++ b/bootloader/rollup.config.ts @@ -1,41 +1,21 @@ -import type {RollupOptions} from 'rollup'; - -import {microWeb} from '@nfps.dev/rollup-plugin-microweb'; - -import replace from '@rollup/plugin-replace'; -import * as dotenv from 'dotenv'; +import {nfpModule} from '@nfps.dev/sdk'; import {defineConfig} from 'rollup'; -dotenv.config(); - -const h_env_safe = Object.fromEntries(Object.entries(process.env).filter(([si_key]) => si_key.startsWith('NFP_'))); - -export default defineConfig([ - 'bootloader.ts', - '_autoboot.ts', -].map(sr_file => ({ - input: `bootloader/src/${sr_file}`, +export default defineConfig({ + input: `bootloader/src/bootloader.ts`, output: { format: 'iife', dir: 'dist', - entryFileNames: `[name]${'development' === process.env['NODE_ENV']? '.dev': ''}.js`, + entryFileNames: `[name]${'development' === process.env['NFP_ENV']? '.dev': ''}.js`, inlineDynamicImports: false, }, plugins: [ - // for the autoboot config - ...sr_file.startsWith('_autoboot')? [ - replace({ - values: { - 'process.env': '('+JSON.stringify(h_env_safe)+')', - }, - }), - ]: [], - - microWeb({ + nfpModule({ + id: 'bootloader', include: 'bootloader/src/**/*.ts', compilerOptions: { sourceMap: false, }, }), ], -} as RollupOptions))); +}); diff --git a/bootloader/src/_autoboot.ts b/bootloader/src/_autoboot.ts index 2234c7f..032ed46 100644 --- a/bootloader/src/_autoboot.ts +++ b/bootloader/src/_autoboot.ts @@ -4,59 +4,51 @@ */ import type {HttpsUrl, SecretBech32} from '@solar-republic/neutrino'; -import {oda} from '@blake.regalia/belt'; - -import {create_svg, ls_get_json, ls_get_str, ls_set_json, ls_set_str, qsa} from '@nfps.dev/runtime'; -import {SecretContract, query_contract_infer} from '@solar-republic/neutrino'; +import { + create_svg, + qsa, +} from '@nfps.dev/runtime'; +import {SecretContract} from '@solar-republic/neutrino'; import {dm_root} from './common'; -// show onlyscripts -qsa(dm_root, '.onlyscript').map(dm_elmt => dm_elmt.classList.remove('onlyscript')); - -// env vars -const h_env = process.env; - -// wait for document to load -addEventListener('load', async() => { - const sa_contract = h_env['NFP_SELF_CONTRACT'] as SecretBech32; - const p_lcd = h_env['NFP_WEB_LCDS']?.split(',')[0] as HttpsUrl; - - // save reusable globals - oda(window, { - // boot info - loc: [h_env['NFP_SELF_CHAIN'], sa_contract, h_env['NFP_SELF_TOKEN']], - lcd: p_lcd, - qp: null, - vk: h_env['NFP_VIEWING_KEY'], - sc: await SecretContract(p_lcd, sa_contract), - - // reserved - toa: '', - - // override inject function to link locally instead - load(si_lib: string) { - return create_svg('script', { - href: `./${si_lib.replace(/\.js$/, '.dev.js')}`, - }); - }, - - // runtime functions - lsgs: ls_get_str, - lsss: ls_set_str, - lsgj: ls_get_json, - lssj: ls_set_json, - - // neutrino functions - qci: query_contract_infer, +// exporting as a function ensures no side effects when imported +export default function autoboot(): void { + // show onlyscripts + qsa(dm_root, '.onlyscript').map(dm_elmt => dm_elmt.classList.remove('onlyscript')); + + // env vars + const h_env = import.meta.env; + + // wait for document to load + addEventListener('load', async() => { + const sa_contract = h_env.SELF_CONTRACT as SecretBech32; + const p_lcd = h_env.WEB_LCDS?.split(',')[0] as HttpsUrl; + + // save reusable globals + exportNfpx({ + // boot info + A_TOKEN_LOCATION: [h_env.SELF_CHAIN!, sa_contract, h_env.SELF_TOKEN!], + P_LCD: p_lcd, + K_CONTRACT: await SecretContract(p_lcd, sa_contract), + G_QUERY_PERMIT: null, + SH_VIEWING_KEY: h_env.VIEWING_KEY!, + + // override inject function to link locally instead + load_script(si_package) { + return Promise.resolve(create_svg('script', { + href: `./${si_package.replace(/\.js$/, '.dev.js')}`, + })); + }, + }); + + // inject the app + dm_root.append(create_svg('script', { + href: './app.dev.js', + })); + + // hide banner + qsa(dm_root, '#default div')[0].style.display = 'none'; }); - - // inject the app - dm_root.append(create_svg('script', { - href: './app.dev.js', - })); - - // hide banner - qsa(dm_root, '#default div')[0].style.display = 'none'; -}); +} diff --git a/bootloader/src/bootloader.ts b/bootloader/src/bootloader.ts index e540623..2b5d342 100644 --- a/bootloader/src/bootloader.ts +++ b/bootloader/src/bootloader.ts @@ -1,59 +1,74 @@ -import {oda} from '@blake.regalia/belt'; -import {ls_get_str, type BootInfo, ls_set_str, ls_get_json, ls_set_json} from '@nfps.dev/runtime'; +import type { + BootInfo, + SlimTokenLocation, +} from '@nfps.dev/runtime'; import { boot, load_script, + ls_read, + ls_write, + ls_read_json, + ls_write_json, } from '@nfps.dev/runtime'; import { query_contract_infer, } from '@solar-republic/neutrino'; +// import autoboot as a module so that it can be optimized out in production +import autoboot from './_autoboot'; + // for side-effects import './common'; -// bind boot function to global so that user has to click to boot -window.boot = async(d_event) => { - const dm_button = d_event.target as HTMLButtonElement; - dm_button.textContent = 'Connecting...'; - dm_button.disabled = true; - - // boot and save bootinfo - let a_info: BootInfo | void; - if((a_info=await boot())) { // eslint-disable-line @typescript-eslint/no-extra-parens - const dm_div = dm_button.closest('div')!; - dm_div.firstChild!.textContent = 'Loaded latest app from chain'; - dm_div.style.background = '#2ec'; - dm_div.style.opacity = '0'; - - dm_button.textContent = 'Connected'; - - // save reusable globals - oda(window, { - // boot info - loc: a_info[0], - lcd: a_info[1], - qp: a_info[2], - vk: a_info[3], - sc: a_info[4], - - // reserved - toa: '', - - // runtime functions - load: load_script, - lsgs: ls_get_str, - lsss: ls_set_str, - lsgj: ls_get_json, - lssj: ls_set_json, - - // neutrino functions - qci: query_contract_infer, - }); - } - // boot failed - else { - dm_button.textContent = 'Failed'; - } +// development mode; perform autoboot +if(import.meta.env.DEV) { + autoboot(); +} +// production +else { + // bind boot function to global so that user has to click to boot + window.boot = async(d_event) => { + const dm_button = d_event.target as HTMLButtonElement; + dm_button.textContent = 'Connecting...'; + dm_button.disabled = true; + + // boot and save bootinfo + let a_info: BootInfo | void; + if((a_info=await boot())) { // eslint-disable-line @typescript-eslint/no-extra-parens + const dm_div = dm_button.closest('div')!; + dm_div.firstChild!.textContent = 'Loaded latest app from chain'; + dm_div.style.background = '#2ec'; + dm_div.style.opacity = '0'; + + dm_button.textContent = 'Connected'; + + // save reusable globals + exportNfpx({ + // boot info + A_TOKEN_LOCATION: a_info[0], + P_LCD: a_info[1], + G_QUERY_PERMIT: a_info[2], + SH_VIEWING_KEY: a_info[3], + K_CONTRACT: a_info[4], + }); + } + // boot failed + else { + dm_button.textContent = 'Failed'; + } + }; +} + +// export anything that may come in handy to reuse in the app or applibs +export { + load_script, + ls_read, + ls_write, + ls_read_json, + ls_write_json, + query_contract_infer, }; + +export type {NfpxExports} from './env'; diff --git a/bootloader/src/env.ts b/bootloader/src/env.ts new file mode 100644 index 0000000..a50eb49 --- /dev/null +++ b/bootloader/src/env.ts @@ -0,0 +1,25 @@ +/// +/// + +import type {Nilable, Promisable} from '@blake.regalia/belt'; +import type {SlimTokenLocation, load_script} from '@nfps.dev/runtime'; +import type {HttpsUrl, QueryPermit, SecretBech32, SecretContract} from '@solar-republic/neutrino'; + +// declare what the bootloader can export dynamically +export interface NfpxExports { + A_TOKEN_LOCATION: SlimTokenLocation; + P_LCD: HttpsUrl; + K_CONTRACT: SecretContract; + G_QUERY_PERMIT: Nilable; + SH_VIEWING_KEY: string; + + load_script?: typeof load_script; +} + +/* eslint-disable @typescript-eslint/naming-convention,no-var */ +declare global { + var boot: (d_event: MouseEvent) => Promisable; + var dismiss: () => void; + + function exportNfpx(h_exports: NfpxExports): void; +} diff --git a/bootloader/src/global.ts b/bootloader/src/global.ts deleted file mode 100644 index a56dd64..0000000 --- a/bootloader/src/global.ts +++ /dev/null @@ -1,53 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention,no-var */ -import type {Promisable, Nilable} from '@blake.regalia/belt'; - -import type { - SlimTokenLocation, - load_script, - ls_get_json, - ls_get_str, - ls_set_json, - ls_set_str, -} from '@nfps.dev/runtime'; - -import type { - SecretContract, - QueryPermit, - Wallet, - HttpsUrl, - query_contract_infer, - SecretBech32, -} from '@solar-republic/neutrino'; - -declare global { - var boot: (d_event: MouseEvent) => Promisable; - var dismiss: () => void; - - /** - * Save the boot info to the global scope so that the app can use it right away - */ - var loc: SlimTokenLocation; - var lcd: HttpsUrl; - var qp: QueryPermit | undefined; - var vk: string | undefined; - var sc: SecretContract; - - /** - * Reserved - */ - var toa: SecretBech32; - - /** - * Runtime functions - */ - var load: typeof load_script; - var lsgs: typeof ls_get_str; - var lsss: typeof ls_set_str; - var lsgj: typeof ls_get_json; - var lssj: typeof ls_set_json; - - /** - * Since the bootloader includes some neutrino lib, make some functions available for reuse - */ - var qci: typeof query_contract_infer; -} diff --git a/build.mjs b/build.mjs index c36aadb..941d7bc 100644 --- a/build.mjs +++ b/build.mjs @@ -14,10 +14,10 @@ const { NFP_SELF_CHAIN: si_chain, NFP_SELF_CONTRACT: sa_contract, NFP_SELF_TOKEN: si_token, - NODE_ENV: SI_NODE_ENV, + NFP_ENV: SI_NFP_ENV, } = h_env; -const B_DEV = 'development' === SI_NODE_ENV; +const B_DEV = 'development' === SI_NFP_ENV; const h_argv = yargs(hideBin(process.argv)) .scriptName('build').usage('$0 [flags]') @@ -42,7 +42,7 @@ const h_argv = yargs(hideBin(process.argv)) // make sure autoboot is only used in development mode if(h_argv.autoboot && !B_DEV) { - throw new Error(`Option '-a' can only be used when NODE_ENV=development`); + throw new Error(`Option '-a' can only be used when NFP_ENV=development`); } const sx_out = await build({ diff --git a/package.json b/package.json index 7dfa4eb..ff1dbb1 100644 --- a/package.json +++ b/package.json @@ -7,17 +7,17 @@ "main": "dist/main.js", "scripts": { "build:bootloader": "rollup -c bootloader/rollup.config.ts --configPlugin @rollup/plugin-typescript", - "dev:bootloader": "yarn build:bootloader --environment NODE_ENV:development", + "dev:bootloader": "yarn build:bootloader --environment NFP_ENV:development", "build:svg": "node build.mjs", - "dev:svg": "NODE_ENV=development yarn build:svg -la -o dist/nfp.dev.svg", + "dev:svg": "NFP_ENV=development yarn build:svg -la -o dist/nfp.dev.svg", "build:nfp": "yarn build:bootloader && yarn build:svg", "dev:nfp": "yarn dev:bootloader && yarn dev:svg", "build:app": "vite build app", - "dev:app": "NODE_ENV=development yarn build:app --mode development", + "dev:app": "NFP_ENV=development yarn build:app --mode development", "watch:app": "yarn dev:app -w", "deploy:app": "node --loader ts-node/esm deploy.ts", "build:applibs": "rollup -c applibs/rollup.config.ts --configPlugin @rollup/plugin-typescript", - "dev:applibs": "yarn build:applibs --environment NODE_ENV:development", + "dev:applibs": "yarn build:applibs --environment NFP_ENV:development", "build": "yarn build:applibs && yarn build:app && yarn build:nfp", "dev": "yarn dev:applibs && yarn dev:app && yarn dev:nfp && echo \"\\n-------- READY --------\\n\"", "watch:dev": "ls {app,applibs,bootloader,media}/**/*.* package.json build.ts | entr yarn dev", @@ -31,18 +31,20 @@ "@blake.regalia/eslint-config-elite": "^0.2.8", "@blake.regalia/tsconfig": "^0.1.2", "@nfps.dev/rollup-plugin-microweb": "^0.3.5", - "@nfps.dev/sdk": "^0.3.0", + "@nfps.dev/sdk": "^0.4.7", "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-typescript": "^11.1.1", - "@types/node": "^20.2.1", + "@types/node": "^20.2.3", "@types/web": "^0.0.99", "@typescript-eslint/parser": "^5.59.6", "dotenv": "^16.0.3", "eslint": "^8.41.0", "eslint-plugin-svelte3": "^4.0.0", + "io-ts-transformer": "^1.1.8", "less": "^4.1.3", - "rollup": "^3.22.0", + "rollup": "^3.22.1", "rollup-plugin-inject-process-env": "^1.3.1", + "rollup-plugin-typescript2": "^0.34.1", "svelte": "^3.59.1", "svelte-check": "^3.3.2", "svelte-preprocess": "^5.0.3", @@ -54,9 +56,8 @@ "yargs": "^17.7.2" }, "dependencies": { - "@nfps.dev/runtime": "^0.8.0", + "@nfps.dev/runtime": "^0.9.3", "@solar-republic/neutrino": "^0.3.7", - "@sveltejs/vite-plugin-svelte": "^2.2.0", - "D": "^1.0.0" + "@sveltejs/vite-plugin-svelte": "^2.2.0" } } diff --git a/tsconfig.json b/tsconfig.json index 32b08a4..7462e8a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,11 @@ "outDir": "dist", "declaration": true, - "declarationDir": "dist" + "declarationDir": "dist", + + "paths": { + "nfpx:*": ["./dist/*.d.ts"] + } }, "include": [ From ca26cd269884b30e650b117e640fbedefe3753be Mon Sep 17 00:00:00 2001 From: Blake Regalia Date: Fri, 26 May 2023 02:33:05 -0700 Subject: [PATCH 2/9] feat: nfp modules --- .eslintrc.cjs | 5 +- .gitignore | 3 + .nfp-modules.json | 113 ++++++++++++++++++++++++ app/media/wallet.svg | 1 + app/src/App.svelte | 164 +++++++++++++++++++++++++++-------- app/src/Wallet.svelte | 156 +++++++++++++++++++++++++++++++++ app/src/env.d.ts | 1 - app/src/env.ts | 14 +++ app/src/main.ts | 91 ++++++++++--------- app/tsconfig.json | 14 +++ app/vite.config.ts | 33 +------ applibs/rollup.config.ts | 1 - applibs/src/env.ts | 3 + applibs/src/interface.d.ts | 29 ------- applibs/src/storage/main.ts | 118 +++++++++++++++++++++++-- bootloader/rollup.config.ts | 7 +- bootloader/src/_autoboot.ts | 15 ++-- bootloader/src/bootloader.ts | 10 ++- bootloader/src/env.ts | 4 +- build.mjs | 39 ++++----- deploy.ts | 31 +++++-- package.json | 33 ++++--- tsconfig.json | 8 +- 23 files changed, 689 insertions(+), 204 deletions(-) create mode 100644 .nfp-modules.json create mode 100644 app/media/wallet.svg create mode 100644 app/src/Wallet.svelte delete mode 100644 app/src/env.d.ts create mode 100644 app/src/env.ts create mode 100644 app/tsconfig.json create mode 100644 applibs/src/env.ts delete mode 100644 applibs/src/interface.d.ts diff --git a/.eslintrc.cjs b/.eslintrc.cjs index ccc876f..898efb3 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,9 +1,12 @@ module.exports = { - extends: '@blake.regalia/eslint-config-elite', + extends: '@blake.regalia/eslint-config-elite/svelte.js', parserOptions: { ecmaVersion: 2022, sourceType: 'module', tsconfigRootDir: __dirname, project: 'tsconfig.json', }, + globals: { + 'destructureImportedNfpModule': 'readonly', + }, }; diff --git a/.gitignore b/.gitignore index ab79e8e..b14c331 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ # ignore package-manager locks **/*.lock **/package-lock.json + +# keep generated module JSON data +!.nfp-modules.json diff --git a/.nfp-modules.json b/.nfp-modules.json new file mode 100644 index 0000000..b113446 --- /dev/null +++ b/.nfp-modules.json @@ -0,0 +1,113 @@ +{ + "schema": "1", + "comment": "Do not edit or delete this generated file! It should also be committed to version control.", + "modules": { + "bootloader": { + "version": "0.0", + "hash": "c0c961f9de8e", + "exports": { + ".": { + "A_TOKEN_LOCATION": { + "symbol": "a", + "dynamic": true + }, + "P_LCD": { + "symbol": "b", + "dynamic": true + }, + "G_QUERY_PERMIT": { + "symbol": "c", + "dynamic": true + }, + "SH_VIEWING_KEY": { + "symbol": "d", + "dynamic": true + }, + "K_CONTRACT": { + "symbol": "e", + "dynamic": true + }, + "load_script": { + "symbol": "f" + }, + "ls_read": { + "symbol": "g" + }, + "ls_write": { + "symbol": "h" + }, + "ls_read_json": { + "symbol": "i" + }, + "ls_write_json": { + "symbol": "j" + }, + "query_contract_infer": { + "symbol": "k" + } + } + }, + "dependencies": {} + }, + "app": { + "version": "0.2", + "hash": "4b1ca6bbcb72", + "exports": { + ".": { + "SA_OWNER": { + "symbol": "a" + }, + "exec_contract": { + "symbol": "b" + }, + "K_WALLET": { + "symbol": "c", + "dynamic": true + }, + "dm_root": { + "symbol": "d" + } + } + }, + "dependencies": { + "storage": { + "version": "0.1", + "hash": "c87091d4dc20", + "dynamic": true, + "imports": {} + }, + "bootloader": { + "version": "0.0", + "hash": "c0c961f9de8e" + } + } + }, + "storage": { + "version": "0.1", + "hash": "c87091d4dc20", + "exports": { + ".": { + "readOwner": { + "symbol": "a" + }, + "writeOwner": { + "symbol": "b" + }, + "findFeegranter": { + "symbol": "c" + } + } + }, + "dependencies": { + "app": { + "version": "0.2", + "hash": "4b1ca6bbcb72" + }, + "bootloader": { + "version": "0.0", + "hash": "c0c961f9de8e" + } + } + } + } +} \ No newline at end of file diff --git a/app/media/wallet.svg b/app/media/wallet.svg new file mode 100644 index 0000000..b8de6d4 --- /dev/null +++ b/app/media/wallet.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/src/App.svelte b/app/src/App.svelte index 737ca10..f71578f 100644 --- a/app/src/App.svelte +++ b/app/src/App.svelte @@ -1,59 +1,149 @@ - qsa((d_event.target as HTMLElement).closest('form')!, 'input,button') - .map(dm => dm.setAttribute('disabled', 'disabled')); + -
-

App connected

+
+
+

+ {b_loading? 'Loading...': 'Synced'} +

+
-
-
-
- - - -
-
-
+
+ + +
-
\ No newline at end of file + + + diff --git a/app/src/Wallet.svelte b/app/src/Wallet.svelte new file mode 100644 index 0000000..7dcc4a5 --- /dev/null +++ b/app/src/Wallet.svelte @@ -0,0 +1,156 @@ + + + + +
+
+ + {@html SX_ICON_WALLET} + + + + {s_spendable} + + + SCRT + + +
+
+
+
+ Address of this burner account wallet +
+
+ {K_WALLET.addr} +
+
+
+
+ Feegrants currently available +
+
+ {#each a_feegrants as g_grant} +
+ +
+ {/each} +
+
+
+
+ Address of the NFP token owner +
+
+ {SA_OWNER} +
+
+
+
+ URL of the network API endpoint +
+
+ {K_WALLET.lcd} +
+
+
+
diff --git a/app/src/env.d.ts b/app/src/env.d.ts deleted file mode 100644 index be5af80..0000000 --- a/app/src/env.d.ts +++ /dev/null @@ -1 +0,0 @@ -/// diff --git a/app/src/env.ts b/app/src/env.ts new file mode 100644 index 0000000..f81f385 --- /dev/null +++ b/app/src/env.ts @@ -0,0 +1,14 @@ +/// +/// + +import type {Wallet} from '@solar-republic/neutrino'; + +// declare what the app can export dynamically +export interface NfpxExports { + K_WALLET: Wallet; +} + +/* eslint-disable @typescript-eslint/naming-convention,no-var */ +declare global { + function exportNfpx(h_exports: NfpxExports): void; +} diff --git a/app/src/main.ts b/app/src/main.ts index 663b430..6c8956a 100644 --- a/app/src/main.ts +++ b/app/src/main.ts @@ -1,12 +1,9 @@ +import type {NfpxExports} from './env'; import type {SecretBech32} from '@solar-republic/neutrino'; -import {base64_to_buffer} from '@blake.regalia/belt'; -import {create_svg, create_html, ls_write, load_script} from '@nfps.dev/runtime'; -import {safe_json, Wallet, gen_sk} from '@solar-republic/neutrino'; - -// import App from './app.svelte'; - -import type {} from '../../applibs/src/interface'; +import {base64_to_buffer, buffer_to_base93} from '@blake.regalia/belt'; +import {create_svg, create_html, ls_write, ls_write_b64, ls_read_b64} from '@nfps.dev/runtime'; +import {Wallet, gen_sk, exec_contract} from '@solar-republic/neutrino'; import { A_TOKEN_LOCATION, @@ -17,6 +14,9 @@ import { ls_read, } from 'nfpx:bootloader'; +import App from './App.svelte'; + + const dm_root = document.documentElement; // create ui to allow user to play/pause animations @@ -26,15 +26,18 @@ const dm_pause = create_html('button', { 'Pause', ]); +const dm_app = create_html('div'); + dm_root.append(create_svg('foreignObject', { width: '100%', height: '100%', x: '0', y: '0', }, [ - create_html('div', {}, [ - dm_pause, - ]), + dm_app, + // create_html('div', {}, [ + // dm_pause, + // ]), ])); dm_pause.onclick = () => { @@ -47,42 +50,50 @@ const si_storage_token_owner_addr = 'toa:'+A_TOKEN_LOCATION.join(':'); const SA_OWNER = ls_read(si_storage_token_owner_addr) as SecretBech32 || ls_write(si_storage_token_owner_addr, prompt('Please enter the account address that owns this token') || ''); +// const SA_OWNER = await idb_read(si_storage_token_owner_addr) +// || await idb_write(si_storage_token_owner_addr, prompt('Please enter ...')); + + + (async() => { // get or create private key - const sh_sk = ls_read('sk'); - const atu8_sk = sh_sk? base64_to_buffer(sh_sk): gen_sk(); + const atu8_sk = ls_read_b64('sk') || gen_sk(); // create wallet - const k_wallet = Wallet(atu8_sk, A_TOKEN_LOCATION[0], P_LCD); - - // fetch library - const dm_script = await load_script('storage.js', { - tag: '1.x', - }, K_CONTRACT, A_TOKEN_LOCATION, G_QUERY_PERMIT || [SH_VIEWING_KEY, SA_OWNER]); - - // request succeded - if(dm_script) { - // wait for script to load before calling its functions - dm_script.onload = async() => { - // read from token - const g_read = await readOwner(['test']); - console.log(g_read); - - // // load app - // new App({ - // target: document.documentElement, - // props: { - // k_wallet, - // k_contract: sc, - // }, - // }); - }; - - // inject library - dm_root.append(dm_script); - } + const k_wallet = await Wallet(atu8_sk, A_TOKEN_LOCATION[0], P_LCD); + + // save to local storage + ls_write_b64('sk', atu8_sk); + console.log(`Hot wallet address: ${k_wallet.addr}`); + + // dynamic export before importing libs (which depend on these exports) + exportNfpx({ + K_WALLET: k_wallet, + }); + + // load the storage library before launching 'App.svelte' + await import('nfpx:storage', { + contract: K_CONTRACT, + location: A_TOKEN_LOCATION, + auth: G_QUERY_PERMIT || [SH_VIEWING_KEY, SA_OWNER], + query: { + tag: '1.x', + }, + }); + + // launch the app + new App({ + target: dm_app, + }); })(); + export { SA_OWNER, + exec_contract, + dm_root, }; + +export default interface Default extends NfpxExports { + merge: string; +} diff --git a/app/tsconfig.json b/app/tsconfig.json new file mode 100644 index 0000000..0440083 --- /dev/null +++ b/app/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": [ + "../tsconfig.json" + ], + + "compilerOptions": { + "verbatimModuleSyntax": true, + + "sourceMap": true, + + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + } +} \ No newline at end of file diff --git a/app/vite.config.ts b/app/vite.config.ts index 0659e47..63b0a1e 100644 --- a/app/vite.config.ts +++ b/app/vite.config.ts @@ -1,9 +1,6 @@ import {resolve} from 'path'; -import {microWeb} from '@nfps.dev/rollup-plugin-microweb'; -import {nfpModule, nfpxWindow} from '@nfps.dev/sdk'; -import {svelte} from '@sveltejs/vite-plugin-svelte'; -import sveltePreprocess from 'svelte-preprocess'; +import {nfpModule} from '@nfps.dev/sdk'; import {defineConfig} from 'vite'; @@ -16,7 +13,7 @@ export default defineConfig(({mode}) => { emptyOutDir: false, minify: !B_DEV, sourcemap: B_DEV? 'inline': false, - target: ['es2022'], + target: ['esnext'], lib: { entry: resolve(__dirname, 'src/main.ts'), @@ -31,38 +28,16 @@ export default defineConfig(({mode}) => { }, }, - plugins: [ nfpModule({ id: 'app', include: 'app/src/**/*.ts', compilerOptions: { sourceMap: false, + module: 'esnext', }, + svelte: {}, }), - - // // nfp window imports - // nfpxWindow({ - // id: 'app', - // }), - - // // build svelte components - // svelte({ - // compilerOptions: { - // immutable: true, - // css: 'injected', - // cssHash: ({hash, css, name, filename}) => `sv${hash(css)}`, - // // namespace: 'svg', - // }, - // preprocess: sveltePreprocess({}), - // }), - - // microWeb({ - // include: 'app/src/**/*.ts', - // compilerOptions: { - // sourceMap: B_DEV, - // }, - // }), ], }; }); diff --git a/applibs/rollup.config.ts b/applibs/rollup.config.ts index 5dce597..cff7b74 100644 --- a/applibs/rollup.config.ts +++ b/applibs/rollup.config.ts @@ -1,7 +1,6 @@ import type {RollupOptions} from 'rollup'; import {nfpModule} from '@nfps.dev/sdk'; - import {defineConfig} from 'rollup'; export default defineConfig([ diff --git a/applibs/src/env.ts b/applibs/src/env.ts new file mode 100644 index 0000000..2471373 --- /dev/null +++ b/applibs/src/env.ts @@ -0,0 +1,3 @@ +/// +/// +/// diff --git a/applibs/src/interface.d.ts b/applibs/src/interface.d.ts deleted file mode 100644 index 8c3fd36..0000000 --- a/applibs/src/interface.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -/// - -/* eslint-disable @typescript-eslint/naming-convention,no-var */ -import type {L} from 'ts-toolbelt'; - -import type { - Dict, -} from '@blake.regalia/belt'; - - -declare global { - /** - * Reads from the contract's owner storage - * @returns an `object` of the key/value pairs contained in the response, or `undefined` if there was - * a query error - */ - var readOwner: < - a_keys extends string[], - >(a_keys: a_keys) => Promise, string> | undefined>; - - /** - * Writes to the contract's owner storage - */ - var writeOwner: (h_write: Dict) => Promise<0 | 1>; -} - - - -export {}; diff --git a/applibs/src/storage/main.ts b/applibs/src/storage/main.ts index 462145d..2d99515 100644 --- a/applibs/src/storage/main.ts +++ b/applibs/src/storage/main.ts @@ -1,17 +1,36 @@ -import type {} from '../interface'; - import type {L} from 'ts-toolbelt'; +import type { + JsonValue, + JsonObject, + Uint128, +} from '@blake.regalia/belt'; + import type { StorageData, } from '@nfps.dev/runtime'; +import type {SecretBech32} from '@solar-republic/neutrino'; + + import { - ofe, -} from '@blake.regalia/belt'; + ode, + oderac, ofe} from '@blake.regalia/belt'; + + +import { + queryFeegrantAllowances, + // tpa, + // tpae, + // tpas, + // tpaw, + // tpar, +} from '@solar-republic/neutrino'; import { SA_OWNER, + K_WALLET, + exec_contract, } from 'nfpx:app'; import { @@ -22,19 +41,100 @@ import { } from 'nfpx:bootloader'; -// eslint-disable-next-line @typescript-eslint/naming-convention +type StoragePutSuccess = { + [si_key in `storage_${si_area}_put`]: { + status: 'success'; + }; +}; + + +type Allowance = []; + +const h_feegrants: Record> = {}; + +/** + * + */ +export async function findFeegranter(sa_grantee: SecretBech32, xg_needed: bigint): Promise { + for(const [sa_granter, a_allowance] of ode(h_feegrants[sa_grantee] || {})) { + if(a_allowance) { + return sa_granter; + } + } + + const a_results = await queryFeegrantAllowances(K_WALLET.lcd, sa_grantee); + + debugger; + for(const g_result of a_results) { + const g_allowance = g_result.allowance; + + // basic allowance + if(g_allowance['@type'].includes('Basic')) { + // destructure spend limit + const [g_coin] = g_allowance.spend_limit; + + // allowance amount is enough to cover needed amount + if(BigInt(g_coin.amount) >= xg_needed) { + return g_result.granter; + } + } + } + + // return tpa(a_results[0])._g || tpa(a_results[0]).$r!; + // return tpas(a_results[0], 'g') || tpae(a_results[0], 'r'); + // return tpaw(a_results[0], 'g') || tpaw(a_results[0], 'r', 1); + // return tpar(a_results[0], /^g/) || tpar(a_results[0], /r$/); + // return a_results[0].granter; + + return; +} + + + +/** + * Reads from the contract's owner storage + * @returns an `object` of the key/value pairs contained in the response, or `undefined` if there was + * a query error + */ export async function readOwner< - a_keys extends string[], ->(a_keys: a_keys): Promise, string> | void | undefined> { + const a_keys extends readonly string[], +>(a_keys: a_keys): Promise<{ + [si_key in L.UnionOf]: string; +} | void | undefined> { // perform query on contract const [g_storage,, s_error] = await query_contract_infer(K_CONTRACT, 'storage_owner_get', { keys: a_keys, }, G_QUERY_PERMIT || [SH_VIEWING_KEY, SA_OWNER]); // restructure response - return g_storage? ofe((g_storage.data || []).map(g => [g.key, g.value])): alert(s_error); + return g_storage? ofe((g_storage.data || []).map(g => [g.key, g.value])) as { + [si_key in L.UnionOf]: string; + }: alert(s_error); } -// } as Pick); +/** + * Writes to the contract's owner storage + */ +export async function writeOwner(h_write: Record): Promise> { + const a_entries = oderac(h_write, (si_key, w_value) => ({ + key: si_key, + value: w_value, + })); + + const x_limit = 60_000; + const x_fee = x_limit * 0.125; + + // find feegrant + const sa_granter = await findFeegranter(K_WALLET.addr, BigInt(x_fee)); + const [xc_code, s_res, g_tx] = await exec_contract(K_CONTRACT, K_WALLET, { + storage_owner_put: { + data: a_entries, + }, + }, [[''+x_fee as Uint128, 'uscrt']], ''+x_limit as Uint128, '', sa_granter); + + if(xc_code) throw s_res; + + return JSON.parse(s_res) as StoragePutSuccess<'owner'>; +} diff --git a/bootloader/rollup.config.ts b/bootloader/rollup.config.ts index 90ad7f3..428c9ae 100644 --- a/bootloader/rollup.config.ts +++ b/bootloader/rollup.config.ts @@ -7,11 +7,16 @@ export default defineConfig({ format: 'iife', dir: 'dist', entryFileNames: `[name]${'development' === process.env['NFP_ENV']? '.dev': ''}.js`, - inlineDynamicImports: false, + + // in case any imported libraries use dynamic imports, they will not work in the nfp + // unless inlined with the bundle. set to false if you never expect the conditions + // to be met in which an imported package would attempt a dynamic import + inlineDynamicImports: true, }, plugins: [ nfpModule({ id: 'bootloader', + injectNfpModuleSystem: true, include: 'bootloader/src/**/*.ts', compilerOptions: { sourceMap: false, diff --git a/bootloader/src/_autoboot.ts b/bootloader/src/_autoboot.ts index 032ed46..9049568 100644 --- a/bootloader/src/_autoboot.ts +++ b/bootloader/src/_autoboot.ts @@ -26,21 +26,22 @@ export default function autoboot(): void { const sa_contract = h_env.SELF_CONTRACT as SecretBech32; const p_lcd = h_env.WEB_LCDS?.split(',')[0] as HttpsUrl; + // override sdk's default script loader with a custom one to link to local package instead + const load_script = nfpx.l = si_package => Promise.resolve(create_svg('script', { + href: `./${si_package.replace(/\.js$/, '.dev.js')}`, + })); + // save reusable globals exportNfpx({ // boot info A_TOKEN_LOCATION: [h_env.SELF_CHAIN!, sa_contract, h_env.SELF_TOKEN!], P_LCD: p_lcd, - K_CONTRACT: await SecretContract(p_lcd, sa_contract), G_QUERY_PERMIT: null, SH_VIEWING_KEY: h_env.VIEWING_KEY!, + K_CONTRACT: await SecretContract(p_lcd, sa_contract), - // override inject function to link locally instead - load_script(si_package) { - return Promise.resolve(create_svg('script', { - href: `./${si_package.replace(/\.js$/, '.dev.js')}`, - })); - }, + // export the custom script loader + load_script, }); // inject the app diff --git a/bootloader/src/bootloader.ts b/bootloader/src/bootloader.ts index 2b5d342..e351cb0 100644 --- a/bootloader/src/bootloader.ts +++ b/bootloader/src/bootloader.ts @@ -1,6 +1,5 @@ import type { BootInfo, - SlimTokenLocation, } from '@nfps.dev/runtime'; import { @@ -56,7 +55,13 @@ else { } // boot failed else { - dm_button.textContent = 'Failed'; + dm_button.textContent = 'Reset'; + dm_button.disabled = false; + dm_button.onclick = () => { + dm_button.disabled = true; + localStorage.clear(); + dm_button.textContent = 'Done. Reload to retry'; + }; } }; } @@ -71,4 +76,5 @@ export { query_contract_infer, }; +// special type-only export for the dynamic export call export type {NfpxExports} from './env'; diff --git a/bootloader/src/env.ts b/bootloader/src/env.ts index a50eb49..e8b1aa4 100644 --- a/bootloader/src/env.ts +++ b/bootloader/src/env.ts @@ -1,9 +1,9 @@ -/// +/// /// import type {Nilable, Promisable} from '@blake.regalia/belt'; import type {SlimTokenLocation, load_script} from '@nfps.dev/runtime'; -import type {HttpsUrl, QueryPermit, SecretBech32, SecretContract} from '@solar-republic/neutrino'; +import type {HttpsUrl, QueryPermit, SecretContract} from '@solar-republic/neutrino'; // declare what the bootloader can export dynamically export interface NfpxExports { diff --git a/build.mjs b/build.mjs index 941d7bc..d36adcb 100644 --- a/build.mjs +++ b/build.mjs @@ -18,6 +18,7 @@ const { } = h_env; const B_DEV = 'development' === SI_NFP_ENV; +const B_PROD = !B_DEV; const h_argv = yargs(hideBin(process.argv)) .scriptName('build').usage('$0 [flags]') @@ -94,32 +95,24 @@ const sx_out = await build({ const dm_root = document.documentElement; // inject content - dm_root.append(...h_argv.autoboot - // autoboot mode: omit bootloader and use autoboot script instead - ? [ - // autoboot (should inject app once it's completed any async bootup tasks) - create.svg('script', { - href: './_autoboot.dev.js' - }), - ] - // use bootloader - : [ - // bootloader; depending on environment - B_DEV && h_argv.link - // development: link to bootloader for better debugging experience in browser - ? create.svg('script', { - href: './bootloader.dev.js' - }) - // testing/production: inline script - : create.svg('script', {}, [ - fs.readFileSync(`./dist/bootloader${B_DEV? '.dev': ''}.js`, 'utf8'), - ]), - - // fetch latest app from chain + dm_root.append( + // bootloader; depending on environment + B_DEV && h_argv.link + // development: link to bootloader for better debugging experience in browser + ? create.svg('script', { + href: './bootloader.dev.js' + }) + // testing/production: inline script + : create.svg('script', {}, [ + fs.readFileSync(`./dist/bootloader${B_DEV? '.dev': ''}.js`, 'utf8'), + ]), + + // fetch latest app from chain in production mode + ...B_DEV? []: [ create.nfp('script', { src: 'app.js?tag=latest', }), - ] + ], ); }, }); diff --git a/deploy.ts b/deploy.ts index a7295fe..c1480cf 100644 --- a/deploy.ts +++ b/deploy.ts @@ -1,4 +1,8 @@ import type {Uint128} from '@blake.regalia/belt'; +import type { + HttpsUrl, + SecretBech32, +} from '@solar-republic/neutrino'; import {readFile} from 'node:fs/promises'; @@ -10,12 +14,6 @@ import {oderac, base64_to_buffer, buffer_to_base64, hex_to_buffer, buffer_to_hex import { format_query, query_contract, - type HttpsUrl, - type SecretBech32, -} from '@solar-republic/neutrino'; - - -import { gen_sk, sk_to_pk, pubkey_to_bech32, @@ -24,8 +22,11 @@ import { query_contract_infer, exec_contract, sign_query_permit, + msgGrantAllowance, } from '@solar-republic/neutrino'; + + // load environment variables import * as dotenv from 'dotenv'; dotenv.config(); @@ -83,12 +84,28 @@ const k_contract = await SecretContract(p_lcd, sa_contract as SecretBech32); (async() => { // await mint(); - await set_vk('test123'); + // await set_vk('test123'); // await storage_owner_put(); // await upload_script('dist/app.js'); // await upload_script('dist/storage.js', ['1.x', 'latest']); + + await authorize('secret1vxrsryq9crnhrjajw5s8npyshpgmcvtlmrw7kw'); })(); +async function authorize() { + console.log(...await exec_contract(k_contract, k_wallet, { + mint_nft: { + token_id: '1', + public_metadata: { + token_uri: 'test-public', + }, + private_metadata: { + token_uri: 'test-private', + }, + }, + }, [['6000', 'uscrt']], `${60000n}`)); +} + // mint async function mint() { console.log(...await exec_contract(k_contract, k_wallet, { diff --git a/package.json b/package.json index ff1dbb1..1e1f9dd 100644 --- a/package.json +++ b/package.json @@ -6,44 +6,48 @@ "type": "module", "main": "dist/main.js", "scripts": { - "build:bootloader": "rollup -c bootloader/rollup.config.ts --configPlugin @rollup/plugin-typescript", + "rollup": "./node_modules/.bin/rollup", + "vite": "./node_modules/.bin/vite", + "build:bootloader": "yarn rollup -c bootloader/rollup.config.ts --configPlugin @rollup/plugin-typescript", "dev:bootloader": "yarn build:bootloader --environment NFP_ENV:development", "build:svg": "node build.mjs", "dev:svg": "NFP_ENV=development yarn build:svg -la -o dist/nfp.dev.svg", "build:nfp": "yarn build:bootloader && yarn build:svg", "dev:nfp": "yarn dev:bootloader && yarn dev:svg", - "build:app": "vite build app", + "build:app": "yarn vite build app", "dev:app": "NFP_ENV=development yarn build:app --mode development", "watch:app": "yarn dev:app -w", "deploy:app": "node --loader ts-node/esm deploy.ts", - "build:applibs": "rollup -c applibs/rollup.config.ts --configPlugin @rollup/plugin-typescript", + "build:applibs": "yarn rollup -c applibs/rollup.config.ts --configPlugin @rollup/plugin-typescript", "dev:applibs": "yarn build:applibs --environment NFP_ENV:development", "build": "yarn build:applibs && yarn build:app && yarn build:nfp", "dev": "yarn dev:applibs && yarn dev:app && yarn dev:nfp && echo \"\\n-------- READY --------\\n\"", - "watch:dev": "ls {app,applibs,bootloader,media}/**/*.* package.json build.ts | entr yarn dev", - "watch:build": "ls {app,applibs,bootloader,media}/**/*.* package.json build.ts | entr yarn build" + "watch:dev": "ls {app,applibs,bootloader,media}/**/*.* package.json build.{mjs,ts} | entr yarn dev", + "watch:build": "ls {app,applibs,bootloader,media}/**/*.* package.json build.{mjs,ts} | entr yarn build" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { - "@blake.regalia/belt": "^0.12.0", - "@blake.regalia/eslint-config-elite": "^0.2.8", + "@blake.regalia/belt": "^0.13.4", + "@blake.regalia/eslint-config-elite": "^0.2.10", "@blake.regalia/tsconfig": "^0.1.2", + "@cosmjs/amino": "^0.30.1", "@nfps.dev/rollup-plugin-microweb": "^0.3.5", - "@nfps.dev/sdk": "^0.4.7", + "@nfps.dev/sdk": "^0.6.0", "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-typescript": "^11.1.1", - "@types/node": "^20.2.3", + "@types/node": "^20.2.4", "@types/web": "^0.0.99", - "@typescript-eslint/parser": "^5.59.6", + "@typescript-eslint/parser": "^5.59.7", "dotenv": "^16.0.3", "eslint": "^8.41.0", "eslint-plugin-svelte3": "^4.0.0", "io-ts-transformer": "^1.1.8", "less": "^4.1.3", - "rollup": "^3.22.1", + "rollup": "^3.23.0", "rollup-plugin-inject-process-env": "^1.3.1", + "rollup-plugin-svelte": "^7.1.4", "rollup-plugin-typescript2": "^0.34.1", "svelte": "^3.59.1", "svelte-check": "^3.3.2", @@ -56,8 +60,9 @@ "yargs": "^17.7.2" }, "dependencies": { - "@nfps.dev/runtime": "^0.9.3", - "@solar-republic/neutrino": "^0.3.7", - "@sveltejs/vite-plugin-svelte": "^2.2.0" + "@nfps.dev/runtime": "^0.9.4", + "@solar-republic/neutrino": "^0.4.0", + "@sveltejs/vite-plugin-svelte": "^2.3.0", + "idb-keyval": "^6.2.1" } } diff --git a/tsconfig.json b/tsconfig.json index 7462e8a..507e332 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,13 +8,19 @@ "declaration": true, "declarationDir": "dist", + "module": "ESNext", + "paths": { - "nfpx:*": ["./dist/*.d.ts"] + "nfpx:*?tag=latest": ["./dist/*.d.ts"], + "nfpx:*?tag=1.x": ["./dist/*.d.ts"], + "nfpx:*": ["./dist/*.d.ts"], } }, "include": [ "app/**/*.ts", + "app/**/*.js", + "app/**/*.svelte", "bootloader/**/*.ts", "applibs/**/*.ts", "test/**/*.ts", From a775e4a5d1f1d53913bce0078ba755bd32c5f1de Mon Sep 17 00:00:00 2001 From: Blake Regalia Date: Sun, 28 May 2023 02:13:52 -0700 Subject: [PATCH 3/9] dev: neutrino wallet --- .nfp-modules.json | 13 +- app/media/wallet.svg | 1 - app/src/App.svelte | 28 +- app/src/Wallet.svelte | 622 +++++++++++++++++++++++++++++++---- app/src/def.less | 27 ++ app/src/env.ts | 1 + app/src/main.ts | 12 +- app/src/webext-portal.ts | 14 + app/vite.config.ts | 4 +- applibs/src/storage/main.ts | 23 ++ bootloader/src/bootloader.ts | 5 +- deploy.ts | 2 +- media/preview.html | 7 + media/template.svg | 9 +- package.json | 3 + 15 files changed, 678 insertions(+), 93 deletions(-) delete mode 100644 app/media/wallet.svg create mode 100644 app/src/def.less create mode 100644 app/src/webext-portal.ts diff --git a/.nfp-modules.json b/.nfp-modules.json index b113446..995f439 100644 --- a/.nfp-modules.json +++ b/.nfp-modules.json @@ -50,8 +50,8 @@ "dependencies": {} }, "app": { - "version": "0.2", - "hash": "4b1ca6bbcb72", + "version": "0.3", + "hash": "780935860a87", "exports": { ".": { "SA_OWNER": { @@ -66,16 +66,13 @@ }, "dm_root": { "symbol": "d" + }, + "dm_foreign": { + "symbol": "e" } } }, "dependencies": { - "storage": { - "version": "0.1", - "hash": "c87091d4dc20", - "dynamic": true, - "imports": {} - }, "bootloader": { "version": "0.0", "hash": "c0c961f9de8e" diff --git a/app/media/wallet.svg b/app/media/wallet.svg deleted file mode 100644 index b8de6d4..0000000 --- a/app/media/wallet.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/app/src/App.svelte b/app/src/App.svelte index f71578f..f5e3c6b 100644 --- a/app/src/App.svelte +++ b/app/src/App.svelte @@ -1,6 +1,4 @@ -
+
- {@html SX_ICON_WALLET} + {@html icon_to_svg(faWallet.icon, 22, 'f7f7f7')} @@ -115,42 +595,58 @@
-
-
-
- Address of this burner account wallet -
-
- {K_WALLET.addr} -
-
-
-
- Feegrants currently available -
-
- {#each a_feegrants as g_grant} -
- +
+
+ {#each a_menus as h_menu, i_menu (JSON.stringify(h_menu))} + -
-
-
- Address of the NFP token owner -
-
- {SA_OWNER} -
-
-
-
- URL of the network API endpoint -
-
- {K_WALLET.lcd} -
+
+ {/each}
diff --git a/app/src/def.less b/app/src/def.less new file mode 100644 index 0000000..04fe605 --- /dev/null +++ b/app/src/def.less @@ -0,0 +1,27 @@ + +/* ease-in corresponds to cubic-bezier(0.42, 0, 1.0, 1.0) */ +@ease-in-quad: cubic-bezier(0.55, 0.085, 0.68, 0.53); +@ease-in-cubic: cubic-bezier(0.55, 0.055, 0.675, 0.19); +@ease-in-quart: cubic-bezier(0.895, 0.03, 0.685, 0.22); +@ease-in-quint: cubic-bezier(0.755, 0.05, 0.855, 0.06); +@ease-in-expo: cubic-bezier(0.95, 0.05, 0.795, 0.035); +@ease-in-circ: cubic-bezier(0.6, 0.04, 0.98, 0.335); + +/* ease-out corresponds to cubic-bezier(0, 0, 0.58, 1.0) */ +@ease-out-quad: cubic-bezier(0.25, 0.46, 0.45, 0.94); +@ease-out-cubic: cubic-bezier(0.215, 0.61, 0.355, 1); +@ease-out-quart: cubic-bezier(0.165, 0.84, 0.44, 1); +@ease-out-quint: cubic-bezier(0.23, 1, 0.32, 1); +@ease-out-expo: cubic-bezier(0.19, 1, 0.22, 1); +@ease-out-circ: cubic-bezier(0.075, 0.82, 0.165, 1); + +/* ease-in-out corresponds to cubic-bezier(0.42, 0, 0.58, 1.0) */ +@ease-in-out-quad: cubic-bezier(0.455, 0.03, 0.515, 0.955); +@ease-in-out-cubic: cubic-bezier(0.645, 0.045, 0.355, 1); +@ease-in-out-quart: cubic-bezier(0.77, 0, 0.175, 1); +@ease-in-out-quint: cubic-bezier(0.86, 0, 0.07, 1); +@ease-in-out-expo: cubic-bezier(1, 0, 0, 1); +@ease-in-out-circ: cubic-bezier(0.785, 0.135, 0.15, 0.86); + +/* custom easing */ +@ease-out-quick: cubic-bezier(0, 1, 0, 1); diff --git a/app/src/env.ts b/app/src/env.ts index f81f385..13408f7 100644 --- a/app/src/env.ts +++ b/app/src/env.ts @@ -1,3 +1,4 @@ +/// /// /// diff --git a/app/src/main.ts b/app/src/main.ts index 6c8956a..ec85176 100644 --- a/app/src/main.ts +++ b/app/src/main.ts @@ -1,7 +1,6 @@ import type {NfpxExports} from './env'; import type {SecretBech32} from '@solar-republic/neutrino'; -import {base64_to_buffer, buffer_to_base93} from '@blake.regalia/belt'; import {create_svg, create_html, ls_write, ls_write_b64, ls_read_b64} from '@nfps.dev/runtime'; import {Wallet, gen_sk, exec_contract} from '@solar-republic/neutrino'; @@ -26,9 +25,11 @@ const dm_pause = create_html('button', { 'Pause', ]); -const dm_app = create_html('div'); +const dm_app = create_html('div', { + id: 'app', +}); -dm_root.append(create_svg('foreignObject', { +const dm_foreign = create_svg('foreignObject', { width: '100%', height: '100%', x: '0', @@ -38,7 +39,9 @@ dm_root.append(create_svg('foreignObject', { // create_html('div', {}, [ // dm_pause, // ]), -])); +]); + +dm_root.append(dm_foreign); dm_pause.onclick = () => { dm_pause.textContent = dm_root.classList.toggle('paused')? 'Resume': 'Pause'; @@ -92,6 +95,7 @@ export { SA_OWNER, exec_contract, dm_root, + dm_foreign, }; export default interface Default extends NfpxExports { diff --git a/app/src/webext-portal.ts b/app/src/webext-portal.ts new file mode 100644 index 0000000..5f63fd5 --- /dev/null +++ b/app/src/webext-portal.ts @@ -0,0 +1,14 @@ +import type {ComcClient, ComcClientHandlers} from '@nfps.dev/runtime'; +import type {HttpsUrl} from '@solar-republic/neutrino'; + +import {comcPortal, comcClient} from '@nfps.dev/runtime'; + +export async function WebextPortal(h_handlers: ComcClientHandlers, p_host: HttpsUrl='https://x.s2r.sh/'): Promise { + const { + dm_foreign, + } = destructureImportedNfpModule('app'); + + const dm_iframe = await comcPortal(p_host, dm_foreign); + + return comcClient(dm_iframe, h_handlers); +} diff --git a/app/vite.config.ts b/app/vite.config.ts index 63b0a1e..e494b40 100644 --- a/app/vite.config.ts +++ b/app/vite.config.ts @@ -2,7 +2,7 @@ import {resolve} from 'path'; import {nfpModule} from '@nfps.dev/sdk'; -import {defineConfig} from 'vite'; +import {defineConfig, type Plugin} from 'vite'; export default defineConfig(({mode}) => { const B_DEV = 'development' === mode; @@ -37,7 +37,7 @@ export default defineConfig(({mode}) => { module: 'esnext', }, svelte: {}, - }), + }) as Plugin[], ], }; }); diff --git a/applibs/src/storage/main.ts b/applibs/src/storage/main.ts index 2d99515..5b0c326 100644 --- a/applibs/src/storage/main.ts +++ b/applibs/src/storage/main.ts @@ -113,6 +113,28 @@ export async function readOwner< } +// /** +// * Reads from the contract's owner storage +// * @returns an `object` of the key/value pairs contained in the response, or `undefined` if there was +// * a query error +// */ +// export async function readOwnerSelf< +// const a_keys extends readonly string[], +// >(a_keys: a_keys): Promise<{ +// [si_key in L.UnionOf]: string; +// } | void | undefined> { +// // perform query on contract +// const [g_storage,, s_error] = await query_contract_infer(K_CONTRACT, 'storage_owner_get', { +// keys: a_keys, +// }, G_QUERY_PERMIT); + +// // restructure response +// return g_storage? ofe((g_storage.data || []).map(g => [g.key, g.value])) as { +// [si_key in L.UnionOf]: string; +// }: alert(s_error); +// } + + /** * Writes to the contract's owner storage */ @@ -131,6 +153,7 @@ export async function writeOwner(h_write: Record): Promise + +
+ +
+
\ No newline at end of file diff --git a/media/template.svg b/media/template.svg index 63a2c4e..76bc58b 100644 --- a/media/template.svg +++ b/media/template.svg @@ -77,19 +77,20 @@ - + - + - - + + + diff --git a/package.json b/package.json index 1e1f9dd..7968daa 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,9 @@ "@blake.regalia/eslint-config-elite": "^0.2.10", "@blake.regalia/tsconfig": "^0.1.2", "@cosmjs/amino": "^0.30.1", + "@fortawesome/fontawesome-svg-core": "^6.4.0", + "@fortawesome/free-solid-svg-icons": "^6.4.0", + "@keplr-wallet/types": "^0.11.64", "@nfps.dev/rollup-plugin-microweb": "^0.3.5", "@nfps.dev/sdk": "^0.6.0", "@rollup/plugin-replace": "^5.0.2", From ff831107c7d94e1707d4e32b6cc3796c122a9bf3 Mon Sep 17 00:00:00 2001 From: Blake Regalia Date: Sun, 28 May 2023 13:09:42 -0700 Subject: [PATCH 4/9] chore: deps --- app/src/App.svelte | 2 -- package.json | 18 +++++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/src/App.svelte b/app/src/App.svelte index f5e3c6b..d7d79ce 100644 --- a/app/src/App.svelte +++ b/app/src/App.svelte @@ -76,8 +76,6 @@ s_action = S_ACTION_SAVE; } - debugger; - b_loading = false; })(); diff --git a/package.json b/package.json index 7968daa..1241a3f 100644 --- a/package.json +++ b/package.json @@ -37,10 +37,10 @@ "@fortawesome/free-solid-svg-icons": "^6.4.0", "@keplr-wallet/types": "^0.11.64", "@nfps.dev/rollup-plugin-microweb": "^0.3.5", - "@nfps.dev/sdk": "^0.6.0", + "@nfps.dev/sdk": "^0.6.2", "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-typescript": "^11.1.1", - "@types/node": "^20.2.4", + "@types/node": "^20.2.5", "@types/web": "^0.0.99", "@typescript-eslint/parser": "^5.59.7", "dotenv": "^16.0.3", @@ -50,22 +50,22 @@ "less": "^4.1.3", "rollup": "^3.23.0", "rollup-plugin-inject-process-env": "^1.3.1", - "rollup-plugin-svelte": "^7.1.4", + "rollup-plugin-svelte": "^7.1.5", "rollup-plugin-typescript2": "^0.34.1", "svelte": "^3.59.1", - "svelte-check": "^3.3.2", - "svelte-preprocess": "^5.0.3", + "svelte-check": "^3.4.3", + "svelte-preprocess": "^5.0.4", "ts-node": "^10.9.1", "ts-toolbelt": "^9.6.0", "typescript": "^5.0.4", - "vite": "^4.3.8", + "vite": "^4.3.9", "xml-formatter": "^3.4.1", "yargs": "^17.7.2" }, "dependencies": { - "@nfps.dev/runtime": "^0.9.4", - "@solar-republic/neutrino": "^0.4.0", - "@sveltejs/vite-plugin-svelte": "^2.3.0", + "@nfps.dev/runtime": "^0.10.0", + "@solar-republic/neutrino": "^0.4.1", + "@sveltejs/vite-plugin-svelte": "^2.4.1", "idb-keyval": "^6.2.1" } } From c5ee0ebbbe0962246b3c46c317d033e48f03a8f8 Mon Sep 17 00:00:00 2001 From: Blake Regalia Date: Sun, 28 May 2023 16:01:38 -0700 Subject: [PATCH 5/9] chore: upgrade deps --- app/src/Wallet.svelte | 3 ++- package.json | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/Wallet.svelte b/app/src/Wallet.svelte index 74f1f1c..6a59e37 100644 --- a/app/src/Wallet.svelte +++ b/app/src/Wallet.svelte @@ -65,7 +65,7 @@ 'Account Details': [() => ({ 'Address of this account': SA_WALLET, }), faUser], - 'Add Funds to Wallet': [() => request_feegrant(reset_menu), faHandHoldingDollar], + 'Add Funds to Wallet': [() => (request_feegrant(reset_menu), null), faHandHoldingDollar], 'Transaction History': [() => null, faReceipt], 'App Information': [() => ({ 'View Packages': [() => ({ @@ -82,6 +82,7 @@ 'Where did my account come from?': 'Neutrino securely generated this account on your device.', 'How do I back up or export my private key?': `YOU DON'T. Think of it as a "burner" account.`, 'How do I send funds to this account?': `YOU DON'T. Instead, this account can be funded using Feegrants, which allow it to spend gas from another account. Follow instructions to "Fund Wallet" from the main menu.`, + 'Developed by': 'Blake Regalia, founder of StarShell Wallet, Solar Republic LLC', }), faCircleInfo], }; diff --git a/package.json b/package.json index 1241a3f..8b25010 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "@fortawesome/free-solid-svg-icons": "^6.4.0", "@keplr-wallet/types": "^0.11.64", "@nfps.dev/rollup-plugin-microweb": "^0.3.5", - "@nfps.dev/sdk": "^0.6.2", + "@nfps.dev/sdk": "^0.6.3", "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-typescript": "^11.1.1", "@types/node": "^20.2.5", @@ -63,7 +63,7 @@ "yargs": "^17.7.2" }, "dependencies": { - "@nfps.dev/runtime": "^0.10.0", + "@nfps.dev/runtime": "^0.10.1", "@solar-republic/neutrino": "^0.4.1", "@sveltejs/vite-plugin-svelte": "^2.4.1", "idb-keyval": "^6.2.1" From 229b38cd432a35c50d0251013effa27b26596b42 Mon Sep 17 00:00:00 2001 From: Blake Regalia Date: Sun, 10 Sep 2023 15:48:27 -0700 Subject: [PATCH 6/9] dev: use nfp cli, update wallet --- .env.example | 11 - .nfp-modules.json | 46 ++- README.md | 49 +++- app/src/App.svelte | 43 +-- app/src/Notifications.svelte | 27 ++ app/src/Wallet.svelte | 531 +++++++++++++++++++++++++++++------ app/src/main.ts | 15 +- app/src/webext-portal.ts | 10 +- applibs/rollup.config.ts | 4 +- applibs/src/storage/main.ts | 1 - bootloader/rollup.config.ts | 4 +- bootloader/src/_autoboot.ts | 2 + bootloader/src/bootloader.ts | 9 +- bootloader/src/env.ts | 1 + build.mjs | 1 + deploy.ts | 247 ---------------- package.json | 54 ++-- 17 files changed, 631 insertions(+), 424 deletions(-) delete mode 100644 .env.example create mode 100644 app/src/Notifications.svelte delete mode 100644 deploy.ts diff --git a/.env.example b/.env.example deleted file mode 100644 index fc94449..0000000 --- a/.env.example +++ /dev/null @@ -1,11 +0,0 @@ -NFP_WEB_LCDS="https://lcd.testnet.secretsaturn.net,https://api.pulsar.scrttestnet.com" -NFP_WEB_COMCS="http://localhost:8080/" -NFP_SELF_CHAIN="pulsar-2" -NFP_SELF_CONTRACT="secret1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -NFP_SELF_TOKEN="123" - -# used in autoboot dev mode -NFP_VIEWING_KEY="password123" - -# leave empty to generate one by running `node deploy.mjs` -NFP_WALLET_PRIVATE_KEY="" diff --git a/.nfp-modules.json b/.nfp-modules.json index 995f439..06d33ee 100644 --- a/.nfp-modules.json +++ b/.nfp-modules.json @@ -3,8 +3,8 @@ "comment": "Do not edit or delete this generated file! It should also be committed to version control.", "modules": { "bootloader": { - "version": "0.0", - "hash": "c0c961f9de8e", + "version": "0.2", + "hash": "5d66fb5ccabb", "exports": { ".": { "A_TOKEN_LOCATION": { @@ -44,14 +44,24 @@ }, "query_contract_infer": { "symbol": "k" + }, + "nfp_tags": { + "symbol": "l" + }, + "nfp_attr": { + "symbol": "m" + }, + "P_COMC_HOST": { + "symbol": "n", + "dynamic": true } } }, "dependencies": {} }, "app": { - "version": "0.3", - "hash": "780935860a87", + "version": "0.6", + "hash": "8b9b83fd27cb", "exports": { ".": { "SA_OWNER": { @@ -69,13 +79,29 @@ }, "dm_foreign": { "symbol": "e" + }, + "A_RPCS": { + "symbol": "f" + }, + "P_COMC_HOST": { + "symbol": "g", + "dynamic": true + }, + "A_COMCS": { + "symbol": "h" } } }, "dependencies": { + "storage": { + "version": "0.1", + "hash": "c87091d4dc20", + "dynamic": true, + "imports": {} + }, "bootloader": { - "version": "0.0", - "hash": "c0c961f9de8e" + "version": "0.2", + "hash": "5d66fb5ccabb" } } }, @@ -97,12 +123,12 @@ }, "dependencies": { "app": { - "version": "0.2", - "hash": "4b1ca6bbcb72" + "version": "0.6", + "hash": "8b9b83fd27cb" }, "bootloader": { - "version": "0.0", - "hash": "c0c961f9de8e" + "version": "0.2", + "hash": "5d66fb5ccabb" } } } diff --git a/README.md b/README.md index 6b9de7b..18aa802 100644 --- a/README.md +++ b/README.md @@ -43,22 +43,34 @@ The root directory contains the `package.json` for managing the dependencies use yarn install ``` -2. Set up environment variables +2. Install the nfp cli ```sh - cp .env.example .env + yarn global add @nfps.dev/cli ``` -3. Generate or import a private key +3. Set up environment variables - You can generate a new account by simply running `node deploy.mjs` without a value set for the private key env var. + ```sh + nfp init + ``` - Or, you can import a private key from `secretcli` using the following command: +4. Import the account of an admin/minter + + You can import a private key from `secretcli` using the following command: ```sh - secretcli keys export ACCOUNT_NAME --unarmored-hex --unsafe + sed -i'' "s/^NFP_WALLET_PRIVATE_KEY=.*/NFP_WALLET_PRIVATE_KEY=\"$(secretcli keys export "${ACCOUNT_NAME}" --unarmored-hex --unsafe)\"/" .env ``` -4. Build everything + Mint a new token from this imported account: + ```sh + nfp mint "${SOME_TOKEN_ID}" + ``` + + On success, the above command will update the env vars `NFP_OWNER` and `NFP_TOKEN_ID` in the `.env` file. + + +5. Build everything For production: ```sh @@ -74,13 +86,30 @@ The root directory contains the `package.json` for managing the dependencies use > While developing, you can use `yarn watch:dev` to automatically reload on file changes -5. Deploy the app to chain +6. Deploy the app to chain ```sh - yarn deploy:app # simply calls `node deploy.mjs` + nfp set-vk "password123" + nfp package upload dist/app.js + nfp package upload dist/storage.js --tags 1.x latest + nfp storage owner put 'foo: "bar", baz: 25' ``` -6. Open the built SVG file in a web browser (`file://` protocol works!) or preview in no-script mode using other means. +7. Open the built SVG file in a web browser (`file://` protocol works!) or preview in no-script mode using other means. + +8. Configure a COMC host + + If operating offline, or `x.s2r.sh` is not available, run a local COMC host and update your `.env` file accordingly: e.g., `NFP_COMC_HOST="http://localhost:8080/"`. A COMC host is a page served from an HTTP(S) URL that is capable of communicating with Keplr on behalf of the NFP when it is served from the `file://` protocol (Keplr only injects a content-script into HTTP(S) tabs). + +9. Connect your web extension wallet to the app + + If using Keplr: + - for the testnet, make sure to accept the prompt to add the `pulsar-3` testnet chain + - reload the page and approve the connection request from the COMC host (e.g., `https://x.s2r.sh` or `http://localhost:8080`) + - make sure your wallet is funded. for the testnet: https://faucet-ui-pulsar3.vercel.app/ + +10. Follow instructions to "Grant Allowance". This allows the Neutrino hot hot wallet to pay tx fees using the account of your web wallet. + Outputs: - `dist/nfp.svg` - the built and minified SVG file diff --git a/app/src/App.svelte b/app/src/App.svelte index d7d79ce..c7d87e1 100644 --- a/app/src/App.svelte +++ b/app/src/App.svelte @@ -7,10 +7,7 @@ ls_write, } from 'nfpx:bootloader'; - // we can't reference the 'storage' module using an import statement since it is loaded dyanmically - // instead, we will use a destructuring assignment to de-alias its imported members - // to maintain strong typing, import the _default_ types from the 'storage' module - + import Notifications from './Notifications.svelte'; import Wallet from './Wallet.svelte'; // before 'App.svelte' is instantiated, 'main.ts' must have successfully loaded the storage module @@ -113,25 +110,30 @@ } fieldset { - border: 0; - } - - input,button { - background: #333; - color: #ce3; - padding: 8px 12px; - border: 0; - - &:focus { - outline: 1px solid orange; - border-radius: 2px; + :global(&) { + border: 0; } - &:disabled { - opacity: 0.8; + input, button { + :global(&) { + background: #333; + color: #ce3; + padding: 8px 12px; + border: 0; + + &:focus { + outline: 1px solid orange; + border-radius: 2px; + } + + &:disabled { + opacity: 0.8; + } + } } } + :global(.flex) { display: flex; } @@ -147,13 +149,16 @@ {b_loading? 'Loading...': b_writable? 'Synced': 'Awaiting Wallet'}
+
+ disabled={b_locked} bind:value={s_name} placeholder={s_placeholder}>
+ + diff --git a/app/src/Notifications.svelte b/app/src/Notifications.svelte new file mode 100644 index 0000000..c7d95ce --- /dev/null +++ b/app/src/Notifications.svelte @@ -0,0 +1,27 @@ + + +
+
+ + +
+
diff --git a/app/src/Wallet.svelte b/app/src/Wallet.svelte index 6a59e37..b7a9ebd 100644 --- a/app/src/Wallet.svelte +++ b/app/src/Wallet.svelte @@ -1,9 +1,12 @@ +``` diff --git a/README.md b/README.md index 18aa802..9337af8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ NFP front-end development is divided into two subdirectories: the bootloader, an ### Bootloader -For maximum portability, robustness, and longevity, a bootloader is a tiny minified script that fetches the NFP's latest packages from the chain and hydrates the running SVG document (e.g., by injecting scripts). The bootloader ensures that the raw, original SVG file can forever be opened and run as a standalone web application in any modern browser. +For maximum portability, robustness, and longevity, a bootloader is a tiny minified script that fetches the NFP's latest packages from the chain and hydrates the running SVG document (i.e., by injecting script elements). The bootloader ensures that the raw, original SVG file can forever be opened and run as a standalone web application in any modern browser. The `@nfps.dev/runtime` package provides a function that can be called by the project's bootloader to automatically handle resolving the NFP's dependencies by reading the requisite children of the `` element. This approach allows project's to add some custom UI or logic to their bootloader flow. @@ -24,6 +24,11 @@ Unlike the bootloader, a project's main application can be updated by deploying The SVG stored on chain references a package by its id and a tag, e.g., ``. +### Modules + +See [MODULES.md](./MODULES.md) for documentation on the NFP module system. + + ## Directory Structure The root directory contains the `package.json` for managing the dependencies used by `app/` and `bootloader/`. diff --git a/app/src/App.svelte b/app/src/App.svelte index c7d87e1..ab0ad0f 100644 --- a/app/src/App.svelte +++ b/app/src/App.svelte @@ -1,7 +1,7 @@ ``` From ccde6f3d9b00339c46aa32545e7fa99d7d99ea49 Mon Sep 17 00:00:00 2001 From: Blake Regalia Date: Thu, 5 Oct 2023 22:43:59 -0700 Subject: [PATCH 9/9] docs: troubleshooting --- MODULES.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/MODULES.md b/MODULES.md index 972085b..3babaf7 100644 --- a/MODULES.md +++ b/MODULES.md @@ -41,7 +41,7 @@ If a module has not yet been loaded into the SVG document but the script depends ## Dynamic Imports -To load a module into the app from a script, the module's source code needs to be downloaded from the chain and appended to the document as a ` ``` + + +### Troubleshooting + +In some cases, `destructureImportedNfpModule` may show a type error about its argument. This can happen for instance if a svelte component is attempting to destructure members exported by its own parent module, but that parent isn't a dependency of any other modules in the project. Therefore, the global augmentation from that module's type definitions never get loaded. To fix this, simply add a type-only import to the entry file, importing its own type definitions: + +```ts +/* app/src/main.ts */ +import type {} from 'nfpx:app'; +``` + +#### Importing from self + +Another issue that can arise when using `destructureImportedNfpModule` to import items from the same (current) module, is that the identifier is not found in the module's exports: +``` +"{IDENTIFIER}" export not found in app module +``` + +This can happen because the SDK is trying to find the identifier in the module's compiled export table, but the module hasn't compiled yet. In this scenario, you need to first compile the module with the new export before using that identifier in any destructuring calls from within the same module. + +```ts +/* app/src/main.ts */ + +(async() => { + await someAsyncTask(); + + exportNfpx({ + FOOBAR: 'data', + }); +}); +``` + +```ts +/* app/src/App.svelte */ +const { + FOOBAR, // causing error: "FOOBAR" export not found in app module +} = destructureImportedNfpModule('app'); +``` + +**Solution:** + +Temporarily disable the import and compile. + +```ts +/* app/src/App.svelte */ +const { + // FOOBAR, +} = destructureImportedNfpModule('app'); + +// temporary decl to satisfy type-checker and linters +let FOOBAR!: string; +``` + +Now, `FOOBAR` should be defined in the app's exports in `.nfp-modules.json`. You should be able to revert the file and compile.