@bajetech/digitalbits-wallet-sdk
Version:
A library to make it easier to write wallets that interact with the DigitalBits blockchain
290 lines • 10.7 kB
JavaScript
import { __assign } from "tslib";
import sinon from "sinon";
import { getKeyMetadata } from "./helpers/getKeyMetadata";
import { testEncrypter, testKeyStore } from "./PluginTesting";
describe("testEncrypter", function () {
function encryptKeyGood(_a) {
var key = _a.key, password = _a.password;
return Promise.resolve(__assign(__assign({}, key), { type: undefined, publicKey: undefined, privateKey: undefined, encryptedBlob: "".concat(key.privateKey).concat(password) }));
}
function decryptKeyGood(_a) {
var encryptedKey = _a.encryptedKey, password = _a.password;
return Promise.resolve(__assign(__assign({}, encryptedKey), { encryptedBlob: undefined, encrypterName: undefined, salt: undefined, privateKey: encryptedKey.encryptedBlob.split(password)[0] }));
}
function encryptKeyInvalid(_a) {
var key = _a.key, password = _a.password;
return Promise.resolve({ key: key, password: password });
}
function decryptKeyInvalid(_a) {
var encryptedKey = _a.encryptedKey, password = _a.password;
return Promise.resolve({ encryptedKey: encryptedKey, password: password });
}
function decryptKeyIncorrect() {
return Promise.resolve({
privateKey: "INCORRECT",
encryptedBlob: undefined,
encrypterName: undefined,
salt: undefined,
});
}
test("error if passed nothing", function (done) {
testEncrypter()
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Encrypter not defined");
done();
});
});
test("Validates good Encrypter", function (done) {
var goodEncrypter = {
name: "goodEncrypter",
encryptKey: encryptKeyGood,
decryptKey: decryptKeyGood,
};
testEncrypter(goodEncrypter)
.then(function () { return done(); })
.catch(done);
});
test("Invalidates missing name", function (done) {
var goodEncrypter = {
encryptKey: encryptKeyGood,
decryptKey: decryptKeyGood,
};
testEncrypter(goodEncrypter)
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Name not defined");
done();
});
});
test("encryptKey should actually encrypt a key", function (done) {
var badEncrypter = {
name: "badEncrypter",
encryptKey: encryptKeyInvalid,
decryptKey: decryptKeyGood,
};
testEncrypter(badEncrypter)
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Encrypted key not valid");
done();
});
});
test("decryptKey should actually decrypt a key", function (done) {
var badEncrypter = {
name: "badEncrypter",
encryptKey: encryptKeyGood,
decryptKey: decryptKeyInvalid,
};
testEncrypter(badEncrypter)
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Decrypted key not valid");
done();
});
});
test("decryptKey should be the same as the original", function (done) {
var badEncrypter = {
name: "badEncrypter",
encryptKey: encryptKeyGood,
decryptKey: decryptKeyIncorrect,
};
testEncrypter(badEncrypter)
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Decrypted key doesn't match original key");
done();
});
});
});
describe("testKeyStore", function () {
function makeKeyMetadata(encryptedKey) {
return getKeyMetadata(__assign({}, encryptedKey));
}
var storage = {};
var skipStorageChecks = false;
var skipUpdateChecks = false;
var goodKeyStore = {
name: "goodKeyStore",
configure: function () {
return Promise.resolve();
},
storeKeys: function (keys) {
// kill anything already in storage
if (!skipStorageChecks) {
var areStored = keys.filter(function (key) { return storage[key.id]; });
if (areStored.length) {
throw new Error("stored already");
}
}
keys.forEach(function (key) {
storage[key.id] = key;
});
return Promise.resolve(keys.map(function (encryptedKey) { return makeKeyMetadata(encryptedKey); }));
},
updateKeys: function (keys) {
// kill anything already in storage
if (!skipUpdateChecks) {
var areNotStored = keys.filter(function (key) { return !storage[key.id]; });
if (areNotStored.length) {
throw new Error("stored already");
}
}
keys.forEach(function (key) {
storage[key.id] = key;
});
return Promise.resolve(keys.map(function (encryptedKey) { return makeKeyMetadata(encryptedKey); }));
},
loadKey: function (id) {
return Promise.resolve(storage[id]);
},
removeKey: function (id) {
var key = storage[id];
if (!key) {
return Promise.resolve(undefined);
}
delete storage[id];
return Promise.resolve(makeKeyMetadata(key));
},
loadAllKeys: function () {
return Promise.resolve(Object.values(storage));
},
};
var clock;
beforeEach(function () {
clock = sinon.useFakeTimers();
// clear storage
storage = {};
skipStorageChecks = false;
skipUpdateChecks = false;
});
afterEach(function () {
clock.restore();
});
test("error if passed nothing", function (done) {
testKeyStore()
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("KeyStore not defined");
done();
});
});
test("error if bad name", function (done) {
testKeyStore(__assign(__assign({}, goodKeyStore), { name: undefined }))
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Name not defined");
done();
});
});
test("error if bad configure", function (done) {
testKeyStore(__assign(__assign({}, goodKeyStore), { configure: undefined }))
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Invalid function");
expect(err.toString()).toMatch("[KeyStore.configure]");
done();
});
});
test("error if bad storeKeys", function (done) {
testKeyStore(__assign(__assign({}, goodKeyStore), { storeKeys: undefined }))
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Invalid function");
expect(err.toString()).toMatch("[KeyStore.storeKeys]");
done();
});
});
test("error if bad updateKeys", function (done) {
testKeyStore(__assign(__assign({}, goodKeyStore), { updateKeys: undefined }))
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Invalid function");
expect(err.toString()).toMatch("[KeyStore.updateKeys]");
done();
});
});
test("error if bad loadKey", function (done) {
testKeyStore(__assign(__assign({}, goodKeyStore), { loadKey: undefined }))
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Invalid function");
expect(err.toString()).toMatch("[KeyStore.loadKey]");
done();
});
});
test("error if bad removeKey", function (done) {
testKeyStore(__assign(__assign({}, goodKeyStore), { removeKey: undefined }))
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Invalid function");
expect(err.toString()).toMatch("[KeyStore.removeKey]");
done();
});
});
test("error if bad loadAllKeys", function (done) {
testKeyStore(__assign(__assign({}, goodKeyStore), { loadAllKeys: undefined }))
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Invalid function");
expect(err.toString()).toMatch("[KeyStore.loadAllKeys]");
done();
});
});
test("Works with good keystore", function (done) {
testKeyStore(goodKeyStore)
.then(function () {
done();
})
.catch(done);
});
test("Error when doesn't check storeKeys", function (done) {
skipStorageChecks = true;
testKeyStore(goodKeyStore)
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Doesn't error when storing a stored key");
done();
});
});
test("Error when doesn't check updateKeys", function (done) {
skipUpdateChecks = true;
testKeyStore(goodKeyStore)
.then(function () {
done("Succeeded but should have failed");
})
.catch(function (err) {
expect(err.toString()).toMatch("Doesn't error when updating a nonexistent key");
done();
});
});
});
//# sourceMappingURL=PluginTesting.test.js.map