Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Query sig
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl committed Feb 15, 2022
1 parent 98f2b56 commit 7532bc4
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ cloud_config/

# Test Files
testFiles/
devKey.js
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const createConnector = require('./src/main.js')
const createConnectorRegistry = require('./src/mainRegistry.js')
const createMoat = require('./src/createMoat.js')

const KwilDB = {createConnector, createMoat}
const KwilDB2 = {createConnectorRegistry}
const KwilDB = {createConnector, createMoat, createConnectorRegistry}

module.exports = {KwilDB,KwilDB2}
module.exports = KwilDB
69 changes: 63 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
"base64url": "^3.0.1",
"buffer": "^6.0.3",
"ethers": "^5.5.4",
"jsrsasign": "^10.5.4",
"jssha": "^3.2.0",
"knex": "^1.0.1",
"pem-jwk": "^2.0.0",
"pg": "^8.7.1",
"react-native-crypto-js": "^1.0.0",
"scrypt-js": "^3.0.1",
Expand Down
8 changes: 3 additions & 5 deletions src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ const create = (_credentials) => {
//Cleaning inputs and giving warnings

//Trying to make this idiot-proof
if (_credentials.protocol == null || _credentials.host == null || _credentials.apiKey == null || _credentials.moat == null) {
throw new Error('Missing a credential. Mandatory are protocol, host, apiKey, and moat')
console.log('Input cleaning for connection is disabled, but I can tell you right now the rest of this shit isnt gonna work')
if (_credentials.protocol == null || _credentials.host == null || _credentials.moat == null) {
throw new Error('Missing a credential. Mandatory are protocol, host, privateKey, and moat')
}

if (_credentials.protocol == 'http' && _credentials.host != 'localhost') {
Expand All @@ -27,8 +26,7 @@ const create = (_credentials) => {
method: 'post',
timeout: 20000,
data: {
moat: _credentials.moat,
apiKey: _credentials.apiKey
moat: _credentials.moat
}
};
return params
Expand Down
25 changes: 25 additions & 0 deletions src/createDataWrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { generateAPIKey } = require("./utils/generateAPIKey")
const { sha384 } = require("./utils/sha384")
const { sign } = require('./utils/sign.js')

function createDataWrite(_data, _store, _secret, _moat, _privateJWK) {
const timestamp = Date.now()
const hash = sha384(_data+timestamp.toString()+_secret)
const queryID = generateAPIKey(64)
const signature = sign({data : _data,
timestamp: timestamp,
hash: hash,
queryID: queryID,
}, _privateJWK)
return {
data : _data,
timestamp: timestamp,
hash: hash,
queryID: queryID,
signature: signature,
store: _store,
moat: _moat
}
}

module.exports = {createDataWrite}
10 changes: 6 additions & 4 deletions src/createMoat.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const axios = require('axios')
const {generateAPIKey} = require('./utils/generateAPIKey.js')
const {encryptKey} = require('./utils/encryptKey.js')
const {generateKeyPair} = require('./utils/generateKeyPair.js')

const createMoat = async (_registry, _moat, _signature, _walletAddr) => {

const apiKey = generateAPIKey()
const keys = await generateKeyPair()
const privateKey = keys.privateKey
const secret = generateAPIKey()
const encryptedKey = await encryptKey(_signature, _walletAddr, apiKey)
const encryptedKey = await encryptKey(_signature, _walletAddr, JSON.stringify(privateKey))
const encryptedSecret = await encryptKey(_signature, _walletAddr, secret)

const params = {
Expand All @@ -15,14 +17,14 @@ const createMoat = async (_registry, _moat, _signature, _walletAddr) => {
timeout: 20000,
data: {
encryptedKey: encryptedKey,
key: apiKey,
publicKey: keys.publicKey.n,
moat: _moat,
address: _walletAddr,
secret: encryptedSecret
}
};
let response = await axios(params)
response.data.apiKey = apiKey
response.data.apiKey = privateKey
response.data.secret = secret
return response.data
}
Expand Down
14 changes: 5 additions & 9 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ const create = require('./create.js')
const Transaction = require('./transactions.js')
const {createWebSocket} = require('./websocket.js')
const table = require('./createTable.js') //Importing like this because I want the function to be called createTable
const { sha384 } = require('./utils/sha384.js')
const { createDataWrite } = require('./createDataWrite.js')
const createConnector = (_credentials, _secret) => {
const secret = _secret.slice()
const privateKey = _credentials.privateKey
const params = create(_credentials)
class KwilDB {

connectionParams = params

query = async (_query, _store = false) => {
let _params = JSON.parse(JSON.stringify(params)) //we must copy the params since we will be writing to them
const timestamp = Date.now()
_params.data.query = _query
_params.data.store = _store
_params.data.timestamp = timestamp
_params.data.hash = sha384(_query+timestamp.toString()+secret)
_params.url = params.url + '/raw' //use .slice to copy
const dataWrite = createDataWrite(_query, _store, secret, _params.data.moat, privateKey)
_params.data = dataWrite
_params.url = params.url + '/raw'
const response = await axios(_params)
return response.data
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/generateAPIKey.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const generateAPIKey = () => {
const generateAPIKey = (_length=32) => {
const validChars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890,.<>/?;:[]{}|+=_-)(*&^%$#@!~';
let salt = '';
for (let i = 0; i < 32; i++) {
for (let i = 0; i < _length; i++) {
const randomElement = Math.floor(Math.random() * validChars.length);
salt = salt + validChars[randomElement];
}
return salt;
};

module.exports = {generateAPIKey}
67 changes: 67 additions & 0 deletions src/utils/generateKeyPair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const rs = require('jsrsasign')
const crypto = require('crypto')
const generateKeyPairNode = require('./generateKeyPairNode')

const getPublicJWKFromPrivateJWK = (_privateJWK) => {
//This function takes a private key and returns a public JWK
let pubJWK = {
kty: _privateJWK.kty,
n: _privateJWK.n,
e: _privateJWK.e,
};
return pubJWK;
};



const generateKeyPair = async () => {

if (typeof crypto.generateKeyPair == "function") {
const keys = await generateKeyPairNode()
return keys
}

async function WrapperFunction() {
if (typeof window === 'object') {
if (typeof window.crypto === 'object') {
try {
let keyPair = await window.crypto.subtle.generateKey(
{
name: 'RSA-PSS',
modulusLength: 4096, //can be 1024, 2048, or 4096
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: 'SHA-256' }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
['sign', 'verify'] //can be any combination of "sign" and "verify"
);
let jwk = await window.crypto.subtle.exportKey('jwk', keyPair.privateKey);
let privateKey = rs.KEYUTIL.getKey(jwk);
const rsaJWK = rs.KEYUTIL.getJWKFromKey(privateKey);
return { pubKey: getPublicJWKFromPrivateJWK(rsaJWK), privateKey: rsaJWK };
}
catch ({ message } ) {
return {
status: 400,
message
};
}
//return keyPair;
}
} //Test for node
else {
console.log('window.crypto not available. Key generation may take a while...');
try {
let keyPair = await rs.KEYUTIL.generateKeypair('RSA', 4096);
const rsaJWK = await rs.KEYUTIL.getJWKFromKey(keyPair.prvKeyObj);
return { pubKey: getPublicJWKFromPrivateJWK(rsaJWK), privateKey: rsaJWK };
} catch (e) {
console.log(e);
}
}
}
const retVal = await WrapperFunction()
return retVal
}

module.exports = {generateKeyPair}
24 changes: 24 additions & 0 deletions src/utils/generateKeyPairNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const crypto = require('crypto')
const pem2jwk = require('pem-jwk').pem2jwk

async function generateKeyPairNode() {
if (typeof crypto.generateKeyPair == "function") {
//running in nodejs
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096, // options
publicExponent: 0x10001,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem'
}
}
)
return {publicKey: pem2jwk(keys.publicKey), privateKey: pem2jwk(keys.privateKey)}
} else {throw new Error('Not in nodejs')}
}

module.exports = generateKeyPairNode
Loading

0 comments on commit 7532bc4

Please sign in to comment.