-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Agoric/rs-improve-height-api
perf: enhance response time for height API
- Loading branch information
Showing
10 changed files
with
351 additions
and
226 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import crypto from 'crypto'; | ||
import querystring from 'querystring'; | ||
import { getCredentials } from './getGCPCredentials.js'; | ||
|
||
const credentials = getCredentials(); | ||
|
||
const createJWT = (scopes) => { | ||
const now = Math.floor(Date.now() / 1000); | ||
|
||
const payload = { | ||
iss: credentials.client_email, | ||
scope: scopes.join(' '), | ||
aud: 'https://oauth2.googleapis.com/token', | ||
exp: now + 3600, | ||
iat: now, | ||
}; | ||
|
||
const header = JSON.stringify({ | ||
alg: 'RS256', | ||
typ: 'JWT', | ||
}); | ||
|
||
const base64Header = Buffer.from(header).toString('base64url'); | ||
const base64Payload = Buffer.from(JSON.stringify(payload)).toString( | ||
'base64url' | ||
); | ||
|
||
const unsignedJwt = `${base64Header}.${base64Payload}`; | ||
const signer = crypto.createSign('RSA-SHA256'); | ||
|
||
signer.update(unsignedJwt); | ||
const signature = signer.sign(credentials.private_key, 'base64'); | ||
|
||
const base64UrlSignature = signature | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=+$/, ''); | ||
|
||
return `${unsignedJwt}.${base64UrlSignature}`; | ||
}; | ||
|
||
export const getAccessToken = async (scopes) => { | ||
try { | ||
const signedJwt = createJWT(scopes); | ||
|
||
const data = querystring.stringify({ | ||
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', | ||
assertion: signedJwt, | ||
}); | ||
|
||
const response = await fetch('https://oauth2.googleapis.com/token', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
body: data, | ||
}); | ||
|
||
const responseBody = await response.json(); | ||
|
||
if (response.ok && responseBody.access_token) { | ||
return responseBody.access_token; | ||
} else { | ||
throw new Error('Failed to get access token'); | ||
} | ||
} catch (error) { | ||
console.error('Error getting access token:', error); | ||
throw error; | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @ts-check | ||
import { fs } from 'zx'; | ||
|
||
let credentials = null; | ||
|
||
export const getCredentials = () => { | ||
if (credentials) { | ||
return credentials; | ||
} | ||
|
||
const credentialsPath = process.env.GOOGLE_APPLICATION_CREDENTIALS; | ||
|
||
if (!credentialsPath) { | ||
throw new Error( | ||
'GOOGLE_APPLICATION_CREDENTIALS environment variable is not set.' | ||
); | ||
} | ||
|
||
try { | ||
const credentialsData = fs.readFileSync(credentialsPath, 'utf8'); | ||
credentials = JSON.parse(credentialsData); | ||
} catch (error) { | ||
throw new Error( | ||
'Failed to read or parse credentials file: ' + error.message | ||
); | ||
} | ||
|
||
return credentials; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const logError = (error, verbose = false) => { | ||
if (error.response) { | ||
console.error(`🚨 Error: ${error.message}`); | ||
console.error(` - Status: ${error.response.status}`); | ||
console.error(` - Stack trace: ${error.stack}`); | ||
|
||
if (verbose) { | ||
console.error( | ||
` - Data: ${JSON.stringify(error.response.data, null, 2)}` | ||
); | ||
console.error( | ||
` - Headers: ${JSON.stringify(error.response.headers, null, 2)}` | ||
); | ||
} | ||
} else if (error.request) { | ||
console.error(`📡 No response received for the request:`); | ||
console.error( | ||
` - Request details: ${JSON.stringify(error.request, null, 2)}` | ||
); | ||
} else { | ||
console.error(`💥 General Error: ${error.message}`); | ||
console.error(` - Stack trace: ${error.stack}`); | ||
} | ||
}; |
This file contains 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
This file contains 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
Oops, something went wrong.