| 
									
										
										
										
											2019-09-26 13:59:33 +02:00
										 |  |  | import * as plugins from './bunq.plugins'; | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  | import * as paths from './bunq.paths'; | 
					
						
							|  |  |  | import { MonetaryAccount } from './bunq.classes.monetaryaccount'; | 
					
						
							| 
									
										
										
										
											2019-09-26 13:59:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface IBunqConstructorOptions { | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  |   deviceName: string; | 
					
						
							|  |  |  |   apiKey: string; | 
					
						
							|  |  |  |   environment: 'SANDBOX' | 'PRODUCTION'; | 
					
						
							| 
									
										
										
										
											2019-09-26 13:59:33 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * the main bunq account | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class BunqAccount { | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  |   public options: IBunqConstructorOptions; | 
					
						
							| 
									
										
										
										
											2019-09-26 13:59:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  |   public bunqJSClient: plugins.bunqCommunityClient.default; | 
					
						
							|  |  |  |   public encryptionKey: string; | 
					
						
							|  |  |  |   public permittedIps = []; // bunq will use the current ip if omitted
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * user id is needed for doing stuff like listing accounts; | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   public userId: number; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(optionsArg: IBunqConstructorOptions) { | 
					
						
							|  |  |  |     this.options = optionsArg; | 
					
						
							| 
									
										
										
										
											2019-09-26 13:59:33 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  |   public async init() { | 
					
						
							|  |  |  |     this.encryptionKey = plugins.smartcrypto.nodeForge.util.bytesToHex( | 
					
						
							|  |  |  |       plugins.smartcrypto.nodeForge.random.getBytesSync(16) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // lets setup bunq client
 | 
					
						
							|  |  |  |     await plugins.smartfile.fs.ensureDir(paths.nogitDir); | 
					
						
							| 
									
										
										
										
											2019-10-03 14:04:15 +02:00
										 |  |  |     await plugins.smartfile.fs.ensureFile(paths.bunqJsonProductionFile, '{}'); | 
					
						
							|  |  |  |     await plugins.smartfile.fs.ensureFile(paths.bunqJsonSandboxFile, '{}'); | 
					
						
							|  |  |  |     let apiKey: string; | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  |     if (this.options.environment === 'SANDBOX') { | 
					
						
							|  |  |  |       this.bunqJSClient = new plugins.bunqCommunityClient.default(plugins.JSONFileStore(paths.bunqJsonSandboxFile)); | 
					
						
							|  |  |  |       apiKey = await this.bunqJSClient.api.sandboxUser.post(); | 
					
						
							|  |  |  |       console.log(apiKey); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       this.bunqJSClient = new plugins.bunqCommunityClient.default(plugins.JSONFileStore(paths.bunqJsonProductionFile)); | 
					
						
							|  |  |  |       apiKey = this.options.apiKey; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // run the bunq application with our API key
 | 
					
						
							|  |  |  |     await this.bunqJSClient.run( | 
					
						
							| 
									
										
										
										
											2019-10-03 14:04:15 +02:00
										 |  |  |       apiKey, | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  |       this.permittedIps, | 
					
						
							|  |  |  |       this.options.environment, | 
					
						
							|  |  |  |       this.encryptionKey | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // install a new keypair
 | 
					
						
							|  |  |  |     await this.bunqJSClient.install(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // register this device
 | 
					
						
							|  |  |  |     await this.bunqJSClient.registerDevice(this.options.deviceName); | 
					
						
							| 
									
										
										
										
											2019-09-26 13:59:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  |     // register a new session
 | 
					
						
							|  |  |  |     await this.bunqJSClient.registerSession(); | 
					
						
							|  |  |  |     await this.getUserId(); | 
					
						
							| 
									
										
										
										
											2019-09-26 13:59:33 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * lists all users | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   private async getUserId() { | 
					
						
							|  |  |  |     const users = await this.bunqJSClient.api.user.list(); | 
					
						
							|  |  |  |     if (users.UserPerson) { | 
					
						
							|  |  |  |       this.userId = users.UserPerson.id; | 
					
						
							|  |  |  |     } else if (users.UserCompany) { | 
					
						
							|  |  |  |       this.userId = users.UserCompany.id; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       console.log('could not determine user id'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public async getAccounts() { | 
					
						
							|  |  |  |     const apiMonetaryAccounts = await this.bunqJSClient.api.monetaryAccount.list(this.userId); | 
					
						
							|  |  |  |     const accountsArray: MonetaryAccount[] = []; | 
					
						
							|  |  |  |     for (const apiAccount of apiMonetaryAccounts) { | 
					
						
							|  |  |  |       accountsArray.push(MonetaryAccount.fromAPIObject(this, apiAccount)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return accountsArray; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-10-03 14:04:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-15 17:21:23 +00:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * stops the instance | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-10-03 14:04:15 +02:00
										 |  |  |   public async stop() { | 
					
						
							|  |  |  |     if (this.bunqJSClient) { | 
					
						
							|  |  |  |       this.bunqJSClient.setKeepAlive(false); | 
					
						
							| 
									
										
										
										
											2019-12-15 17:21:23 +00:00
										 |  |  |       await this.bunqJSClient.destroyApiSession(); | 
					
						
							| 
									
										
										
										
											2019-10-03 14:04:15 +02:00
										 |  |  |       this.bunqJSClient = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-10-02 23:34:05 +02:00
										 |  |  | } |