-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: encode record-store keys in pubsub (#9)
- Loading branch information
1 parent
068ec27
commit 510bd2c
Showing
4 changed files
with
80 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
'use strict' | ||
|
||
const base32 = require('base32.js') | ||
const multibase = require('multibase') | ||
const errcode = require('err-code') | ||
|
||
const namespace = '/record/' | ||
const base64urlCode = 'u' // base64url code from multibase | ||
|
||
module.exports.encodeBase32 = (buf) => { | ||
const enc = new base32.Encoder() | ||
return enc.write(buf).finalize() | ||
return multibase.encode('base32', buf).slice(1) // slice off multibase codec | ||
} | ||
|
||
// converts a binary record key to a pubsub topic key. | ||
module.exports.keyToTopic = (key) => { | ||
// Record-store keys are arbitrary binary. However, pubsub requires UTF-8 string topic IDs | ||
// Encodes to "/record/base64url(key)" | ||
const b64url = multibase.encode('base64url', key).slice(1).toString() | ||
|
||
return `${namespace}${b64url}` | ||
} | ||
|
||
// converts a pubsub topic key to a binary record key. | ||
module.exports.topicToKey = (topic) => { | ||
if (topic.substring(0, namespace.length) !== namespace) { | ||
throw errcode(new Error('topic received is not from a record'), 'ERR_TOPIC_IS_NOT_FROM_RECORD_NAMESPACE') | ||
} | ||
|
||
const key = `${base64urlCode}${topic.substring(namespace.length)}` | ||
|
||
return multibase.decode(key).toString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters