Add NIP-46 app metadata with origin, icon and permissions
Marco (host) as app name, absolute icon URL, requested signing kinds
This commit is contained in:
3 files changed
+138
-6
No files matched your search
@@ -6,6 +6,11 @@ import {
|
||||
PrivateKeySigner,
|
||||
} from 'applesauce-signers';
|
||||
|
||||
import {
|
||||
buildNostrAppMetadata,
|
||||
MARCO_SIGNING_KINDS,
|
||||
} from '../utils/nostr-app-metadata';
|
||||
|
||||
const STORAGE_KEY = 'marco:nostr_pubkey';
|
||||
const STORAGE_KEY_TYPE = 'marco:nostr_signer_type'; // 'extension' | 'connect'
|
||||
const STORAGE_KEY_CONNECT_LOCAL_KEY = 'marco:nostr_connect_local_key';
|
||||
@@ -78,6 +83,18 @@ export default class NostrAuthService extends Service {
|
||||
return isMobile();
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-46 client metadata: name/url/image are display hints for the signer
|
||||
* app, and the requested permissions let it pre-approve what Marco signs.
|
||||
*/
|
||||
get _appMetadata() {
|
||||
return {
|
||||
...buildNostrAppMetadata(),
|
||||
permissions:
|
||||
NostrConnectSigner.buildSigningPermissions(MARCO_SIGNING_KINDS),
|
||||
};
|
||||
}
|
||||
|
||||
get isConnected() {
|
||||
return (
|
||||
!!this.pubkey &&
|
||||
@@ -150,10 +167,19 @@ export default class NostrAuthService extends Service {
|
||||
const relay = DEFAULT_CONNECT_RELAY;
|
||||
localStorage.setItem(STORAGE_KEY_CONNECT_RELAY, relay);
|
||||
|
||||
// NIP-46 client metadata: name/url/image are display hints for the signer
|
||||
// app, and the requested permissions let it pre-approve what Marco signs.
|
||||
// This flow never sends a `connect` request, so the metadata only travels
|
||||
// in the nostrconnect:// URI below.
|
||||
const metadata = this._appMetadata;
|
||||
|
||||
this._signerInstance = new NostrConnectSigner({
|
||||
pool: this.nostrRelay.pool,
|
||||
relays: [relay],
|
||||
signer: localSigner,
|
||||
// Also the default for getNostrConnectURI(), and used if a future
|
||||
// bunker:// flow sends a `connect` request.
|
||||
metadata,
|
||||
onAuth: async (url) => {
|
||||
// NIP-46 auth callback. Normally the signer app does this natively via notification.
|
||||
// But if it requires an explicit browser window:
|
||||
@@ -168,12 +194,7 @@ export default class NostrAuthService extends Service {
|
||||
});
|
||||
|
||||
// Set the uri for display (e.g., to redirect via intent)
|
||||
this.connectUri = this._signerInstance.getNostrConnectURI({
|
||||
name: 'Marco',
|
||||
url: window.location.origin,
|
||||
description: 'An unhosted maps application.',
|
||||
icons: [],
|
||||
});
|
||||
this.connectUri = this._signerInstance.getNostrConnectURI(metadata);
|
||||
|
||||
// Trigger the deep link intent immediately for the user if on mobile
|
||||
if (this.isMobile) {
|
||||
@@ -237,6 +258,9 @@ export default class NostrAuthService extends Service {
|
||||
relays: [relay],
|
||||
signer: localSigner,
|
||||
remote: remotePubkey,
|
||||
// Kept in sync with connectWithApp(); only used if a `connect` request is
|
||||
// sent (bunker:// flow) or a URI is generated for this session.
|
||||
metadata: this._appMetadata,
|
||||
onAuth: async (url) => {
|
||||
if (
|
||||
confirm(
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* How Marco describes itself to a NIP-46 remote signer.
|
||||
*
|
||||
* These fields are unauthenticated display hints: signers may show them, but
|
||||
* must not use them for authorization. The origin is part of the app name
|
||||
* because a signer's connection list usually renders only the app name and
|
||||
* icon for a saved session; the canonical `url` is only visible on the
|
||||
* approval screen (e.g. Amber >= 6.2.2). Without it, every Marco deployment
|
||||
* would look like the same "Marco" session.
|
||||
*/
|
||||
|
||||
/** Event kinds Marco signs, requested up front so signers can pre-approve them. */
|
||||
export const MARCO_SIGNING_KINDS = [
|
||||
5, // NIP-09 deletion (removing a photo)
|
||||
360, // Place Photos
|
||||
9734, // NIP-57 zap request
|
||||
24242, // Blossom authorization (upload/delete)
|
||||
];
|
||||
|
||||
/**
|
||||
* Builds the NIP-46 client metadata for the given origin.
|
||||
*
|
||||
* The host (including a port) is part of the name, so self-hosted instances
|
||||
* and dev servers are distinguishable in the signer app. The icon has to be
|
||||
* an absolute https URL: signers fetch it from the user's device, so origins
|
||||
* the device cannot reach (localhost, LAN) show no icon.
|
||||
*
|
||||
* @param {string} [origin] Origin of the running app
|
||||
* @returns {{name: string, url: string, image: string}}
|
||||
*/
|
||||
export function buildNostrAppMetadata(origin = window.location.origin) {
|
||||
const { host, origin: canonicalOrigin } = new URL(origin);
|
||||
|
||||
return {
|
||||
name: `Marco (${host})`,
|
||||
url: canonicalOrigin,
|
||||
image: `${canonicalOrigin}/icons/icon-192.png`,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { NostrConnectSigner } from 'applesauce-signers';
|
||||
import {
|
||||
createNostrConnectURI,
|
||||
parseNostrConnectURI,
|
||||
} from 'applesauce-signers/helpers/nostr-connect';
|
||||
import {
|
||||
buildNostrAppMetadata,
|
||||
MARCO_SIGNING_KINDS,
|
||||
} from 'marco/utils/nostr-app-metadata';
|
||||
|
||||
module('Unit | Utility | nostr-app-metadata', function () {
|
||||
test('builds name, url and image from the origin', function (assert) {
|
||||
const metadata = buildNostrAppMetadata('https://marco.kosmos.org');
|
||||
|
||||
assert.deepEqual(metadata, {
|
||||
name: 'Marco (marco.kosmos.org)',
|
||||
url: 'https://marco.kosmos.org',
|
||||
image: 'https://marco.kosmos.org/icons/icon-192.png',
|
||||
});
|
||||
});
|
||||
|
||||
test('includes the port so self-hosted and dev instances differ', function (assert) {
|
||||
const metadata = buildNostrAppMetadata('http://localhost:5173');
|
||||
|
||||
assert.strictEqual(metadata.name, 'Marco (localhost:5173)');
|
||||
assert.strictEqual(metadata.url, 'http://localhost:5173');
|
||||
});
|
||||
|
||||
test('normalizes the origin', function (assert) {
|
||||
const metadata = buildNostrAppMetadata('https://marco.kosmos.org/');
|
||||
|
||||
assert.strictEqual(metadata.url, 'https://marco.kosmos.org');
|
||||
assert.strictEqual(
|
||||
metadata.image,
|
||||
'https://marco.kosmos.org/icons/icon-192.png'
|
||||
);
|
||||
});
|
||||
|
||||
test('the connect URI carries the metadata and requested permissions', function (assert) {
|
||||
const metadata = {
|
||||
...buildNostrAppMetadata('https://marco.kosmos.org'),
|
||||
permissions:
|
||||
NostrConnectSigner.buildSigningPermissions(MARCO_SIGNING_KINDS),
|
||||
};
|
||||
|
||||
const uri = createNostrConnectURI({
|
||||
client: 'a'.repeat(64),
|
||||
connectSecret: 'test-secret',
|
||||
relays: ['wss://relay.example'],
|
||||
metadata,
|
||||
});
|
||||
const parsed = parseNostrConnectURI(uri);
|
||||
|
||||
assert.strictEqual(parsed.metadata.name, 'Marco (marco.kosmos.org)');
|
||||
assert.strictEqual(parsed.metadata.url, 'https://marco.kosmos.org');
|
||||
assert.strictEqual(
|
||||
parsed.metadata.image,
|
||||
'https://marco.kosmos.org/icons/icon-192.png'
|
||||
);
|
||||
assert.deepEqual(parsed.metadata.permissions, [
|
||||
'get_public_key',
|
||||
'sign_event:5',
|
||||
'sign_event:360',
|
||||
'sign_event:9734',
|
||||
'sign_event:24242',
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user