|
| 1 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 2 | +import { Tltc } from '@bitgo/sdk-coin-ltc'; |
| 3 | +import * as sjcl from '@bitgo/sjcl'; |
| 4 | +require('dotenv').config({ path: '../../.env' }); |
| 5 | + |
| 6 | +const bitgo = new BitGoAPI({ |
| 7 | + accessToken: process.env.TESTNET_ACCESS_TOKEN, |
| 8 | + env: 'test', |
| 9 | +}); |
| 10 | + |
| 11 | +const coin = 'tltc'; |
| 12 | +bitgo.register(coin, Tltc.createInstance); |
| 13 | + |
| 14 | +async function main() { |
| 15 | + // 1. Generate a key pair for the coin |
| 16 | + const baseCoin = bitgo.coin(coin); |
| 17 | + const keyPair = baseCoin.generateKeyPair(); |
| 18 | + const walletPassphrase = 'myStrongPassphrase'; |
| 19 | + const encryptedPrv = sjcl.encrypt(walletPassphrase, keyPair.prv); |
| 20 | + |
| 21 | + // 2. Call the function with valid arguments so that the function validates the created key |
| 22 | + try { |
| 23 | + baseCoin.assertIsValidKey({ |
| 24 | + encryptedPrv, |
| 25 | + walletPassphrase, |
| 26 | + publicKey: keyPair.pub, |
| 27 | + }); |
| 28 | + console.log('Key validated successfully with correct passphrase and encryptedPrv.'); |
| 29 | + } catch (e) { |
| 30 | + console.error('Unexpected error occurred', e); |
| 31 | + } |
| 32 | + |
| 33 | + // 3. Call the function with the incorrect passphrase, console log the captured error and explain it's the wrong password |
| 34 | + try { |
| 35 | + baseCoin.assertIsValidKey({ |
| 36 | + encryptedPrv, |
| 37 | + walletPassphrase: 'wrongPassphrase', |
| 38 | + publicKey: keyPair.pub, |
| 39 | + }); |
| 40 | + } catch (e) { |
| 41 | + console.log('Error with wrong passphrase:', e.message); |
| 42 | + console.log('This error is expected because the passphrase is incorrect.'); |
| 43 | + } |
| 44 | + |
| 45 | + // 4. Call the function with a modified encryptedPrv, console log the captured error and explain that the prv is wrong |
| 46 | + try { |
| 47 | + const tamperedEncryptedPrv = encryptedPrv.slice(0, -1) + (encryptedPrv.slice(-1) === 'a' ? 'b' : 'a'); |
| 48 | + baseCoin.assertIsValidKey({ |
| 49 | + encryptedPrv: tamperedEncryptedPrv, |
| 50 | + walletPassphrase, |
| 51 | + publicKey: keyPair.pub, |
| 52 | + }); |
| 53 | + } catch (e) { |
| 54 | + console.log('Error with tampered encryptedPrv:', e.message); |
| 55 | + console.log('This error is expected because the encrypted private key was modified and cannot be decrypted.'); |
| 56 | + } |
| 57 | + |
| 58 | + // 5. Call the function with a mismatched public key, console log the captured error and explain the result |
| 59 | + try { |
| 60 | + // Generate a new key pair to get a different public key |
| 61 | + const anotherKeyPair = baseCoin.generateKeyPair(); |
| 62 | + baseCoin.assertIsValidKey({ |
| 63 | + encryptedPrv, |
| 64 | + walletPassphrase, |
| 65 | + publicKey: anotherKeyPair.pub, // mismatched public key |
| 66 | + }); |
| 67 | + } catch (e) { |
| 68 | + console.log('Error with mismatched public key:', e.message); |
| 69 | + console.log('This error is expected because the public key does not match the decrypted private key.'); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +main().catch((e) => console.error(e)); |
0 commit comments