@@ -12,6 +12,12 @@ const { Router, encode, decode } = require('./spec/hyperdispatch')
1212const BlindPeering = require ( 'blind-peering' )
1313const db = require ( './spec/db/index.js' )
1414const enc = require ( 'hypercore-id-encoding' )
15+ const c = require ( 'compact-encoding' )
16+
17+ const { getEncoding } = require ( './spec/schema/index.js' )
18+
19+ const PUBLIC_INVITE_METADATA = getEncoding ( '@autopass/public-invite-metadata' )
20+ const INVITEE = getEncoding ( '@autopass/invitee' )
1521
1622class AutopassPairer extends ReadyResource {
1723 constructor ( store , invite , opts = { } ) {
@@ -29,6 +35,7 @@ class AutopassPairer extends ReadyResource {
2935 this . pass = null
3036 this . relayThrough = opts . relayThrough || null
3137 this . blindEncryption = opts . blindEncryption || null
38+ this . name = opts . name || null
3239
3340 this . ready ( ) . catch ( noop )
3441 }
@@ -54,9 +61,10 @@ class AutopassPairer extends ReadyResource {
5461 await core . ready ( )
5562 const key = core . key
5663 await core . close ( )
64+
5765 this . candidate = this . pairing . addCandidate ( {
5866 invite : z32 . decode ( this . invite ) ,
59- userData : key ,
67+ userData : c . encode ( INVITEE , { key, name : this . name } ) ,
6068 onadd : async ( result ) => {
6169 if ( this . pass === null ) {
6270 this . pass = new Autopass ( this . store , {
@@ -70,10 +78,12 @@ class AutopassPairer extends ReadyResource {
7078
7179 await this . pass . deleteInvite ( )
7280 }
73- const readOnly = JSON . parse ( result . data . toString ( ) ) . readOnly
81+
82+ const metadata = result . data ? c . decode ( PUBLIC_INVITE_METADATA , result . data ) : null
83+
7484 this . swarm = null
7585 this . store = null
76- if ( this . onresolve && readOnly ) {
86+ if ( this . onresolve && metadata && metadata . readOnly ) {
7787 this . _whenReadable ( )
7888 } else if ( this . onresolve ) {
7989 this . _whenWritable ( )
@@ -84,11 +94,7 @@ class AutopassPairer extends ReadyResource {
8494 }
8595
8696 _whenReadable ( ) {
87- const check = ( ) => {
88- this . pass . base . off ( 'update' , check )
89- this . onresolve ( this . pass )
90- }
91- this . pass . base . on ( 'update' , check )
97+ this . onresolve ( this . pass )
9298 }
9399
94100 _whenWritable ( ) {
@@ -149,11 +155,13 @@ class Autopass extends ReadyResource {
149155 this . debug = ! ! opts . key
150156 // Register handlers for commands
151157 this . router . add ( '@autopass/remove-writer' , async ( data , context ) => {
158+ await context . view . delete ( '@autopass/writer' , data )
152159 await context . base . removeWriter ( data . key )
153160 } )
154161
155162 this . router . add ( '@autopass/add-writer' , async ( data , context ) => {
156- await context . base . addWriter ( data . key )
163+ await context . view . insert ( '@autopass/writer' , data )
164+ if ( ! data . readOnly ) await context . base . addWriter ( data . key )
157165 } )
158166
159167 this . router . add ( '@autopass/put' , async ( data , context ) => {
@@ -263,7 +271,7 @@ class Autopass extends ReadyResource {
263271 const { id, invite, publicKey, expires, additional } = BlindPairing . createInvite (
264272 this . base . key ,
265273 {
266- data : Buffer . from ( JSON . stringify ( { readOnly } ) )
274+ data : c . encode ( PUBLIC_INVITE_METADATA , { readOnly } )
267275 }
268276 )
269277
@@ -293,19 +301,25 @@ class Autopass extends ReadyResource {
293301 return { value : data . value , file : data . file }
294302 }
295303
296- async addWriter ( key ) {
297- await this . base . append (
298- encode ( '@autopass/add-writer' , {
299- key : b4a . isBuffer ( key ) ? key : b4a . from ( key )
300- } )
301- )
304+ listWriters ( query ) {
305+ return this . base . view . find ( '@autopass/writer' , query )
306+ }
307+
308+ async getWriter ( query ) {
309+ return this . base . view . get ( '@autopass/writer' , query )
310+ }
311+
312+ async addWriter ( data ) {
313+ if ( typeof key === 'string' ) key = b4a . from ( key , 'hex' )
314+ if ( b4a . isBuffer ( data ) ) data = { key : data , name : null , readOnly : false }
315+ await this . base . append ( encode ( '@autopass/add-writer' , data ) )
302316 return true
303317 }
304318
305319 async removeWriter ( key ) {
306320 await this . base . append (
307321 encode ( '@autopass/remove-writer' , {
308- key : b4a . isBuffer ( key ) ? key : b4a . from ( key )
322+ key : b4a . isBuffer ( key ) ? key : b4a . from ( key , 'hex' )
309323 } )
310324 )
311325 }
@@ -337,9 +351,8 @@ class Autopass extends ReadyResource {
337351 }
338352 const readOnly = inv . readOnly
339353 candidate . open ( inv . publicKey )
340- if ( ! readOnly ) {
341- await this . addWriter ( candidate . userData )
342- }
354+ const { key, name } = c . decode ( INVITEE , candidate . userData )
355+ await this . addWriter ( { key, name, readOnly } )
343356 candidate . confirm ( {
344357 key : this . base . key ,
345358 encryptionKey : this . base . encryptionKey ,
0 commit comments