Skip to content

Commit

Permalink
feat(abstract-eth): add chain id as network identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
gianchandania committed Jan 2, 2024
1 parent 73e9551 commit a89a6d2
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 235 deletions.
5 changes: 4 additions & 1 deletion modules/abstract-eth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"check-fmt": "prettier --check .",
"clean": "rm -r ./dist",
"lint": "eslint --quiet .",
"prepare": "npm run build"
"prepare": "npm run build",
"test": "npm run coverage",
"coverage": "nyc -- npm run unit-test",
"unit-test": "mocha"
},
"author": "BitGo SDK Team <[email protected]>",
"license": "MIT",
Expand Down
12 changes: 4 additions & 8 deletions modules/abstract-eth/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ export class TransferBuilder {
*/
coin(coin: string): TransferBuilder {
this._coin = coins.get(coin);
this._network = this._coin.network as EthLikeNetwork;
this._nativeCoinOperationHashPrefix = this._network.nativeCoinOperationHashPrefix;

if (this._coin instanceof ContractAddressDefinedToken) {
this._tokenContractAddress = this._coin.contractAddress.toString();
this._tokenOperationHashPrefix = this._network.tokenOperationHashPrefix;
}

return this;
Expand Down Expand Up @@ -148,7 +144,7 @@ export class TransferBuilder {
operationData = [
['string', 'address', 'uint', 'address', 'uint', 'uint'],
[
this._tokenOperationHashPrefix ?? this.getTokenOperationHashPrefix(),
this.getTokenOperationHashPrefix(),
new BN(ethUtil.stripHexPrefix(this._toAddress), 16),
this._amount,
new BN(ethUtil.stripHexPrefix(this._tokenContractAddress), 16),
Expand All @@ -160,7 +156,7 @@ export class TransferBuilder {
operationData = [
['string', 'address', 'uint', 'bytes', 'uint', 'uint'],
[
this._nativeCoinOperationHashPrefix ?? this.getNativeOperationHashPrefix(),
this.getNativeOperationHashPrefix(),
new BN(ethUtil.stripHexPrefix(this._toAddress), 16),
this._amount,
Buffer.from(ethUtil.padToEven(ethUtil.stripHexPrefix(this._data)) || '', 'hex'),
Expand All @@ -178,7 +174,7 @@ export class TransferBuilder {
* @returns the string prefix
*/
protected getTokenOperationHashPrefix(): string {
return 'ERC20';
return (this._coin.network as EthLikeNetwork).tokenOperationHashPrefix ?? 'ERC20';
}

/**
Expand All @@ -187,7 +183,7 @@ export class TransferBuilder {
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'ETHER';
return (this._coin.network as EthLikeNetwork).nativeCoinOperationHashPrefix ?? 'ETHER';
}

/** Return an expiration time, in seconds, set to one hour from now
Expand Down
3 changes: 3 additions & 0 deletions modules/abstract-eth/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ export function sendMultiSigData(
sequenceId: number,
signature: string
): string {
console.log('send multisig data');
const params = [to, value, toBuffer(data), expireTime, sequenceId, toBuffer(signature)];
console.log('send multisig data params', params);

const method = EthereumAbi.methodID('sendMultiSig', sendMultiSigTypes);
const args = EthereumAbi.rawEncode(sendMultiSigTypes, params);
return addHexPrefix(Buffer.concat([method, args]).toString('hex'));
Expand Down
76 changes: 76 additions & 0 deletions modules/abstract-eth/test/unit/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import assert from 'assert';
import should from 'should';
import { KeyPair, TransferBuilder } from '../../src';

describe('Eth send multi sig builder', function () {
const toAddress = '0x7325A3F7d4f9E86AE62Cf742426078C3755730d5';
const xprv =
'xprv9s21ZrQH143K3D8TXfvAJgHVfTEeQNW5Ys9wZtnUZkqPzFzSjbEJrWC1vZ4GnXCvR7rQL2UFX3RSuYeU9MrERm1XBvACow7c36vnz5iYyj2';
const key = new KeyPair({ prv: xprv }).getKeys().prv as string;
const amount = '100000000000000000'; // equivalent to 0.1 ether

describe('should fail', () => {
it('should fail if a coin does not exists in @bitgo/statics', () => {
should(() => {
new TransferBuilder().coin('inexistentcoin');
}).throw();
});

it('should fail with an invalid key', () => {
const builder = new TransferBuilder()
.expirationTime(1590078260)
.amount(amount)
.to(toAddress)
.contractSequenceId(2)
.key('invalidkey');
should(() => {
builder.signAndBuild();
}).throw('private key length is invalid');
});

it('should fail with an invalid sequence id', () => {
should(() => {
new TransferBuilder().contractSequenceId(-1);
}).throw('Invalid contract sequence id');
});

it('should fail with an invalid destination address', () => {
should(() => {
new TransferBuilder().to('invalidaddress');
}).throw('Invalid address');
});

it('should fail with an invalid amount: text value', () => {
should(() => {
new TransferBuilder().amount('invalidamount');
}).throw('Invalid amount');
});

it('should fail with an invalid amount: negative value', () => {
should(() => {
new TransferBuilder().amount('-10');
}).throw('Invalid amount');
});

it('should fail with an invalid expiration time', () => {
should(() => {
new TransferBuilder().expirationTime(-1);
}).throw('Invalid expiration time');
});

it('should fail if a sequenceId param is missing', () => {
const builder = new TransferBuilder().amount(amount).to(toAddress).key(key);
assert.throws(() => builder.signAndBuild());
});

it('should fail if a destination param is missing', () => {
const builder = new TransferBuilder().amount(amount).contractSequenceId(2).key(key);
assert.throws(() => builder.signAndBuild());
});

it('should fail if a amount param is missing', () => {
const builder = new TransferBuilder().to(toAddress).contractSequenceId(2).key(key);
assert.throws(() => builder.signAndBuild());
});
});
});
80 changes: 0 additions & 80 deletions modules/sdk-coin-arbeth/test/unit/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert';
import should from 'should';
import { KeyPair, TransferBuilder } from '../../src';

Expand All @@ -25,19 +24,6 @@ describe('Eth send multi sig builder', function () {
should.equal(result, testData.SEND_FUNDS_DATA);
});

it('native coin transfer with coin explicitly set should succeed', async () => {
const builder = new TransferBuilder()
.expirationTime(1590078260)
.coin('tarbeth')
.amount(amount)
.to(toAddress)
.contractSequenceId(2)
.key(key)
.data('0x');
const result = builder.signAndBuild();
should.equal(result, testData.SEND_FUNDS_DATA);
});

it('native coin transfer with sequenceId zero should succeed', async () => {
const builder = new TransferBuilder()
.coin('tarbeth')
Expand Down Expand Up @@ -118,70 +104,4 @@ describe('Eth send multi sig builder', function () {
should.equal(result, testData.SEND_FUNDS_DATA);
});
});

describe('should fail', () => {
it('should fail if a coin does not exists in @bitgo/statics', () => {
should(() => {
new TransferBuilder().coin('inexistentcoin');
}).throw();
});

it('should fail with an invalid key', () => {
const builder = new TransferBuilder()
.coin('tarbeth')
.expirationTime(1590078260)
.amount(amount)
.to(toAddress)
.contractSequenceId(2)
.key('invalidkey');
should(() => {
builder.signAndBuild();
}).throw('private key length is invalid');
});

it('should fail with an invalid sequence id', () => {
should(() => {
new TransferBuilder().contractSequenceId(-1);
}).throw('Invalid contract sequence id');
});

it('should fail with an invalid destination address', () => {
should(() => {
new TransferBuilder().to('invalidaddress');
}).throw('Invalid address');
});

it('should fail with an invalid amount: text value', () => {
should(() => {
new TransferBuilder().amount('invalidamount');
}).throw('Invalid amount');
});

it('should fail with an invalid amount: negative value', () => {
should(() => {
new TransferBuilder().amount('-10');
}).throw('Invalid amount');
});

it('should fail with an invalid expiration time', () => {
should(() => {
new TransferBuilder().expirationTime(-1);
}).throw('Invalid expiration time');
});

it('should fail if a sequenceId param is missing', () => {
const builder = new TransferBuilder().amount(amount).to(toAddress).key(key);
assert.throws(() => builder.signAndBuild());
});

it('should fail if a destination param is missing', () => {
const builder = new TransferBuilder().amount(amount).contractSequenceId(2).key(key);
assert.throws(() => builder.signAndBuild());
});

it('should fail if a amount param is missing', () => {
const builder = new TransferBuilder().to(toAddress).contractSequenceId(2).key(key);
assert.throws(() => builder.signAndBuild());
});
});
});
66 changes: 0 additions & 66 deletions modules/sdk-coin-eth/test/unit/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert';
import should from 'should';

import { KeyPair, TransferBuilder } from '../../src';
Expand Down Expand Up @@ -91,69 +90,4 @@ describe('Eth send multi sig builder', function () {
should.equal(result, testData.SEND_FUNDS_DATA);
});
});

describe('should fail', () => {
it('should fail if a coin does not exists in @bitgo/statics', () => {
should(() => {
new TransferBuilder().coin('inexistentcoin');
}).throw();
});

it('should fail with an invalid key', () => {
const builder = new TransferBuilder()
.expirationTime(1590078260)
.amount(amount)
.to(toAddress)
.contractSequenceId(2)
.key('invalidkey');
should(() => {
builder.signAndBuild();
}).throw('private key length is invalid');
});

it('should fail with an invalid sequence id', () => {
should(() => {
new TransferBuilder().contractSequenceId(-1);
}).throw('Invalid contract sequence id');
});

it('should fail with an invalid destination address', () => {
should(() => {
new TransferBuilder().to('invalidaddress');
}).throw('Invalid address');
});

it('should fail with an invalid amount: text value', () => {
should(() => {
new TransferBuilder().amount('invalidamount');
}).throw('Invalid amount');
});

it('should fail with an invalid amount: negative value', () => {
should(() => {
new TransferBuilder().amount('-10');
}).throw('Invalid amount');
});

it('should fail with an invalid expiration time', () => {
should(() => {
new TransferBuilder().expirationTime(-1);
}).throw('Invalid expiration time');
});

it('should fail if a sequenceId param is missing', () => {
const builder = new TransferBuilder().amount(amount).to(toAddress).key(key);
assert.throws(() => builder.signAndBuild());
});

it('should fail if a destination param is missing', () => {
const builder = new TransferBuilder().amount(amount).contractSequenceId(2).key(key);
assert.throws(() => builder.signAndBuild());
});

it('should fail if a amount param is missing', () => {
const builder = new TransferBuilder().to(toAddress).contractSequenceId(2).key(key);
assert.throws(() => builder.signAndBuild());
});
});
});
Loading

0 comments on commit a89a6d2

Please sign in to comment.