Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tiagolr/bcevents
https://github.com/tiagolr/bcevents
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tiagolr/bcevents
- Owner: tiagolr
- Created: 2019-12-15T21:05:55.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T19:21:12.000Z (about 2 years ago)
- Last Synced: 2024-11-13T12:37:21.958Z (about 2 months ago)
- Language: JavaScript
- Size: 946 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# bcevents
Sends and listens to events broadcasted on bitcoin transactions. Events can be used to trigger remote actions or send payloads to listeners.
## Install
```
npm install bcevents datapay bsv
```## Get Started
```js
var Emitter = require('bcevents')
var emitter = new Emitter({ pkey: 'private key for tx fees' })emitter.on('my-event', console.log) // prints { ..., payload: 1234 }
emitter.emit('my-event', 1234, console.log) // prints tx hash
```## Protocol
Everytime `emit()` is called, a new transaction is broadcasted with the *event* and *payload* inside the OP_RETURN:
* OP_RETURN
* protocol_id
* event
* payload (optional)The *protocol Id* should be set when creating emitters otherwise the events will be listened by any agent using the default protocol id.
```js
emitter = new Emitter({ protocol: 'my-unique-protocol-id' })
```## Encryption
To Encrypt both *event* and *payload* on transactions, use the *enkKey* field. When set, transaction data will be encrypted and decrypted using AES.
```js
var emitter = new Emitter({ encKey: 'my-password' })
```To use a different encryption, both encrypt and decrypt function can be set:
```js
var emitter = new Emitter({
encrypt: (data) => myEncrypt(data),
decrypt: (data) => myDecrypt(data)
})
```## Examples
### Ping-Pong
```js
// endless ping-pong between two agents
// oracle.js
var emitter = new Emitter({ pkey: 'oracle pkey', protocol: 'pingpongtest' })
emitter.on('ping' () => {
emitter.emit('pong')
})// client.js
var emitter = new Emitter({ pkey: 'client pkey', protocol: 'pingpongtest' })
emitter.on('pong', () => {
emitter.emit('ping')
})
emitter.emit('ping').then(console.log)
```### Whitelisting
```js
var whitelist = [addr1, addr2, ...] // addresses whitelistemitter.on('some-event', (e) {
if (!whitelist.includes(e.from)) {
return
}
// ...
})
```