identities.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/storage-runtime-api')
  24. describe('Identities', () => {
  25. let api
  26. before(async () => {
  27. api = await RuntimeApi.create({ canPromptForPassphrase: true })
  28. })
  29. it('imports keys', async () => {
  30. // Unlocked keys can be imported without asking for a passphrase
  31. await api.identities.loadUnlock('test/data/edwards_unlocked.json')
  32. // Edwards and schnorr keys should unlock
  33. const passphrase_stub = sinon.stub(api.identities, 'askForPassphrase').callsFake((_) => 'asdf')
  34. await api.identities.loadUnlock('test/data/edwards.json')
  35. await api.identities.loadUnlock('test/data/schnorr.json')
  36. passphrase_stub.restore()
  37. // Except if the wrong passphrase is given
  38. const passphrase_stub_bad = sinon.stub(api.identities, 'askForPassphrase').callsFake((_) => 'bad')
  39. expect(async () => {
  40. await api.identities.loadUnlock('test/data/edwards.json')
  41. }).to.throw
  42. passphrase_stub_bad.restore()
  43. })
  44. it('knows about membership', async () => {
  45. const key = await api.identities.loadUnlock('test/data/edwards_unlocked.json')
  46. const addr = key.address
  47. // Without seeding the runtime with data, we can only verify that the API
  48. // reacts well in the absence of membership
  49. expect(await api.identities.isMember(addr)).to.be.false
  50. const member_id = await api.identities.firstMemberIdOf(addr)
  51. expect(member_id).to.be.undefined
  52. })
  53. it('exports keys', async () => {
  54. const key = await api.identities.loadUnlock('test/data/edwards_unlocked.json')
  55. const passphrase_stub = sinon.stub(api.identities, 'askForPassphrase').callsFake((_) => 'asdf')
  56. const exported = await api.identities.exportKeyPair(key.address)
  57. passphrase_stub.restore()
  58. expect(exported).to.have.property('address')
  59. expect(exported.address).to.equal(key.address)
  60. expect(exported).to.have.property('encoding')
  61. expect(exported.encoding).to.have.property('version', '2')
  62. expect(exported.encoding).to.have.property('content')
  63. expect(exported.encoding.content).to.include('pkcs8')
  64. expect(exported.encoding.content).to.include('ed25519')
  65. expect(exported.encoding).to.have.property('type')
  66. expect(exported.encoding.type).to.include('salsa20')
  67. })
  68. it('writes key export files', async () => {
  69. const prefix = temp.mkdirSync('joystream-runtime-api-test')
  70. const key = await api.identities.loadUnlock('test/data/edwards_unlocked.json')
  71. const passphrase_stub = sinon.stub(api.identities, 'askForPassphrase').callsFake((_) => 'asdf')
  72. const filename = await api.identities.writeKeyPairExport(key.address, prefix)
  73. passphrase_stub.restore()
  74. const fs = require('fs')
  75. const stat = fs.statSync(filename)
  76. expect(stat.isFile()).to.be.true
  77. })
  78. })