|
| 1 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 2 | +import { Eddsa } from '@bitgo/sdk-core'; |
| 3 | +import { Ed25519Bip32HdTree, HDTree } from '@bitgo/sdk-lib-mpc'; |
| 4 | +import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; |
| 5 | +import assert from 'assert'; |
| 6 | +import should from 'should'; |
| 7 | + |
| 8 | +import { KeyPair, Tcanton } from '../../src'; |
| 9 | +import utils from '../../src/lib/utils'; |
| 10 | + |
| 11 | +describe('Canton KeyPair', function () { |
| 12 | + let rootKeychain: string; |
| 13 | + let rootPublicKey: string; |
| 14 | + let MPC: Eddsa; |
| 15 | + let hdTree: HDTree; |
| 16 | + let bitgo: TestBitGoAPI; |
| 17 | + let basecoin: Tcanton; |
| 18 | + |
| 19 | + before(async () => { |
| 20 | + hdTree = await Ed25519Bip32HdTree.initialize(); |
| 21 | + MPC = await Eddsa.initialize(hdTree); |
| 22 | + const A = MPC.keyShare(1, 2, 3); |
| 23 | + const B = MPC.keyShare(2, 2, 3); |
| 24 | + const C = MPC.keyShare(3, 2, 3); |
| 25 | + |
| 26 | + const A_combine = MPC.keyCombine(A.uShare, [B.yShares[1], C.yShares[1]]); |
| 27 | + |
| 28 | + const commonKeychain = A_combine.pShare.y + A_combine.pShare.chaincode; |
| 29 | + rootKeychain = MPC.deriveUnhardened(commonKeychain, 'm/0'); |
| 30 | + rootPublicKey = Buffer.from(rootKeychain.slice(0, 64), 'hex').toString('base64'); |
| 31 | + bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' }); |
| 32 | + bitgo.safeRegister('tcanton', Tcanton.createInstance); |
| 33 | + basecoin = bitgo.coin('tcanton') as Tcanton; |
| 34 | + }); |
| 35 | + |
| 36 | + describe('should create a valid KeyPair', () => { |
| 37 | + it('from an empty value', async () => { |
| 38 | + const keyPair = new KeyPair(); |
| 39 | + should.exists(keyPair.getKeys().prv); |
| 40 | + should.exists(keyPair.getKeys().pub); |
| 41 | + const address = utils.getAddressFromPublicKey(keyPair.getKeys().pub); |
| 42 | + should.exists(address); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('Keypair from derived Public Key', () => { |
| 47 | + it('should create keypair with just derived public key', () => { |
| 48 | + const keyPair = new KeyPair({ pub: rootPublicKey }); |
| 49 | + keyPair.getKeys().pub.should.equal(rootPublicKey); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should derived ed25519 public key should be valid', () => { |
| 53 | + utils.isValidPublicKey(rootPublicKey).should.be.true(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('Keypair from random seed', () => { |
| 58 | + it('should generate a keypair from random seed', function () { |
| 59 | + const keyPair = basecoin.generateKeyPair(); |
| 60 | + keyPair.should.have.property('pub'); |
| 61 | + keyPair.should.have.property('prv'); |
| 62 | + if (keyPair.pub) { |
| 63 | + basecoin.isValidPub(keyPair.pub).should.equal(true); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('should fail to create a KeyPair', function () { |
| 69 | + it('from an invalid public key', () => { |
| 70 | + const source = { |
| 71 | + pub: '01D63D', |
| 72 | + }; |
| 73 | + |
| 74 | + assert.throws(() => new KeyPair(source)); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments