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