Last active
February 20, 2019 14:42
-
-
Save andyshora/2a28bdcc59bdb169d36422fde229a844 to your computer and use it in GitHub Desktop.
Example managing keys, converting array buffers to strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as log from 'loglevel'; | |
import { utils } from '@qadre/sdk-client'; | |
import b64 from 'base64-js'; | |
import moment from 'moment'; | |
import { stringToBuffer } from './utils'; | |
// Services | |
import { Storage } from './ConfigService'; | |
const { | |
addressFromPublicKey, | |
bufferToHex, | |
hexToBuffer, | |
hexToBase64, | |
keyPair, | |
keyPairFromSeed, | |
sign | |
} = utils; | |
const b64EncodeStr = str => hexToBase64(str); | |
/** | |
* This service manages authentication | |
* ...but currently it's just a placeholder, todo | |
*/ | |
class AuthService { | |
constructor() { | |
this._storage = Storage; | |
this._networkId = null; | |
} | |
init() { | |
if (this.publicKey && this.privateKey) { | |
log.info('keys found'); | |
} else { | |
this.generateNewKeys(); | |
} | |
return Promise.resolve({ | |
keys: { | |
publicKey: this.publicKey, | |
privateKey: this.privateKey, | |
address: this.address | |
} | |
}); | |
} | |
generateKeysAndAddressFromSeed(encodedSigningKey) { | |
const signingKey = hexToBuffer(encodedSigningKey); | |
const { publicKey, privateKey } = keyPairFromSeed(signingKey); | |
const address = addressFromPublicKey(hexToBuffer(publicKey)); | |
this.address = address; | |
this.publicKey = bufferToHex(publicKey); | |
this.privateKey = bufferToHex(privateKey); | |
return { | |
address, | |
publicKey: bufferToHex(publicKey), | |
privateKey: bufferToHex(privateKey) | |
}; | |
} | |
generateNewKeys() { | |
const keys = keyPair(); | |
const { | |
publicKey, | |
privateKey | |
} = keys; | |
const address = addressFromPublicKey(publicKey); | |
// write to storage | |
this.address = address; | |
this.publicKey = bufferToHex(publicKey); | |
this.privateKey = bufferToHex(privateKey); | |
return { | |
address, | |
publicKey: bufferToHex(publicKey), | |
privateKey: bufferToHex(privateKey) | |
}; | |
} | |
get address() { | |
return this._storage.readSetting('ADDRESS'); | |
} | |
get publicKey() { | |
return this._storage.readSetting('PUBLIC_KEY'); | |
} | |
get privateKey() { | |
return this._storage.readSetting('SECRET_KEY'); | |
} | |
set publicKey(str) { | |
this._storage.writeSetting('PUBLIC_KEY', str); | |
} | |
set privateKey(str) { | |
this._storage.writeSetting('SECRET_KEY', str); | |
} | |
set address(str) { | |
this._storage.writeSetting('ADDRESS', str); | |
} | |
set networkId(networkId) { | |
this._networkId = networkId; | |
} | |
} | |
export const authService = new AuthService(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash'; | |
import { createRequestPath } from './api'; | |
/** | |
* Convert an Uint8Array into a string. | |
* | |
* @returns {String} | |
*/ | |
const bufferToString = uint8array => new TextDecoder('utf-8').decode(uint8array); | |
/** | |
* Convert a string into a Uint8Array. | |
* | |
* @returns {Uint8Array} | |
*/ | |
const stringToBuffer = myString => new TextEncoder('utf-8').encode(myString); | |
export { | |
bufferToString, | |
stringToBuffer | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment