Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function makeAssociationToken(appPrivateKey: string, identityKey: string): string {
const appPublicKey = ECPair.fromPrivateKey(Buffer.from(appPrivateKey, 'hex'), { compressed: true }).publicKey.toString('hex');
const identityPublicKey = ECPair.fromPrivateKey(Buffer.from(identityKey, 'hex'), { compressed: true }).publicKey.toString('hex');
const FOUR_MONTH_SECONDS = 60 * 60 * 24 * 31 * 4; // apparently a standard
const salt = randomBytes(16).toString('hex');
const associationTokenClaim = {
childToAssociate: appPublicKey,
iss: identityPublicKey,
exp: FOUR_MONTH_SECONDS + (new Date().getTime() / 1000),
salt
};
const associationToken = new TokenSigner('ES256K', identityKey).sign(associationTokenClaim);
return associationToken;
}
constructor(node: BIP32Interface) {
if(node instanceof WrappedNode)
this._node = node.node;
else
this._node = node;
if(this._node.privateKey)
// @ts-ignore
this._keyPair = ECPair.fromPrivateKey(this._node.privateKey);
else if(this._node.publicKey)
// @ts-ignore
this._keyPair = ECPair.fromPublicKey(this._node.publicKey);
else
this._keyPair = null;
}
function getNodePrivateKey(node: BIP32Interface): string {
return ecPairToHexString(ECPair.fromPrivateKey(node.privateKey))
}
export function hexStringToECPair(skHex: string): ECPair.ECPairInterface {
const ecPairOptions = {
network: config.network.layer1,
compressed: true
}
if (skHex.length === 66) {
if (skHex.slice(64) !== '01') {
throw new Error('Improperly formatted private-key hex string. 66-length hex usually '
+ 'indicates compressed key, but last byte must be == 1')
}
return ECPair.fromPrivateKey(Buffer.from(skHex.slice(0, 64), 'hex'), ecPairOptions)
} else if (skHex.length === 64) {
ecPairOptions.compressed = false
return ECPair.fromPrivateKey(Buffer.from(skHex, 'hex'), ecPairOptions)
} else {
throw new Error('Improperly formatted private-key hex string: length should be 64 or 66.')
}
}
export function getPublicKeyFromPrivate(privateKey: string) {
const keyPair = ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'))
return keyPair.publicKey.toString('hex')
}
export async function getBucketUrl(gaiaHubUrl: string, appPrivateKey: string): Promise {
const challengeSigner = ECPair.fromPrivateKey(Buffer.from(appPrivateKey, 'hex'))
const response = await fetchPrivate(`${gaiaHubUrl}/hub_info`)
const responseText = await response.text()
const responseJSON = JSON.parse(responseText)
const readURL = responseJSON.read_url_prefix
const address = ecPairToAddress(challengeSigner)
const bucketUrl = `${readURL}${address}/`
return bucketUrl
}