@@ -2,14 +2,208 @@ import 'should';
22import { TestBitGo , TestBitGoAPI } from '@bitgo/sdk-test' ;
33import { BitGoAPI } from '@bitgo/sdk-api' ;
44import { Apt , Tapt } from '../../src' ;
5+ import * as testData from '../resources/apt' ;
6+ import _ from 'lodash' ;
7+ import BigNumber from 'bignumber.js' ;
8+ import sinon from 'sinon' ;
9+ import assert from 'assert' ;
510
611describe ( 'APT:' , function ( ) {
712 let bitgo : TestBitGoAPI ;
13+ let basecoin ;
14+ let newTxPrebuild ;
15+ let newTxParams ;
16+
17+ const txPreBuild = {
18+ txHex : testData . TRANSFER ,
19+ txInfo : { } ,
20+ } ;
21+
22+ const txParams = {
23+ recipients : testData . recipients ,
24+ } ;
825
926 before ( function ( ) {
1027 bitgo = TestBitGo . decorate ( BitGoAPI , { env : 'mock' } ) ;
1128 bitgo . safeRegister ( 'apt' , Apt . createInstance ) ;
1229 bitgo . safeRegister ( 'tapt' , Tapt . createInstance ) ;
1330 bitgo . initializeTestVars ( ) ;
31+ basecoin = bitgo . coin ( 'tapt' ) ;
32+ newTxPrebuild = ( ) => {
33+ return _ . cloneDeep ( txPreBuild ) ;
34+ } ;
35+ newTxParams = ( ) => {
36+ return _ . cloneDeep ( txParams ) ;
37+ } ;
38+ } ) ;
39+
40+ it ( 'should return the right info' , function ( ) {
41+ const apt = bitgo . coin ( 'apt' ) ;
42+ const tapt = bitgo . coin ( 'tapt' ) ;
43+
44+ apt . getChain ( ) . should . equal ( 'apt' ) ;
45+ apt . getFamily ( ) . should . equal ( 'apt' ) ;
46+ apt . getFullName ( ) . should . equal ( 'Aptos' ) ;
47+ apt . getBaseFactor ( ) . should . equal ( 1e8 ) ;
48+
49+ tapt . getChain ( ) . should . equal ( 'tapt' ) ;
50+ tapt . getFamily ( ) . should . equal ( 'apt' ) ;
51+ tapt . getFullName ( ) . should . equal ( 'Testnet Aptos' ) ;
52+ tapt . getBaseFactor ( ) . should . equal ( 1e8 ) ;
53+ } ) ;
54+
55+ describe ( 'Verify transaction: ' , ( ) => {
56+ it ( 'should succeed to verify transaction' , async function ( ) {
57+ const txPrebuild = newTxPrebuild ( ) ;
58+ const txParams = newTxParams ( ) ;
59+ const verification = { } ;
60+ const isTransactionVerified = await basecoin . verifyTransaction ( { txParams, txPrebuild, verification } ) ;
61+ isTransactionVerified . should . equal ( true ) ;
62+ } ) ;
63+
64+ it ( 'should succeed to verify transaction when recipients amount are numbers' , async function ( ) {
65+ const txPrebuild = newTxPrebuild ( ) ;
66+ const txParamsWithNumberAmounts = newTxParams ( ) ;
67+ txParamsWithNumberAmounts . recipients = txParamsWithNumberAmounts . recipients . map ( ( { address, amount } ) => {
68+ return { address, amount : Number ( amount ) } ;
69+ } ) ;
70+ const verification = { } ;
71+ const isTransactionVerified = await basecoin . verifyTransaction ( {
72+ txParams : txParamsWithNumberAmounts ,
73+ txPrebuild,
74+ verification,
75+ } ) ;
76+ isTransactionVerified . should . equal ( true ) ;
77+ } ) ;
78+
79+ it ( 'should fail to verify transaction with invalid param' , async function ( ) {
80+ const txPrebuild = { } ;
81+ const txParams = newTxParams ( ) ;
82+ txParams . recipients = undefined ;
83+ await basecoin
84+ . verifyTransaction ( {
85+ txParams,
86+ txPrebuild,
87+ } )
88+ . should . rejectedWith ( 'missing required tx prebuild property txHex' ) ;
89+ } ) ;
90+ } ) ;
91+ describe ( 'Parse Transactions: ' , ( ) => {
92+ const transferInputsResponse = [
93+ {
94+ address : testData . recipients [ 0 ] . address ,
95+ amount : new BigNumber ( testData . AMOUNT ) ,
96+ } ,
97+ ] ;
98+
99+ const transferOutputsResponse = [
100+ {
101+ address : testData . recipients [ 0 ] . address ,
102+ amount : testData . recipients [ 0 ] . amount ,
103+ } ,
104+ ] ;
105+
106+ it ( 'should parse a transfer transaction' , async function ( ) {
107+ const parsedTransaction = await basecoin . parseTransaction ( {
108+ txHex : testData . TRANSFER ,
109+ } ) ;
110+
111+ parsedTransaction . should . deepEqual ( {
112+ inputs : transferInputsResponse ,
113+ outputs : transferOutputsResponse ,
114+ } ) ;
115+ } ) ;
116+
117+ it ( 'should fail to parse a transfer transaction when explainTransaction response is undefined' , async function ( ) {
118+ const stub = sinon . stub ( Apt . prototype , 'explainTransaction' ) ;
119+ stub . resolves ( undefined ) ;
120+ await basecoin . parseTransaction ( { txHex : testData . TRANSFER } ) . should . be . rejectedWith ( 'Invalid transaction' ) ;
121+ stub . restore ( ) ;
122+ } ) ;
123+ } ) ;
124+
125+ describe ( 'Address Validation' , ( ) => {
126+ let keychains ;
127+ let commonKeychain ;
128+
129+ before ( function ( ) {
130+ commonKeychain =
131+ '19bdfe2a4b498a05511381235a8892d54267807c4a3f654e310b938b8b424ff4adedbe92f4c146de641c67508a961324c8504cdf8e0c0acbb68d6104ccccd781' ;
132+ keychains = [
133+ {
134+ id : '6424c353eaf78d000766e95949868468' ,
135+ source : 'user' ,
136+ type : 'tss' ,
137+ commonKeychain :
138+ '19bdfe2a4b498a05511381235a8892d54267807c4a3f654e310b938b8b424ff4adedbe92f4c146de641c67508a961324c8504cdf8e0c0acbb68d6104ccccd781' ,
139+ encryptedPrv :
140+ '{"iv":"cZd5i7L4RxtwrALW2rK7UA==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"5zgoH1Bd3Fw=","ct":"9vVlnXFRtrM9FVEo+d2chbGHlM9lFZemueBuAs3BIkPo33Fo7jzwwNK/kIWkEyg+NmEBd5IaqAS157nvvvwzzsmMWlQdUz9qbmXNv3pg987cXFR08exS+4uhwP1YNOjJTRvRNcO9ZqHb46d4fmyJ/yC9/susCge7r/EsbaN5C3afv1dzybuq912FwaQElZLYYp5BICudFOMZ9k0UDMfKM/PMDkH7WexoGHr9GKq/bgCH2B39TZZyHKU6Uy47lXep2s6h0DrMwHOrnmiL3DZjOj88Ynvphlzxuo4eOlD2UHia2+nvIaISYs29Pr0DAvREutchvcBpExj1kWWPv7hQYrv8F0NAdatsbWl3w+xKyfiMKo1USlrwyJviypGtQtXOJyw0XPN0rv2+L5lW8BbjpzHfYYN13fJTedlGTFhhkzVtbbPAKE02kx7zCJcjYaiexdSTsrDLScYNT9/Jhdt27KpsooehwVohLfSKz4vbFfRu2MPZw3/+c/hfiJNgtz6esWbnxGrcE8U2IwPYCaK+Ghk4DcqWNIni59RI5B5kAsQOToII40qPN510uTgxBSPO7q7MHgkxdd4CqBq+ojr9j0P7oao8E5Y+CBDJrojDoCh1oCCDW9vo2dXlVcD8SIbw7U/9AfvEbA4xyE/5md1M7CIwLnWs2Ynv0YtaKoqhdS9x6FmHlMDhN/DKHinrwmowtrTT82fOkpO5g9saSmgU7Qy3gLt8t+VwdEyeFeQUKRSyci8qgqXQaZIg4+aXgaSOnlCFMtmB8ekYxEhTY5uzRfrNgS4s1QeqFBpNtUF+Ydi297pbVXnJoXAN+SVWd80GCx+yI2dpVC89k3rOWK9WeyqlnzuLJWp2RIOB9cdW8GFv/fN+QAJpYeVxOE4+nZDsKnsj8nKcg9t4Dlx1G6gLM1/Vq9YxNLbuzuRC0asUYvdMnoMvszmpm++TxndYisgNYscpZSoz7wvcazJNEPfhPVjEkd6tUUuN4GM35H0DmKCUQNT+a6B6hmHlTZvjxiyGAg5bY59hdjvJ+22QduazlEEC6LI3HrA7uK0TpplWzS1tCIFvTMUhj65DEZmNJ2+ZY9bQ4vsMf+DRR3OOG4t+DMlNfjOd3zNv3QoY95BjfWpryFwPzDq7bCP67JDsoj7j2TY5FRSrRkD77H0Ewlux2cWfjRTwcMHcdQxxuV0OP0aNjGDjybFN"}' ,
141+ } ,
142+ {
143+ id : '6424c353eaf78d000766e96137d4404b' ,
144+ source : 'backup' ,
145+ type : 'tss' ,
146+ commonKeychain :
147+ '19bdfe2a4b498a05511381235a8892d54267807c4a3f654e310b938b8b424ff4adedbe92f4c146de641c67508a961324c8504cdf8e0c0acbb68d6104ccccd781' ,
148+ encryptedPrv :
149+ '{"iv":"vi0dPef/Rx7kG/pRySQi6Q==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"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yWvhm4eB8u/e66l3o0qbaSx8q9YYmT9EpRcl5TP4ThLBKETYdzVvg4exjQfektMatk5EyUpEIhZPXh5vXpJZesdfO9LJ8zTaHBsBjDPU7cdNgQMbebpataRi8A0el2/IJXl+E+olgAz5zC4i2O1Q=="}' ,
150+ } ,
151+ {
152+ id : '6424c353eaf78d000766e9510b125fba' ,
153+ source : 'bitgo' ,
154+ type : 'tss' ,
155+ commonKeychain :
156+ '19bdfe2a4b498a05511381235a8892d54267807c4a3f654e310b938b8b424ff4adedbe92f4c146de641c67508a961324c8504cdf8e0c0acbb68d6104ccccd781' ,
157+ verifiedVssProof : true ,
158+ isBitGo : true ,
159+ } ,
160+ ] ;
161+ } ) ;
162+
163+ it ( 'should return true when validating a well formatted address' , async function ( ) {
164+ const address = 'f941ae3cbe5645dccc15da8346b533f7f91f202089a5521653c062b2ff10b304' ;
165+ basecoin . isValidAddress ( address ) . should . equal ( true ) ;
166+ } ) ;
167+
168+ it ( 'should return true when validating a well formatted address prefixed with 0x' , async function ( ) {
169+ const address = '0xf941ae3cbe5645dccc15da8346b533f7f91f202089a5521653c062b2ff10b304' ;
170+ basecoin . isValidAddress ( address ) . should . equal ( true ) ;
171+ } ) ;
172+
173+ it ( 'should return false when validating an old address' , async function ( ) {
174+ const address = '0x2959bfc3fdb7dc23fed8deba2fafb70f3e606a59' ;
175+ basecoin . isValidAddress ( address ) . should . equal ( false ) ;
176+ } ) ;
177+
178+ it ( 'should return false when validating an incorrectly formatted' , async function ( ) {
179+ const address = 'wrongaddress' ;
180+ basecoin . isValidAddress ( address ) . should . equal ( false ) ;
181+ } ) ;
182+
183+ it ( 'should return true for isWalletAddress with valid address for index 4' , async function ( ) {
184+ const newAddress = '0x8b3c7807730d75792dd6c49732cf9f014d6984a9c77d386bdb1072a9e537d8d8' ;
185+ const index = 4 ;
186+
187+ const params = { commonKeychain, address : newAddress , index, keychains } ;
188+ ( await basecoin . isWalletAddress ( params ) ) . should . equal ( true ) ;
189+ } ) ;
190+
191+ it ( 'should throw error for isWalletAddress when keychains is missing' , async function ( ) {
192+ const address = '0x2959bfc3fdb7dc23fed8deba2fafb70f3e606a59' ;
193+ const index = 0 ;
194+
195+ const params = { commonKeychain, address, index } ;
196+ await assert . rejects ( async ( ) => basecoin . isWalletAddress ( params ) ) ;
197+ } ) ;
198+
199+ it ( 'should throw error for isWalletAddress when new address is invalid' , async function ( ) {
200+ const wrongAddress = 'badAddress' ;
201+ const index = 0 ;
202+
203+ const params = { commonKeychain, address : wrongAddress , index } ;
204+ await assert . rejects ( async ( ) => basecoin . isWalletAddress ( params ) , {
205+ message : `invalid address: ${ wrongAddress } ` ,
206+ } ) ;
207+ } ) ;
14208 } ) ;
15209} ) ;
0 commit comments