identities.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. const mocha = require('mocha');
  20. const expect = require('chai').expect;
  21. const sinon = require('sinon');
  22. const temp = require('temp').track();
  23. const { RuntimeApi } = require('@joystream/runtime-api');
  24. describe('Identities', () => {
  25. var api;
  26. before(async () => {
  27. api = await RuntimeApi.create({ canPromptForPassphrase: true });
  28. });
  29. it('creates role keys', async () => {
  30. const key = await api.identities.createRoleKey('foo', 'bar');
  31. expect(key).to.have.property('type', 'ed25519');
  32. expect(key.meta.name).to.include('foo');
  33. expect(key.meta.name).to.include('bar');
  34. });
  35. it('imports keys', async () => {
  36. // Unlocked keys can be imported without asking for a passphrase
  37. await api.identities.loadUnlock('test/data/edwards_unlocked.json');
  38. // Edwards and schnorr keys should unlock
  39. const passphrase_stub = sinon.stub(api.identities, 'askForPassphrase').callsFake(_ => 'asdf');
  40. await api.identities.loadUnlock('test/data/edwards.json');
  41. await api.identities.loadUnlock('test/data/schnorr.json');
  42. passphrase_stub.restore();
  43. // Except if the wrong passphrase is given
  44. const passphrase_stub_bad = sinon.stub(api.identities, 'askForPassphrase').callsFake(_ => 'bad');
  45. expect(async () => {
  46. await api.identities.loadUnlock('test/data/edwards.json');
  47. }).to.throw;
  48. passphrase_stub_bad.restore();
  49. });
  50. it('knows about membership', async () => {
  51. const key = await api.identities.loadUnlock('test/data/edwards_unlocked.json');
  52. const addr = key.address;
  53. // Without seeding the runtime with data, we can only verify that the API
  54. // reacts well in the absence of membership
  55. expect(await api.identities.isMember(addr)).to.be.false;
  56. const member_id = await api.identities.firstMemberIdOf(addr);
  57. expect(member_id).to.be.undefined;
  58. });
  59. it('exports keys', async () => {
  60. const key = await api.identities.loadUnlock('test/data/edwards_unlocked.json');
  61. const passphrase_stub = sinon.stub(api.identities, 'askForPassphrase').callsFake(_ => 'asdf');
  62. const exported = await api.identities.exportKeyPair(key.address);
  63. passphrase_stub.restore();
  64. expect(exported).to.have.property('address');
  65. expect(exported.address).to.equal(key.address);
  66. expect(exported).to.have.property('encoding');
  67. expect(exported.encoding).to.have.property('version', '2');
  68. expect(exported.encoding).to.have.property('content');
  69. expect(exported.encoding.content).to.include('pkcs8');
  70. expect(exported.encoding.content).to.include('ed25519');
  71. expect(exported.encoding).to.have.property('type');
  72. expect(exported.encoding.type).to.include('salsa20');
  73. });
  74. it('writes key export files', async () => {
  75. const prefix = temp.mkdirSync('joystream-runtime-api-test');
  76. const key = await api.identities.loadUnlock('test/data/edwards_unlocked.json');
  77. const passphrase_stub = sinon.stub(api.identities, 'askForPassphrase').callsFake(_ => 'asdf');
  78. const filename = await api.identities.writeKeyPairExport(key.address, prefix);
  79. passphrase_stub.restore();
  80. const fs = require('fs');
  81. const stat = fs.statSync(filename);
  82. expect(stat.isFile()).to.be.true;
  83. });
  84. });