@keeex/js-keys
v4.6.1
Published
Manage keys for digital signatures
Downloads
241
Readme
KeeeX Key handling library
This library provide a Keys class allowing easy generation, exportation, importation and usage of cryptographic keys for the purpose of digital signature.
Currently only support the "Bitcoin" type of keys, which exposes their public key as a Bitcoin address.
Quick how-to
The @keeex/js-keys
library uses the @keeex/crypto
library to generate
cryptographically secure random keys.
Initialize crypto provider
To make sure the correct cryptographic is imported before using this library. See the package
@keeex/crypto
for more details about provider.
import "@keeex/crypto-provider-node";
Manipulating keys
To generate a new key:
import {
generateKey,
KeyType,
} from "@keeex/js-keys/lib/keys";
generateKey(KeyType.bitcoin).then(key => console.log(key));
To export a key as JSON:
const key = /* get your key object */
key.exportKey(false, {type: "json"}).then(
exportedData => console.log(Buffer.from(exportedData).toString())
);
exportedData
is an ArrayBuffer. In the case of a JSON export the ArrayBuffer
will contain text data.
The first argument of exportKey()
is "publicOnly", to only export public data.
To import a key from a previous JSON export:
import {
importKey,
KeyType,
} from "@keeex/js-keys/lib/keys";
importKey(KeyType.bitcoin, {type:"json", value: keyData})
.then(key => console.log(key));
To export a key protected by a password:
const key = /* get your key object */
key.exportKey(
false,
{
type: "json",
password: "somepass",
}
)
).then(secureKey => ...);
To import a key protected by a password:
import {
importKey,
KeyType,
} from "@keeex/js-keys/lib/keys";
importKey(KeyType.bitcoin, {
type: "json",
password: "somepass",
value: previouslySealedKey,
});
Signature/verification
To sign:
key.sign(data).then(signature => ...);
Both data and signature are ArrayBuffer
To verify:
key.verify(data, signature).then(signOk => ...);
Both data and signature are ArrayBuffer; signOk is a boolean.
In the case of bitcoin-message signature, it is possible to convert the signature to a string.
Data encryption/decryption
It is possible to encrypt data only knowing the recipient's public key.
There are many functions and methods to tweak the process, but the main usage is:
import {Bundle} from "@keeex/js-keys/lib/bundle";
import "@keeex/crypto-provider-node";
import {generateKey, KeyType} from "@keeex/js-keys/lib/keys";
import assert from "assert";
const main = async(): Promise<void> => {
// Recipients only need publicKey part
const recipientKey1 = await generateKey(KeyType.bitcoin);
const recipientKey2 = await generateKey(KeyType.bitcoin);
// The data and metadata to encrypt
const inputData = new Uint8Array([1, 2, 3, 4]).buffer;
const inputMetadata = new Uint8Array([5, 6, 7, 8]).buffer;
const bigBundle = await Bundle.createBundle(
[
recipientKey1,
recipientKey2,
],
{
metadata: inputMetadata,
data: inputData,
},
);
const encryptedMetadataBundle = await bigBundle.saveBundle(["metadata"]);
const encryptedDataBundle = await bigBundle.saveBundle(["data"]);
// Decrypt data
// For this, the recipient key must have the private part
const readBundleMetadata = await Bundle.loadBundle(encryptedMetadataBundle);
const readBundleData = await Bundle.loadBundle(encryptedDataBundle);
const decryptedMetadata = await readBundleMetadata.getData(
recipientKey1,
"metadata",
);
const decryptedData = await readBundleData.getData(recipientKey1, "data");
assert.deepStrictEqual(decryptedMetadata, inputMetadata);
assert.deepStrictEqual(decryptedData, inputData);
console.log("Ok");
};
main().catch(e => console.error(e));