Skip to content

Commit 6d5bd40

Browse files
author
Arik Sosman
committed
Add support for XRP unsigned transaction explanation
Reviewers: andrew Reviewed By: andrew Subscribers: ben Differential Revision: https://phabricator.bitgo.com/D6241
1 parent fd36b42 commit 6d5bd40

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitgo",
3-
"version": "3.6.0",
3+
"version": "3.7.0",
44
"description": "BitGo Javascript SDK",
55
"main": "./src/index.js",
66
"keywords": [

src/v2/coins/xrp.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Xrp.prototype.supplementGenerateWallet = function(walletParams, keychains) {
130130
if (walletParams.rootPrivateKey) {
131131
const rootPrivateKey = walletParams.rootPrivateKey;
132132
if (typeof rootPrivateKey !== 'string' || rootPrivateKey.length !== 64) {
133-
throw new Error('rootPrivateKey needs to be a hexadecimal private key string')
133+
throw new Error('rootPrivateKey needs to be a hexadecimal private key string');
134134
}
135135
keyPair = prova.ECPair.fromPrivateKeyBuffer(Buffer.from(walletParams.rootPrivateKey, 'hex'));
136136
}
@@ -225,7 +225,16 @@ Xrp.prototype.supplementGenerateWallet = function(walletParams, keychains) {
225225
* @returns {{displayOrder: [string,string,string,string,string], id: *, outputs: Array, changeOutputs: Array}}
226226
*/
227227
Xrp.prototype.explainTransaction = function(params) {
228-
var transaction = rippleBinaryCodec.decode(params.txHex);
228+
let transaction;
229+
try {
230+
transaction = rippleBinaryCodec.decode(params.txHex);
231+
} catch (e) {
232+
try {
233+
transaction = JSON.parse(params.txHex);
234+
} catch (e) {
235+
throw new Error('txHex needs to be either hex or JSON string for XRP');
236+
}
237+
}
229238
var id = rippleHashes.computeBinaryTransactionHash(params.txHex);
230239
var changeAmount = 0;
231240
var explanation = {

test/v2/coins/xrp.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('XRP:', function() {
1717
return bitgo.authenticateTestUser(bitgo.testUserOTP())
1818
.then(function() {
1919
basecoin = bitgo.coin('txrp');
20-
})
20+
});
2121
});
2222

2323
it('Should verify addresses', function() {
@@ -73,4 +73,22 @@ describe('XRP:', function() {
7373
});
7474
});
7575

76+
it('Should be able to explain an XRP transaction', function() {
77+
const signedExplanation = basecoin.explainTransaction({ txHex: '120000228000000024000000072E00000000201B0018D07161400000000003DE2968400000000000002D73008114726D0D8A26568D5D9680AC80577C912236717191831449EE221CCACC4DD2BF8862B22B0960A84FC771D9F3E010732103AFBB6845826367D738B0D42EA0756C94547E70B064E8FE1260CF21354C898B0B74473045022100CA3A98AA6FC8CCA251C3A2754992E474EA469884EB8D489D2B180EB644AC7695022037EB886DCF57928E5844DB73C2E86DE553FB59DCFC9408F3FD5D802ADB69DFCC8114F0DBA9D34C77B6769F6142AB7C9D0AF67D113EBCE1F1' });
78+
const unsignedExplanation = basecoin.explainTransaction({ txHex: '{"TransactionType":"Payment","Account":"rBSpCz8PafXTJHppDcNnex7dYnbe3tSuFG","Destination":"rfjub8A4dpSD5nnszUFTsLprxu1W398jwc","DestinationTag":0,"Amount":"253481","Flags":2147483648,"LastLedgerSequence":1626225,"Fee":"45","Sequence":7}' });
79+
unsignedExplanation.id.should.equal('CA528085F95335027A8D8555E685500C5B3E325AAB22AA0BCB56605CDF97B1C8');
80+
signedExplanation.id.should.equal('D52681436CC5B94E9D00BC8172047B1A6F3C028D2D0A5CDFB81680039C48ADFD');
81+
unsignedExplanation.outputAmount.should.equal('253481');
82+
signedExplanation.outputAmount.should.equal('253481');
83+
});
84+
85+
it('Should be unable to explain bogus XRP transaction', function() {
86+
try {
87+
basecoin.explainTransaction({ txHex: 'abcdefgH' });
88+
throw new Error('this is the wrong error!');
89+
} catch (e) {
90+
e.message.should.equal('txHex needs to be either hex or JSON string for XRP');
91+
}
92+
});
93+
7694
});

0 commit comments

Comments
 (0)