|
| 1 | +/** |
| 2 | + * @prettier |
| 3 | + */ |
| 4 | +import sinon from 'sinon'; |
| 5 | +import 'should'; |
| 6 | +import { Ofc } from '../../../src/coins/ofc'; |
| 7 | +import { OfcToken } from '../../../src/coins/ofcToken'; |
| 8 | +import { BaseCoin } from '../../../src/bitgo/baseCoin/baseCoin'; |
| 9 | +import { Wallet } from '../../../src'; |
| 10 | + |
| 11 | +const TEST_TOKEN_CONFIG = { |
| 12 | + coin: 'ofcusdt', |
| 13 | + decimalPlaces: 6, |
| 14 | + name: 'OFCUSDT', |
| 15 | + type: 'ofcusdt', |
| 16 | + backingCoin: 'usdt', |
| 17 | + isFiat: false, |
| 18 | +}; |
| 19 | + |
| 20 | +describe('Ofc / OfcToken', function () { |
| 21 | + let mockBitGo: any; |
| 22 | + |
| 23 | + beforeEach(function () { |
| 24 | + mockBitGo = { url: sinon.stub().returns('https://test.bitgo.com') }; |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(function () { |
| 28 | + sinon.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('signMessage', function () { |
| 32 | + let ofc: Ofc; |
| 33 | + |
| 34 | + beforeEach(function () { |
| 35 | + ofc = new Ofc(mockBitGo); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('with a Wallet instance', function () { |
| 39 | + it('should delegate to wallet.toTradingAccount().signPayload() and return a Buffer', async function () { |
| 40 | + const hexSignature = 'deadbeef'; |
| 41 | + const signPayloadStub = sinon.stub().resolves(hexSignature); |
| 42 | + |
| 43 | + const mockBaseCoin = { supportsTss: sinon.stub().returns(false), getMPCAlgorithm: sinon.stub() }; |
| 44 | + const walletData = { |
| 45 | + id: 'wallet-id', |
| 46 | + keys: ['key1', 'key2', 'key3'], |
| 47 | + multisigType: 'onchain', |
| 48 | + enterprise: 'ent-id', |
| 49 | + }; |
| 50 | + const wallet = new Wallet(mockBitGo, mockBaseCoin as any, walletData); |
| 51 | + sinon.stub(wallet, 'toTradingAccount').returns({ signPayload: signPayloadStub } as any); |
| 52 | + |
| 53 | + const message = 'test message'; |
| 54 | + const result = await ofc.signMessage(wallet, message); |
| 55 | + |
| 56 | + signPayloadStub.calledOnceWith({ payload: message }).should.be.true(); |
| 57 | + result.should.deepEqual(Buffer.from(hexSignature, 'hex')); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('with a prv key', function () { |
| 62 | + it('should delegate to the base class signMessage', async function () { |
| 63 | + const expectedResult = Buffer.from('basesignature', 'hex'); |
| 64 | + const superSignMessageStub = sinon.stub(BaseCoin.prototype, 'signMessage').resolves(expectedResult); |
| 65 | + |
| 66 | + const key = { |
| 67 | + prv: 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqhuCo36EkzGH6qiT9mJHBvuPKtLRYD4NxFb5hgXMQBB2LLT6mxLDHHo', |
| 68 | + }; |
| 69 | + const message = 'test message'; |
| 70 | + const result = await ofc.signMessage(key, message); |
| 71 | + |
| 72 | + superSignMessageStub.calledOnceWith(key, message).should.be.true(); |
| 73 | + result.should.equal(expectedResult); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('signTransaction (OfcToken)', function () { |
| 79 | + let ofcToken: OfcToken; |
| 80 | + const payload = '{"amount":"100","from":"alice","to":"bob"}'; |
| 81 | + |
| 82 | + beforeEach(function () { |
| 83 | + ofcToken = new OfcToken(mockBitGo, TEST_TOKEN_CONFIG); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('with wallet and no prv (BitGo remote signing)', function () { |
| 87 | + it('should call wallet.toTradingAccount().signPayload() without a passphrase', async function () { |
| 88 | + const hexSignature = 'aabbccdd'; |
| 89 | + const signPayloadStub = sinon.stub().resolves(hexSignature); |
| 90 | + const mockWallet = { toTradingAccount: sinon.stub().returns({ signPayload: signPayloadStub }) }; |
| 91 | + |
| 92 | + const result = await ofcToken.signTransaction({ txPrebuild: { payload }, wallet: mockWallet as any }); |
| 93 | + |
| 94 | + signPayloadStub.calledOnceWith({ payload, walletPassphrase: undefined }).should.be.true(); |
| 95 | + result.should.deepEqual({ halfSigned: { payload, signature: hexSignature } }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('with wallet and prv (local signing routed through wallet)', function () { |
| 100 | + it('should call wallet.toTradingAccount().signPayload() with the wallet passphrase', async function () { |
| 101 | + const hexSignature = 'aabbccdd'; |
| 102 | + const passphrase = 'test-passphrase'; |
| 103 | + const signPayloadStub = sinon.stub().resolves(hexSignature); |
| 104 | + const mockWallet = { toTradingAccount: sinon.stub().returns({ signPayload: signPayloadStub }) }; |
| 105 | + |
| 106 | + const result = await ofcToken.signTransaction({ |
| 107 | + txPrebuild: { payload }, |
| 108 | + wallet: mockWallet as any, |
| 109 | + prv: passphrase, |
| 110 | + }); |
| 111 | + |
| 112 | + signPayloadStub.calledOnceWith({ payload, walletPassphrase: passphrase }).should.be.true(); |
| 113 | + result.should.deepEqual({ halfSigned: { payload, signature: hexSignature } }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('with prv only (local signing without wallet)', function () { |
| 118 | + it('should sign locally and return the correct halfSigned result', async function () { |
| 119 | + const signatureBytes = Buffer.from('ccddee', 'hex'); |
| 120 | + sinon.stub(BaseCoin.prototype, 'signMessage').resolves(signatureBytes); |
| 121 | + |
| 122 | + const result = await ofcToken.signTransaction({ txPrebuild: { payload }, prv: 'test-prv' }); |
| 123 | + |
| 124 | + result.should.deepEqual({ halfSigned: { payload, signature: signatureBytes.toString('hex') } }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should pass the prv to signMessage', async function () { |
| 128 | + const signatureBytes = Buffer.from('ccddee', 'hex'); |
| 129 | + const superSignMessageStub = sinon.stub(BaseCoin.prototype, 'signMessage').resolves(signatureBytes); |
| 130 | + const prv = 'test-prv'; |
| 131 | + |
| 132 | + await ofcToken.signTransaction({ txPrebuild: { payload }, prv }); |
| 133 | + |
| 134 | + superSignMessageStub.calledOnceWith({ prv }, payload).should.be.true(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('with neither wallet nor prv', function () { |
| 139 | + it('should throw an error', async function () { |
| 140 | + await ofcToken |
| 141 | + .signTransaction({ txPrebuild: { payload } }) |
| 142 | + .should.be.rejectedWith('You must pass in either one of wallet or prv'); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments