mirror of
				https://github.com/hackku21/loc-chain-backend.git
				synced 2025-06-13 12:53:59 +00:00 
			
		
		
		
	Add reference client implementation for testing
This commit is contained in:
		
							parent
							
								
									67fd01c9e9
								
							
						
					
					
						commit
						0cb38027a1
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -105,3 +105,4 @@ dist | |||||||
| 
 | 
 | ||||||
| lib/* | lib/* | ||||||
| *.gpg.key | *.gpg.key | ||||||
|  | *.salt | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								.idea/jsLibraryMappings.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								.idea/jsLibraryMappings.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <project version="4"> | ||||||
|  |   <component name="JavaScriptLibraryMappings"> | ||||||
|  |     <includedPredefinedLibrary name="Node.js Core" /> | ||||||
|  |   </component> | ||||||
|  | </project> | ||||||
							
								
								
									
										47
									
								
								test_clients.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								test_clients.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,47 @@ | |||||||
|  | const fs = require('fs') | ||||||
|  | const openpgp = require('openpgp') | ||||||
|  | const bcrypt = require('bcrypt') | ||||||
|  | 
 | ||||||
|  | class TestClient { | ||||||
|  |     constructor(keyName, id) { | ||||||
|  |         this.keyName = keyName | ||||||
|  |         this.id = id | ||||||
|  |         this.publicKey = fs.readFileSync(`./${this.keyName}.public.gpg.key`).toString('utf-8') | ||||||
|  |         this.privateKey = fs.readFileSync(`./${this.keyName}.private.gpg.key`).toString('utf-8') | ||||||
|  |         this.salt = fs.readFileSync(`./${this.keyName}.salt`).toString('utf-8') | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     getHash(otherClientId) { | ||||||
|  |         const [lesserId, greaterId] = [otherClientId, this.id].sort() | ||||||
|  |         return bcrypt.hashSync(`${lesserId}-${greaterId}`, this.salt) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     async interactWith(otherClient) { | ||||||
|  |         const hash = this.getHash(otherClient.id) | ||||||
|  |         const message = openpgp.Message.fromText(hash) | ||||||
|  |         const signature = (await openpgp.sign({ | ||||||
|  |             message, privateKeys: await openpgp.readKey({ | ||||||
|  |                 armoredKey: this.privateKey | ||||||
|  |             }) | ||||||
|  |         })).toString() | ||||||
|  | 
 | ||||||
|  |         return { | ||||||
|  |             combinedHash: hash, | ||||||
|  |             timestamp: (new Date).getTime(), | ||||||
|  |             encodedGPSLocation: JSON.stringify({ none: true }), | ||||||
|  |             partnerPublicKey: otherClient.publicKey, | ||||||
|  |             validationSignature: signature | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | ;(async () => { | ||||||
|  |     const client_a = new TestClient('test_a', 'a893irwe') | ||||||
|  |     const client_b = new TestClient('test_b', 'bawesfdi') | ||||||
|  | 
 | ||||||
|  |     const a_transact = await client_a.interactWith(client_b) | ||||||
|  |     const b_transact = await client_b.interactWith(client_a) | ||||||
|  | 
 | ||||||
|  |     console.log(a_transact, b_transact) | ||||||
|  | })() | ||||||
|  | 
 | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user