You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
| 
								 
											5 years ago
										 
									 | 
							
								import { KeyPair } from '../../libtextsecure/libsignal-protocol';
							 | 
						||
| 
								 | 
							
								import { fromHexToArray, toHex } from '../session/utils/String';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export type HexKeyPair = {
							 | 
						||
| 
								 | 
							
								  publicHex: string;
							 | 
						||
| 
								 | 
							
								  privateHex: string;
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export class ECKeyPair {
							 | 
						||
| 
								 | 
							
								  public readonly publicKeyData: Uint8Array;
							 | 
						||
| 
								 | 
							
								  public readonly privateKeyData: Uint8Array;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  constructor(publicKeyData: Uint8Array, privateKeyData: Uint8Array) {
							 | 
						||
| 
								 | 
							
								    this.publicKeyData = publicKeyData;
							 | 
						||
| 
								 | 
							
								    this.privateKeyData = privateKeyData;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  public static fromArrayBuffer(pub: ArrayBuffer, priv: ArrayBuffer) {
							 | 
						||
| 
								 | 
							
								    return new ECKeyPair(new Uint8Array(pub), new Uint8Array(priv));
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  public static fromKeyPair(pair: KeyPair) {
							 | 
						||
| 
								 | 
							
								    return new ECKeyPair(
							 | 
						||
| 
								 | 
							
								      new Uint8Array(pair.pubKey),
							 | 
						||
| 
								 | 
							
								      new Uint8Array(pair.privKey)
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  public static fromHexKeyPair(pair: HexKeyPair) {
							 | 
						||
| 
								 | 
							
								    return new ECKeyPair(
							 | 
						||
| 
								 | 
							
								      fromHexToArray(pair.publicHex),
							 | 
						||
| 
								 | 
							
								      fromHexToArray(pair.privateHex)
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  public toString() {
							 | 
						||
| 
								 | 
							
								    const hexKeypair = this.toHexKeyPair();
							 | 
						||
| 
								 | 
							
								    return `ECKeyPair: ${hexKeypair.publicHex} ${hexKeypair.privateHex}`;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  public toHexKeyPair(): HexKeyPair {
							 | 
						||
| 
								 | 
							
								    const publicHex = toHex(this.publicKeyData);
							 | 
						||
| 
								 | 
							
								    const privateHex = toHex(this.privateKeyData);
							 | 
						||
| 
								 | 
							
								    return {
							 | 
						||
| 
								 | 
							
								      publicHex,
							 | 
						||
| 
								 | 
							
								      privateHex,
							 | 
						||
| 
								 | 
							
								    };
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 |