mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-04-25 18:42:31 +00:00
Merge pull request #4 from nginnever/master
public key derivation from private key stored in go-ipfs config file
This commit is contained in:
commit
1f72aaf7b7
@ -9,5 +9,13 @@ branches:
|
|||||||
before_install:
|
before_install:
|
||||||
- npm i -g npm
|
- npm i -g npm
|
||||||
# Workaround for a permissions issue with Travis virtual machine images
|
# Workaround for a permissions issue with Travis virtual machine images
|
||||||
|
|
||||||
|
addons:
|
||||||
|
firefox: 'latest'
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- export DISPLAY=:99.0
|
||||||
|
- sh -e /etc/init.d/xvfb start
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- npm test
|
- npm test
|
||||||
|
@ -6,7 +6,10 @@ peer-id JavaScript implementation
|
|||||||
|
|
||||||
# Description
|
# Description
|
||||||
|
|
||||||
A IPFS Peer Id is based on a sha256 has of the peer public key, using [multihash](https://github.com/jbenet/multihash)
|
An IPFS Peer Id is based on a sha256 hash of the peer public key, using [multihash](https://github.com/jbenet/multihash)
|
||||||
|
|
||||||
|
The public key is a base64 encoded string of a protobuf containing an RSA DER buffer. This uses a node buffer to pass the base64 encoded public key protobuf to the multihash for ID generation.
|
||||||
|
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
|
29552
deps/forge.bundle.js
vendored
Normal file
29552
deps/forge.bundle.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
57
karma.conf.js
Normal file
57
karma.conf.js
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
module.exports = function (config) {
|
||||||
|
var path = require('path')
|
||||||
|
var node_modules_dir = path.join(__dirname, 'node_modules')
|
||||||
|
var deps = [
|
||||||
|
'deps/forge.bundle.js'
|
||||||
|
]
|
||||||
|
config.set({
|
||||||
|
basePath: '',
|
||||||
|
frameworks: ['mocha'],
|
||||||
|
|
||||||
|
files: [
|
||||||
|
'tests/test.js'
|
||||||
|
],
|
||||||
|
|
||||||
|
preprocessors: {
|
||||||
|
'tests/*': ['webpack']
|
||||||
|
},
|
||||||
|
|
||||||
|
webpack: {
|
||||||
|
resolve: {
|
||||||
|
extensions: ['', '.js', '.json'],
|
||||||
|
alias: { 'node-forge': __dirname + '/deps/forge.bundle.js' }
|
||||||
|
},
|
||||||
|
externals: {
|
||||||
|
fs: '{}'
|
||||||
|
},
|
||||||
|
node: {
|
||||||
|
Buffer: true
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
loaders: [
|
||||||
|
{ test: /\.json$/, loader: 'json' }
|
||||||
|
],
|
||||||
|
noParse: []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
webpackMiddleware: {
|
||||||
|
noInfo: true,
|
||||||
|
stats: {
|
||||||
|
colors: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
reporters: ['spec'],
|
||||||
|
port: 9876,
|
||||||
|
colors: true,
|
||||||
|
logLevel: config.LOG_INFO,
|
||||||
|
autoWatch: false,
|
||||||
|
browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],
|
||||||
|
singleRun: true
|
||||||
|
})
|
||||||
|
|
||||||
|
deps.forEach(function (dep) {
|
||||||
|
var depPath = path.resolve(node_modules_dir, dep)
|
||||||
|
config.webpack.module.noParse.push(depPath)
|
||||||
|
})
|
||||||
|
}
|
27
package.json
27
package.json
@ -4,12 +4,10 @@
|
|||||||
"description": "IPFS Peer Id implementation in Node.js",
|
"description": "IPFS Peer Id implementation in Node.js",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test:node": "node tests/*-test.js",
|
"test:node": "mocha tests/test.js",
|
||||||
"lint": "standard",
|
"lint": "standard",
|
||||||
"test": "npm run test:node",
|
"test": "npm run test:node && npm run test:browser",
|
||||||
"test:browser": "./node_modules/.bin/zuul --browser-version $BROWSER_VERSION --browser-name $BROWSER_NAME -- tests/id-test.js",
|
"test:browser": "karma start karma.conf.js"
|
||||||
"test:browser:q": "BROWSER_VERSION=46 BROWSER_NAME=chrome npm run test:browser",
|
|
||||||
"build": "./node_modules/.bin/browserify -s PeerId -e ./src/index.js | tee dist/peer-id.js | ./node_modules/.bin/uglifyjs -m > dist/peer-id.min.js"
|
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
@ -33,14 +31,25 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/diasdavid/js-peer-id",
|
"homepage": "https://github.com/diasdavid/js-peer-id",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"buffer-loader": "0.0.1",
|
||||||
|
"chai": "^3.5.0",
|
||||||
|
"json-loader": "^0.5.4",
|
||||||
|
"karma": "^0.13.19",
|
||||||
|
"karma-chrome-launcher": "^0.2.2",
|
||||||
|
"karma-cli": "^0.1.2",
|
||||||
|
"karma-firefox-launcher": "^0.1.7",
|
||||||
|
"karma-mocha": "^0.2.1",
|
||||||
|
"karma-spec-reporter": "0.0.24",
|
||||||
|
"karma-webpack": "^1.7.0",
|
||||||
|
"mocha": "^2.4.5",
|
||||||
"pre-commit": "^1.1.1",
|
"pre-commit": "^1.1.1",
|
||||||
"standard": "^5.3.1",
|
"standard": "^5.3.1",
|
||||||
"tape": "^4.2.2",
|
"webpack": "^1.12.13"
|
||||||
"zuul": "^3.7.2"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bs58": "^3.0.0",
|
"bs58": "^3.0.0",
|
||||||
"keypair": "^1.0.0",
|
"multihashing": "^0.2.0",
|
||||||
"multihashing": "^0.2.0"
|
"node-forge": "^0.6.38",
|
||||||
|
"protocol-buffers": "^3.1.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
pb/crypto.proto
Normal file
13
pb/crypto.proto
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
enum KeyType {
|
||||||
|
RSA = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
message PublicKey {
|
||||||
|
required KeyType Type = 1;
|
||||||
|
required bytes Data = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message PrivateKey {
|
||||||
|
required KeyType Type = 1;
|
||||||
|
required bytes Data = 2;
|
||||||
|
}
|
105
src/index.js
105
src/index.js
@ -2,9 +2,16 @@
|
|||||||
* Id is an object representation of a peer Id. a peer Id is a multihash
|
* Id is an object representation of a peer Id. a peer Id is a multihash
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var fs = require('fs')
|
||||||
var multihashing = require('multihashing')
|
var multihashing = require('multihashing')
|
||||||
var base58 = require('bs58')
|
var base58 = require('bs58')
|
||||||
var keypair = require('keypair')
|
var forge = require('node-forge')
|
||||||
|
var protobuf = require('protocol-buffers')
|
||||||
|
|
||||||
|
var isNode = !global.window
|
||||||
|
|
||||||
|
// protobuf read from file
|
||||||
|
var messages = isNode ? protobuf(fs.readFileSync(__dirname + '/../pb/crypto.proto')) : protobuf(require('buffer!./../pb/crypto.proto'))
|
||||||
|
|
||||||
exports = module.exports = Id
|
exports = module.exports = Id
|
||||||
|
|
||||||
@ -25,7 +32,7 @@ function Id (id, privKey, pubKey) {
|
|||||||
|
|
||||||
self.toPrint = function () {
|
self.toPrint = function () {
|
||||||
return {
|
return {
|
||||||
id: self.toHexString(),
|
id: self.toB58String(),
|
||||||
privKey: privKey.toString('hex'),
|
privKey: privKey.toString('hex'),
|
||||||
pubKey: pubKey.toString('hex')
|
pubKey: pubKey.toString('hex')
|
||||||
}
|
}
|
||||||
@ -46,18 +53,71 @@ function Id (id, privKey, pubKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unwrap the private key protobuf
|
||||||
|
function unmarshal (key) {
|
||||||
|
var dpb = messages.PrivateKey.decode(key)
|
||||||
|
return dpb
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a public key protobuf to be base64 string stored in config
|
||||||
|
function marshal (data, type) {
|
||||||
|
var epb
|
||||||
|
if (type === 'Public') {
|
||||||
|
epb = messages.PublicKey.encode({
|
||||||
|
Type: 0,
|
||||||
|
Data: data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'Private') {
|
||||||
|
epb = messages.PrivateKey.encode({
|
||||||
|
Type: 0,
|
||||||
|
Data: data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return epb
|
||||||
|
}
|
||||||
|
|
||||||
|
// this returns a base64 encoded protobuf of the public key
|
||||||
|
function formatKey (key, type) {
|
||||||
|
// create der buffer of public key asn.1 object
|
||||||
|
var der = forge.asn1.toDer(key)
|
||||||
|
|
||||||
|
// create forge buffer of der public key buffer
|
||||||
|
var fDerBuf = forge.util.createBuffer(der.data, 'binary')
|
||||||
|
|
||||||
|
// convert forge buffer to node buffer public key
|
||||||
|
var nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
||||||
|
|
||||||
|
// protobuf the new DER bytes to the PublicKey Data: field
|
||||||
|
var marshalKey = marshal(nDerBuf, type)
|
||||||
|
|
||||||
|
// encode the protobuf public key to base64 string
|
||||||
|
var b64 = marshalKey.toString('base64')
|
||||||
|
return b64
|
||||||
|
}
|
||||||
|
|
||||||
// generation
|
// generation
|
||||||
|
|
||||||
exports.create = function () {
|
exports.create = function () {
|
||||||
var pair = keypair()
|
// generate keys
|
||||||
|
var pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 })
|
||||||
|
|
||||||
var mhId = multihashing(pair.public, 'sha2-256')
|
// return the RSA public/private key to asn1 object
|
||||||
|
var asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
||||||
|
var asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
||||||
|
|
||||||
return new Id(mhId, pair.private, pair.public)
|
// format the keys to protobuf base64 encoded string
|
||||||
|
var protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
|
var protoPrivate64 = formatKey(asnPriv, 'Private')
|
||||||
|
|
||||||
|
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
||||||
|
|
||||||
|
return new Id(mhId, protoPrivate64, protoPublic64)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromHexString = function (str) {
|
exports.createFromHexString = function (str) {
|
||||||
return new Id(new Buffer(str), 'hex')
|
return new Id(new Buffer(str, 'hex'))
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromBytes = function (buf) {
|
exports.createFromBytes = function (buf) {
|
||||||
@ -69,10 +129,35 @@ exports.createFromB58String = function (str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromPubKey = function (pubKey) {
|
exports.createFromPubKey = function (pubKey) {
|
||||||
var mhId = multihashing(pubKey, 'sha2-256')
|
var buf = new Buffer(pubKey, 'base64')
|
||||||
|
var mhId = multihashing(buf, 'sha2-256')
|
||||||
return new Id(mhId, null, pubKey)
|
return new Id(mhId, null, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromPrivKey = function () {
|
exports.createFromPrivKey = function (privKey) {
|
||||||
// TODO(daviddias) derive PubKey from priv
|
// create a buffer from the base64 encoded string
|
||||||
|
var buf = new Buffer(privKey, 'base64')
|
||||||
|
|
||||||
|
// get the private key data from the protobuf
|
||||||
|
var mpk = unmarshal(buf)
|
||||||
|
|
||||||
|
// create a forge buffer
|
||||||
|
var fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
||||||
|
|
||||||
|
// create an asn1 object from the private key bytes saved in the protobuf Data: field
|
||||||
|
var asnPriv = forge.asn1.fromDer(fbuf)
|
||||||
|
|
||||||
|
// get the RSA privatekey data from the asn1 object
|
||||||
|
var privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
|
||||||
|
|
||||||
|
// set the RSA public key to the modulus and exponent of the private key
|
||||||
|
var publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
|
||||||
|
|
||||||
|
// return the RSA public key to asn1 object
|
||||||
|
var asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
||||||
|
|
||||||
|
// format the public key
|
||||||
|
var protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
|
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
||||||
|
return new Id(mhId, privKey, protoPublic64)
|
||||||
}
|
}
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
var test = require('tape')
|
|
||||||
var PeerId = require('../src')
|
|
||||||
|
|
||||||
var testId = {
|
|
||||||
id: '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d',
|
|
||||||
privKey: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAgPEiGHOwFEUdo95/DaALH69umbFI4xD3Jmla0hiHbkcW535arBfFd8nJ\ns5VPt49sgdSgn1ZmiqmHLgMwMz6mKplu4GsmWj5mjdyxiNl5z6R2rF+ZziuiwRTeHVX/8zR8\nM7Cbh0QXmzpoq6LcNOFHbg495zsbmT9QAtjVsS1KyF5324mxbTZtjaD6hxJkAL8aVi0ikvhA\nL4HuZ1m3brjSSZ0+epFCFL7UIoJlFfOvap2sAyxdOrSvY2PXKTE00s51YTin5+CrvofRLCJP\nROls1oFkhXIDGfuTGTxMxe3hSUlNj0LjQi6RXPGat/5XH0nCuFTODmyhrnnnx51OdgT9vwID\nAQABAoIBAQAIAnKSwET0zWJM9po/12w5eKVPKMMVT816dlrs6Bcpk4LpuGCbhhJ/IWrFHAZK\nqb8cxX+AxlYyUNuT0SDiXgbmaIeJqz5DptKqB0aD8LZvXpD8nieote8zPT+a5Oe0TNNWRqcy\nnNk2jEdKOiChrEjKnlncDkDloRgwRRXpHp4hmh3XrZwygAekxC+LFhO5YS4fuc5tQAzGyl/O\nGKnEmOtRqz4bYQRTrrfhwtAWdMOC90AEtoIPapLnJPBUujHNn7KLktdrmlPSqxFilIIe3jJH\nH0oG9Nr9ueSNat54NQZr2BBrXliFqXu/SomiAfN0jmouB+8lzNYSoUK25JmbJQExAoGBANLE\nLqJS1DpLa3Mg/GMCeCbjWG6qwpLjJttFAG2F4Yst3el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g495zsbmT9QAtjVsS1KyF5324mxbTZtjaD6hxJkAL8aVi0ikvhAL4Hu\nZ1m3brjSSZ0+epFCFL7UIoJlFfOvap2sAyxdOrSvY2PXKTE00s51YTin5+CrvofRLCJPROls\n1oFkhXIDGfuTGTxMxe3hSUlNj0LjQi6RXPGat/5XH0nCuFTODmyhrnnnx51OdgT9vwIDAQAB\n-----END RSA PUBLIC KEY-----\n'
|
|
||||||
}
|
|
||||||
|
|
||||||
var testIdHex = '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d'
|
|
||||||
|
|
||||||
var testIdBytes = new Buffer('1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d', 'hex')
|
|
||||||
|
|
||||||
var testIdB58String = 'QmPm2sunRFpswBAByqunK5Yk8PLj7mxL5HpCS4Qg6p7LdS'
|
|
||||||
|
|
||||||
test('create a new Id', function (t) {
|
|
||||||
var id = PeerId.create()
|
|
||||||
t.ok(id)
|
|
||||||
t.end()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('recreate an Id from Hex string', function (t) {
|
|
||||||
var id = PeerId.createFromHexString(testIdHex)
|
|
||||||
t.ok(id)
|
|
||||||
t.end()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Recreate an Id from a Buffer', function (t) {
|
|
||||||
var id = PeerId.createFromBytes(testIdBytes)
|
|
||||||
t.ok(id)
|
|
||||||
t.end()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Recreate an B58 String', function (t) {
|
|
||||||
var id = PeerId.createFromB58String(testIdB58String)
|
|
||||||
t.ok(id)
|
|
||||||
t.end()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Recreate from a Public Key', function (t) {
|
|
||||||
var id = PeerId.createFromPubKey(testId.pubKey)
|
|
||||||
t.ok(id)
|
|
||||||
t.end()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Recreate from a Private Key', function (t) {
|
|
||||||
// TODO
|
|
||||||
t.end()
|
|
||||||
})
|
|
51
tests/test.js
Normal file
51
tests/test.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const PeerId = require('../src')
|
||||||
|
|
||||||
|
const testId = {
|
||||||
|
id: '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d',
|
||||||
|
privKey: 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ1bovzGoCwx3iGIatQKFOxyVCyO1PsJYT7JO+kZbhBWRRE+L7l+ppPER9bdLFxs1t5CrKc078h+wuUr05S1P1JjXk68pk3+kQKBgQDeK8AR11373Mzib6uzpjGzgNRMzdYNuExWjxyxAzz53NAR7zrPHvXvfIqjDScLJ4NcRO2TddhXAfZoOPVH5k4PJHKLBPKuXZpWlookCAyENY7+Pd55S8r+a+MusrMagYNljb5WbVTgN8cgdpim9lbbIFlpN6SZaV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l+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE='
|
||||||
|
}
|
||||||
|
|
||||||
|
const testIdHex = '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d'
|
||||||
|
|
||||||
|
const testIdBytes = new Buffer('1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d', 'hex')
|
||||||
|
|
||||||
|
const testIdB58String = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
|
||||||
|
|
||||||
|
describe('id', function (done) {
|
||||||
|
this.timeout(30000)
|
||||||
|
it('create a new id', done => {
|
||||||
|
var id = PeerId.create()
|
||||||
|
expect(id.toB58String().length).to.equal(46)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
it('recreate an Id from Hex string', done => {
|
||||||
|
var id = PeerId.createFromHexString(testIdHex)
|
||||||
|
expect(testIdBytes).to.deep.equal(id.id)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
it('Recreate an Id from a Buffer', done => {
|
||||||
|
var id = PeerId.createFromBytes(testIdBytes)
|
||||||
|
expect(testId.id).to.equal(id.toHexString())
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
it('Recreate a B58 String', done => {
|
||||||
|
var id = PeerId.createFromB58String(testIdB58String)
|
||||||
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
it('Recreate from a Public Key', done => {
|
||||||
|
var id = PeerId.createFromPubKey(testId.pubKey)
|
||||||
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
it('Recreate from a Private Key', done => {
|
||||||
|
var id = PeerId.createFromPrivKey(testId.privKey)
|
||||||
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user