| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  | import * as fs from 'fs'; | 
					
						
							|  |  |  | import * as path from 'path'; | 
					
						
							|  |  |  | import * as io from '@actions/io'; | 
					
						
							|  |  |  | import * as exec from '@actions/exec'; | 
					
						
							|  |  |  | import * as util from './util'; | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  | import { ExecOptions } from '@actions/exec/lib/interfaces'; | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const PRIVATE_KEY_FILE = path.join(util.getTempDir(), 'private-key.asc'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const PRIVATE_KEY_FINGERPRINT_REGEX = /\w{40}/; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function importKey(privateKey: string) { | 
					
						
							|  |  |  |   fs.writeFileSync(PRIVATE_KEY_FILE, privateKey, { | 
					
						
							|  |  |  |     encoding: 'utf-8', | 
					
						
							|  |  |  |     flag: 'w' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let output = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const options: ExecOptions = { | 
					
						
							|  |  |  |     silent: true, | 
					
						
							|  |  |  |     listeners: { | 
					
						
							|  |  |  |       stdout: (data: Buffer) => { | 
					
						
							|  |  |  |         output += data.toString(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await exec.exec( | 
					
						
							|  |  |  |     'gpg', | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |     ['--batch', '--import-options', 'import-show', '--import', PRIVATE_KEY_FILE], | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  |     options | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await io.rmRF(PRIVATE_KEY_FILE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const match = output.match(PRIVATE_KEY_FINGERPRINT_REGEX); | 
					
						
							|  |  |  |   return match && match[0]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function deleteKey(keyFingerprint: string) { | 
					
						
							| 
									
										
										
										
											2021-09-22 04:39:53 -04:00
										 |  |  |   await exec.exec('gpg', ['--batch', '--yes', '--delete-secret-and-public-key', keyFingerprint], { | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |     silent: true | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  | } |