Skip to content

Commit

Permalink
Merge ac2327a into 4eb3780
Browse files Browse the repository at this point in the history
  • Loading branch information
yusefnapora committed Jan 13, 2017
2 parents 4eb3780 + ac2327a commit f899904
Show file tree
Hide file tree
Showing 22 changed files with 61 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/peer/identity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const thenifyAll = require('thenify-all')
const path = require('path')
const fs = thenifyAll(require('fs'), {}, ['readFile'])
const fs = thenifyAll(require('fs'), {}, ['readFile', 'writeFile'])
const PeerId = thenifyAll(require('peer-id'), {}, ['createFromPrivKey', 'create'])
const PeerInfo = require('peer-info')
const Crypto = require('libp2p-crypto')
const Crypto = thenifyAll(require('libp2p-crypto'), {}, ['unmarshalPrivateKey'])
const Multiaddr = require('multiaddr')
const b58 = require('bs58')
const { b58MultihashForBuffer } = require('../common/util')

const NODE_KEY_TYPE = 'RSA'
const NODE_KEY_BITS = 2048
Expand Down Expand Up @@ -129,7 +128,20 @@ function publisherKeyFromB58String (key58: string): PublicSigningKey { // eslint
}

function publisherKeyToB58String (key: PublicSigningKey): string { // eslint-disable-line no-undef
return b58MultihashForBuffer(key.bytes)
return b58.encode(key.bytes)
}

function loadPublisherId (filename: string): Promise<PublisherId> {
return fs.readFile(filename)
.then(bytes => Crypto.unmarshalPrivateKey(bytes))
.then(privateKey => ({
id58: b58.encode(privateKey.public.bytes),
privateKey
}))
}

function savePublisherId (publisherId: PublisherId, filename: string): Promise<void> {
return fs.writeFile(filename, publisherId.privateKey.bytes)
}

function signBuffer (
Expand Down Expand Up @@ -164,6 +176,8 @@ module.exports = {
loadOrGenerateIdentity,
inflateMultiaddr,
generatePublisherId,
loadPublisherId,
savePublisherId,
publisherKeyFromB58String,
publisherKeyToB58String,
signBuffer,
Expand Down
19 changes: 19 additions & 0 deletions test/resources/generate_test_publisher_ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('path')
const mkdirp = require('mkdirp')
const {generatePublisherId, savePublisherId} = require('../../lib/peer/identity')

const NUM_IDS = 20
const outputDir = path.join(__dirname, 'publisher_ids')
mkdirp.sync(outputDir)

const promises = []
for (let i = 0; i < NUM_IDS; i++) {
promises.push(
generatePublisherId().then(id =>
savePublisherId(id, path.join(outputDir, `${id.id58}.id`)))
)
}

Promise.all(promises).then(ids => {
console.log(`wrote ${ids.length} ids to ${outputDir}`)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`���\t�N���ia�ɥ6u&"��ӿ����E*�/�}������*#�W�؊���(��բ4��/�}������*#�W�؊���(��բ4�
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`�4̌6�F���E���Q��K���X+_��O�'m�Z4I�����7}:�9�%��'��Y^�>�>TZ4I�����7}:�9�%��'��Y^�>�>T
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`�-N����xV�څ��]�N�lSP]u��]�F'xO40r��e��v�Ìw*���A7;�YO40r��e��v�Ìw*���A7;�Y
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`�`�Q8➭��)�^=ˢ��Yh�d��[ф`��O=E���JL�4��­�,^3P��K��겑�.�O=E���JL�4��­�,^3P��K��겑�.�
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`������4�s����G����
�3<�Z��rٌ �aJ�H�ɦge���qkZ�y��6��F>R��aJ�H�ɦge���qkZ�y��6��F>R��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`�Ibe� �(.�3md>=�u�?�7H��5��Î"������@^�7�$YB `�ъA�.z~ B�"������@^�7�$YB `�ъA�.z~ B
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`�
�E��ZΦ,t!M�j��h��B#���ג�x�f�Z`��]�� ��� ~궱�ML=���x�f�Z`��]�� ��� ~궱�ML=�
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`��f��|��S5�rB��� ֮I�s�Qs4�Gm��4��)?�[���shN8�CuB����a��˰�4��)?�[���shN8�CuB����a��˰
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`Iu^����L���Ly�g�9� O�܋ݹ'�ž;��vv�-VI�Z��8=�g��T��읟q�ž;��vv�-VI�Z��8=�g��T��읟q
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
` �(F�Ҋ�Ď� P��m�Ft����aa�m+@���Wt��#�7�ͽ��e�`�A�u��Y����q��Wt��#�7�ͽ��e�`�A�u��Y����q
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`/��r�l� �4
!���fU7���@�@��>�EϏ��s�W�Ȝ����=��u�m��n�'��Ϗ��s�W�Ȝ����=��u�m��n�'��
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`l[Ǵ��r;P��۱ ��U���~�(z�#��Ѕ����;��-��
�4.��(��KNЮ����Ѕ����;��-��
�4.��(��KN����
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`%A�x�.��E�d�Aܴ�L���F��
�O'�{�2 �1��z|��J/��&.D�H���F6���{�2 �1��z|��J/��&.D�H���F6��
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`�q� ��:���ŢS�k�܍����^����7��P}B <���ٜ�K����l"��1A�
�\]�P}B <���ٜ�K����l"��1A�
�\]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`zF������mWS�0�$�F���� �>)�A�>)6��/@D����U���3&WZa�OP6�>)6��/@D����U���3&WZa�OP6
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`v�`���o@�1�@�C5"�]�{ؤ�|̃�#���Dh���8��ʨ•�Y���}+6�_��#���Dh���8��ʨ•�Y���}+6�_�
Binary file not shown.

0 comments on commit f899904

Please sign in to comment.