simple-signed-records-identity
Version:
cryptographic identity and content verification for decentralized apps
43 lines (34 loc) • 1.35 kB
JavaScript
/* Simple Signed Records Identity*/
import nacl from 'tweetnacl';
import niceware from 'niceware';
import ssr from 'simple-signed-records-engine';
import { decodeBase64, encodeBase64 } from 'tweetnacl-util';
import { bech32 } from 'bech32';
function createIdentity() {
const seedBytes = nacl.randomBytes(32);
const words = niceware.bytesToPassphrase(seedBytes);
const keyPair = nacl.sign.keyPair.fromSeed(seedBytes);
const secretKey = encodeBase64(keyPair.secretKey);
const publicKey = bech32.encode('ssi', keyPair.publicKey);
return { words, publicKey, secretKey };
}
function restoreIdentity(wordArr) {
const wordBytes = niceware.passphraseToBytes(wordArr);
const keyPair = nacl.sign.keyPair.fromSeed(wordBytes);
const restoredSecretKey = encodeBase64(keyPair.secretKey);
const restoredPublicKey = bech32.encode('ssi', keyPair.publicKey);
return { restoredSecretKey, restoredPublicKey };
}
function generateCert(identity) {
const record = {};
const validSeconds = 15778800;
const secretKey = decodeBase64(identity.secretKey);
const publicKey = bech32.decode(identity.publicKey);
let signedData = ssr.sign({
record,
keypair: {secretKey, publicKey},
validSeconds,
});
return btoa(JSON.stringify(signedData));
}
export default { createIdentity, restoreIdentity, generateCert };