identities.js 3.6 KB

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