-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afbc2ba
commit bc5dfc5
Showing
6 changed files
with
124 additions
and
3 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import Arweave from 'arweave'; | ||
import crypto from 'crypto'; | ||
import type { PrivateState, Repo } from '../types'; | ||
import { getActivePublicKey, getAddress } from './arweaveHelper'; | ||
import { log } from './common'; | ||
import { | ||
decryptAesKeyWithPrivateKey, | ||
decryptFileWithAesGcm, | ||
} from './privateRepo'; | ||
|
||
export async function deriveAddress(publicKey: string) { | ||
const arweave = Arweave.init({ | ||
host: 'ar-io.net', | ||
port: 443, | ||
protocol: 'https', | ||
}); | ||
|
||
const pubKeyBuf = arweave.utils.b64UrlToBuffer(publicKey); | ||
const sha512DigestBuf = await crypto.subtle.digest('SHA-512', pubKeyBuf); | ||
|
||
return arweave.utils.bufferTob64Url(new Uint8Array(sha512DigestBuf)); | ||
} | ||
|
||
async function decryptPAT( | ||
encryptedPATString: string, | ||
privateStateTxId: string | ||
): Promise<string> { | ||
const arweave = new Arweave({ | ||
host: 'ar-io.net', | ||
port: 443, | ||
protocol: 'https', | ||
}); | ||
|
||
const encryptedPAT = arweave.utils.b64UrlToBuffer(encryptedPATString); | ||
const response = await fetch(`https://arweave.net/${privateStateTxId}`); | ||
const privateState = (await response.json()) as PrivateState; | ||
const ivArrBuff = arweave.utils.b64UrlToBuffer(privateState.iv); | ||
|
||
//public key -> hash -> get the aes key from object | ||
const pubKey = await getActivePublicKey(); | ||
const address = await deriveAddress(pubKey); | ||
|
||
const encAesKeyStr = privateState.encKeys[address]; | ||
const encAesKeyBuf = arweave.utils.b64UrlToBuffer(encAesKeyStr!); | ||
|
||
const aesKey = (await decryptAesKeyWithPrivateKey( | ||
encAesKeyBuf | ||
)) as unknown as ArrayBuffer; | ||
const accessToken = await decryptFileWithAesGcm( | ||
encryptedPAT, | ||
aesKey, | ||
ivArrBuff | ||
); | ||
|
||
return new TextDecoder().decode(accessToken); | ||
} | ||
|
||
export async function triggerGithubSync(repo: Repo) { | ||
try { | ||
if (!repo) return; | ||
|
||
const githubSync = repo.githubSync; | ||
if (!githubSync || !githubSync?.enabled) return; | ||
|
||
const connectedAddress = await getAddress(); | ||
const isAllowed = | ||
githubSync.allowed.findIndex( | ||
(address) => address === connectedAddress | ||
) > -1; | ||
|
||
if ( | ||
!isAllowed || | ||
!githubSync.repository || | ||
!githubSync.workflowId || | ||
!githubSync.branch | ||
) | ||
return; | ||
|
||
const accessToken = await decryptPAT( | ||
githubSync.accessToken, | ||
githubSync.privateStateTxId | ||
); | ||
|
||
if (!accessToken) return; | ||
|
||
const response = await fetch( | ||
`https://api.github.com/repos/${githubSync?.repository}/actions/workflows/${githubSync?.workflowId}/dispatches`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/vnd.github+json', | ||
'X-GitHub-Api-Version': '2022-11-28', | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
body: JSON.stringify({ ref: githubSync?.branch, inputs: {} }), | ||
} | ||
); | ||
|
||
if (response.status === 204) { | ||
log('Successfully synced to GitHub Successfully'); | ||
} | ||
} catch (err) { | ||
// | ||
} | ||
} |
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
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