Skip to content

Example #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,49 @@ This module:
var SecretStack = require('secret-stack')
var databasePlugin = require('./some-database')
var bluetoothPlugin = require('./bluetooth')
var config = require('./some-config')
const ssbKeys = require('ssb-keys')
var keys = ssbKeys.loadOrCreateSync(path.join(__dirname, 'secret'))
var keysTwo = ssbKeys.loadOrCreateSync(path.join(__dirname, 'secret-two'))
var myPlugin = require('./my-plugin)

// keys are necessary for multiserver address
var config = { keys }

var App = SecretStack({ appKey: '1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=' })
.use(databasePlugin)
.use(bluetoothPlugin)
.use(myPlugin)

var app = App(config)
const addr = app.getAddress()


// ... in a different process ...

var SecondApp = SecretStack({
appKey: '1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=',
// TODO -- what is permissions?
// permissions: {}
})
.use(myPlugin)

const secondApp = SecondApp({ keys: keysTwo })

secondApp.connect(addr, (err, rpc) => {
console.log('*connected*', err)

rpc.myPlugin.foo((err, res) => {
console.log('*foo reponse*', err, res)

app.close(null, (err) => {
console.log('closed', err)
secondApp.close(null, err => {
console.log('closed 2', err)
})
})
})
})
```


For documentation on plugins, see [PLUGINS.md](./PLUGINS.md).


Expand Down
93 changes: 93 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const SecretStack = require('./lib')
const ssbKeys = require('ssb-keys')
const S = require('pull-stream')
const path = require('path')
var keys = ssbKeys.loadOrCreateSync(path.join(__dirname, 'secret'))
var keysTwo = ssbKeys.loadOrCreateSync(path.join(__dirname, 'secret-two'))
// var Perms = require('muxrpc/permissions')

var App = SecretStack({
appKey: '1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=',
// permissions: {}
})
.use(createMyPlugin('one'))

const config = { keys }
var app = App(config)

const addr = app.getAddress()

// app.auth.hook(function (auth, args) {
// console.log('hook', auth, args)
// var cb = args.pop()
// var id = args.shift()
// auth(id, function (err, perms) {
// if (err) return cb(err)
// console.log('hook auth', id, err, perms)
// cb(null, { allow: ['foo', 'bar'] })
// })
// })

// ... in a different process ...
var SecondApp = SecretStack({
appKey: '1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=',
// permissions: {}
})
.use(createMyPlugin('two'))

const secondApp = SecondApp({ keys: keysTwo })

secondApp.connect(addr, (err, rpc) => {
console.log('*connected*', err)
console.log('*rpc keys*', Object.keys(rpc.myPlugin))

rpc.myPlugin.foo((err, res) => {
console.log('*foo reponse*', err, res)

app.close(null, (err) => {
console.log('closed', err)
secondApp.close(null, err => {
console.log('closed 2', err)
})
})
})
})


// var perms = Perms({ allow: ['foo', 'bar'] })
// console.log('perms', perms)


function createMyPlugin (str) {
return {
name: 'myPlugin',
version: '0.0.0',
manifest: {
foo: 'async',
bar: 'source'
},

// permissions: perms,
// permissions: {
// anonymous: { allow: ['foo', 'bar'], deny: [] }
// },

init: (api, opts) => {
// .. do things

// return things promised by the manifest:
return {
foo: function (cb) { // an async function (takes a callback)
process.nextTick(() => {
cb(null, 'foo' + '-' + str)
})
},

// a function which returns a pull-stream source
bar: function () {
return S.values([1,2,3])
}
}
}
}
}
38 changes: 38 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
the `connect` function -- https://github.com/ssb-js/secret-stack/blob/76d1432ac2db60ac14fa986eed84910dacd45ea6/src/core.ts#L293 -- calls setupRpc with `isClient` set to true.

So whichever muxrpc initiates the connection is the client.

---------------------------------------------------

_unraveling the secret-stack_

The `connect` function calls `setupRPC`, which calls `Muxrpc` with `isClient` set to true. So whichever `secret-stack` calls `connect` on an address is the client, and if we look here — https://github.com/ssb-js/secret-stack/blob/76d1432ac2db60ac14fa986eed84910dacd45ea6/src/core.ts#L233

you can see the permissions uses `anonymous`

```js
const rpc = Muxrpc(
manifest,
manf ?? manifest,
api,
_id,
isClient
? permissions.anonymous
: isPermissions(stream.auth)
? stream.auth
: permissions.anonymous,
false
)
```

and the `permissions` object is passed into the function `init`.


where ssb-conn calls secret-stack's `.connect` :
https://github.com/staltz/ssb-conn-hub/blob/master/src/index.ts#L243

```js
var [err, rpc] = connect

this._rpcs.set(address, rpc);
```
Loading