|
| 1 | +import should from 'should'; |
| 2 | +import { KeyPair } from '../../src/keyPair'; |
| 3 | +import { enterpriseAccounts } from '../resources/'; |
| 4 | +import { PrivateKey, PublicKey } from '@emurgo/cardano-serialization-lib-nodejs'; |
| 5 | +import { AddressFormat, toHex } from '@bitgo/sdk-core'; |
| 6 | + |
| 7 | +describe('Ada Keypair', () => { |
| 8 | + const defaultSeed = { seed: Buffer.alloc(32) }; |
| 9 | + |
| 10 | + describe('Keypair creation', () => { |
| 11 | + it('initial state', () => { |
| 12 | + const keyPair = new KeyPair(); |
| 13 | + should.exists(keyPair.getKeys().prv); |
| 14 | + should.exists(keyPair.getKeys().prv); |
| 15 | + }); |
| 16 | + |
| 17 | + it('initialization from private key', () => { |
| 18 | + let keyPair = new KeyPair({ |
| 19 | + prv: enterpriseAccounts.account1.secretKey, |
| 20 | + }); |
| 21 | + |
| 22 | + should.equal( |
| 23 | + keyPair.getKeys().prv, |
| 24 | + toHex(PrivateKey.from_bech32(enterpriseAccounts.account1.secretKey).as_bytes()) |
| 25 | + ); |
| 26 | + |
| 27 | + should.equal(keyPair.getKeys().pub, enterpriseAccounts.account1.hexPublic); |
| 28 | + |
| 29 | + keyPair = new KeyPair({ |
| 30 | + prv: enterpriseAccounts.account2.secretKey, |
| 31 | + }); |
| 32 | + |
| 33 | + should.equal( |
| 34 | + keyPair.getKeys().prv, |
| 35 | + toHex(PrivateKey.from_bech32(enterpriseAccounts.account2.secretKey).as_bytes()) |
| 36 | + ); |
| 37 | + should.equal(keyPair.getKeys().pub, enterpriseAccounts.account2.hexPublic); |
| 38 | + }); |
| 39 | + |
| 40 | + it('initialization from public key', () => { |
| 41 | + let keyPair = new KeyPair({ pub: enterpriseAccounts.account1.publicKey }); |
| 42 | + |
| 43 | + should.equal( |
| 44 | + keyPair.getKeys().pub, |
| 45 | + toHex(PublicKey.from_bech32(enterpriseAccounts.account1.publicKey).as_bytes()) |
| 46 | + ); |
| 47 | + |
| 48 | + keyPair = new KeyPair({ pub: enterpriseAccounts.account2.publicKey }); |
| 49 | + should.equal(keyPair.getAddress(AddressFormat.testnet), enterpriseAccounts.account2.baseAddress); |
| 50 | + |
| 51 | + keyPair = new KeyPair({ pub: enterpriseAccounts.account4.publicKeyHex }); |
| 52 | + should.equal(keyPair.getKeys().pub, enterpriseAccounts.account4.publicKeyHex); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('KeyPair validation', () => { |
| 57 | + it('should fail to create from an invalid seed', () => { |
| 58 | + const seed = { seed: Buffer.alloc(8) }; // Seed should be 512 bits (64 bytes) |
| 59 | + should.throws(() => new KeyPair(seed), 'bad seed size'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should fail to create from an invalid public key', () => { |
| 63 | + const source = { |
| 64 | + pub: '01D63D', |
| 65 | + }; |
| 66 | + should.throws(() => new KeyPair(source), 'address seems to be malformed'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should fail to create from an invalid private key', () => { |
| 70 | + const source = { |
| 71 | + prv: '82A34', |
| 72 | + }; |
| 73 | + should.throws(() => new KeyPair(source), 'Invalid base32 characters'); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('getAddress', () => { |
| 78 | + it('should get an address', () => { |
| 79 | + let keyPair = new KeyPair({ prv: enterpriseAccounts.account1.secretKey }); |
| 80 | + let address = keyPair.getAddress(AddressFormat.testnet); |
| 81 | + address.should.equal(enterpriseAccounts.account1.baseAddress); |
| 82 | + |
| 83 | + keyPair = new KeyPair({ prv: enterpriseAccounts.account3.secretKey }); |
| 84 | + address = keyPair.getAddress(AddressFormat.testnet); |
| 85 | + address.should.equal(enterpriseAccounts.account3.baseAddress); |
| 86 | + |
| 87 | + keyPair = new KeyPair({ pub: enterpriseAccounts.account4.publicKeyHex }); |
| 88 | + address = keyPair.getAddress(AddressFormat.testnet); |
| 89 | + address.should.equal(enterpriseAccounts.account4.baseAddress); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('getKeys', () => { |
| 94 | + it('should get private and public keys in the protocol default format', () => { |
| 95 | + const keyPair = new KeyPair(defaultSeed); |
| 96 | + const { prv, pub } = keyPair.getKeys(); |
| 97 | + |
| 98 | + prv!.should.equal(prv); |
| 99 | + pub.should.equal(pub); |
| 100 | + const address = keyPair.getAddress(AddressFormat.testnet); |
| 101 | + address.should.equal(enterpriseAccounts.seedAccount.enterpriseAddress); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should get private and public keys for a random seed', () => { |
| 105 | + const keyPair = new KeyPair(); |
| 106 | + const { prv, pub } = keyPair.getKeys(); |
| 107 | + should.exist(prv); |
| 108 | + should.exist(pub); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments