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 Dec 29, 2023
1 parent fbde1c3 commit f21c4d0
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 50 deletions.
22 changes: 20 additions & 2 deletions modules/abstract-eth/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class TransferBuilder {
operationData = [
['string', 'address', 'uint', 'address', 'uint', 'uint'],
[
this._tokenOperationHashPrefix,
this._tokenOperationHashPrefix ?? this.getTokenOperationHashPrefix(),
new BN(ethUtil.stripHexPrefix(this._toAddress), 16),
this._amount,
new BN(ethUtil.stripHexPrefix(this._tokenContractAddress), 16),
Expand All @@ -159,7 +159,7 @@ export class TransferBuilder {
operationData = [
['string', 'address', 'uint', 'bytes', 'uint', 'uint'],
[
this._nativeCoinOperationHashPrefix,
this._nativeCoinOperationHashPrefix ?? this.getNativeOperationHashPrefix(),
new BN(ethUtil.stripHexPrefix(this._toAddress), 16),
this._amount,
Buffer.from(ethUtil.padToEven(ethUtil.stripHexPrefix(this._data)) || '', 'hex'),
Expand All @@ -171,6 +171,24 @@ export class TransferBuilder {
return operationData;
}

/**
* Get the prefix used in generating an operation hash for sending tokens
*
* @returns the string prefix
*/
protected getTokenOperationHashPrefix(): string {
return 'ERC20';
}

/**
* Get the prefix used in generating an operation hash for sending native coins
*
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'ETHER';
}

/** Return an expiration time, in seconds, set to one hour from now
*
* @returns {number} expiration time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ export abstract class BaseNFTTransferBuilder {
return this._signature;
}

/**
* Get the prefix used in generating an operation hash for sending native coins
*
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'ETHER';
}

/**
* Obtains the proper operation hash to sign either a sendMultiSig data
* or a sendMultiSigToken data
Expand Down
25 changes: 24 additions & 1 deletion modules/sdk-coin-celo/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
export { TransferBuilder } from '@bitgo/sdk-coin-eth';
import { TransferBuilder as EthTransferBuilder } from '@bitgo/sdk-coin-eth';

/** CELO transfer builder */
export class TransferBuilder extends EthTransferBuilder {
/**
* Get the prefix used in generating an operation hash for sending tokens
* See https://github.com/BitGo/eth-multisig-v2/blob/master/contracts/coins/CeloWalletSimple.sol
*
* @returns the string prefix
*/
protected getTokenOperationHashPrefix(): string {
return 'CELO-ERC20';
}

/**
* Get the prefix used in generating an operation hash for sending native coins
* See https://github.com/BitGo/eth-multisig-v2/blob/master/contracts/coins/CeloWalletSimple.sol
*
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'CELO';
}
}
25 changes: 24 additions & 1 deletion modules/sdk-coin-etc/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
export { TransferBuilder } from '@bitgo/abstract-eth';
import { TransferBuilder as EthTransferBuilder } from '@bitgo/sdk-coin-eth';

/** ETC transfer builder */
export class TransferBuilder extends EthTransferBuilder {
/**
* Get the prefix used in generating an operation hash for sending tokens
* See https://github.com/BitGo/eth-multisig-v2/blob/master/contracts/coins/EtcWalletSimple.sol
*
* @returns the string prefix
*/
protected getTokenOperationHashPrefix(): string {
return 'ETC-ERC20';
}

/**
* Get the prefix used in generating an operation hash for sending native coins
* See https://github.com/BitGo/eth-multisig-v2/blob/master/contracts/coins/EtcWalletSimple.sol
*
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'ETC';
}
}
19 changes: 2 additions & 17 deletions modules/sdk-coin-etc/test/unit/transactionBuilder/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('Etc send transaction', function () {
const sequenceId = 5;
txBuilder
.transfer()
.coin('tetc')
.amount(amount)
.to(recipient)
.expirationTime(1590066728)
Expand Down Expand Up @@ -62,28 +61,14 @@ describe('Etc send transaction', function () {

it('a send funds with amount 0 transaction', async () => {
initTxBuilder();
txBuilder
.transfer()
.coin('tetc')
.amount('0')
.to(testData.ACCOUNT_2)
.expirationTime(1590066728)
.contractSequenceId(5)
.key(key);
txBuilder.transfer().amount('0').to(testData.ACCOUNT_2).expirationTime(1590066728).contractSequenceId(5).key(key);
txBuilder.sign({ key: testData.PRIVATE_KEY_1 });
const tx = await txBuilder.build();
should.equal(tx.toBroadcastFormat(), testData.SEND_TX_AMOUNT_ZERO_BROADCAST);
});
it('unsigned transaction with final v check', async () => {
initTxBuilder();
txBuilder
.transfer()
.coin('tetc')
.amount('0')
.to(testData.ACCOUNT_2)
.expirationTime(1590066728)
.contractSequenceId(5)
.key(key);
txBuilder.transfer().amount('0').to(testData.ACCOUNT_2).expirationTime(1590066728).contractSequenceId(5).key(key);
const tx = await txBuilder.build();
should.equal(tx.toJson().v, '0xa1');
});
Expand Down
23 changes: 22 additions & 1 deletion modules/sdk-coin-eth/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
export { TransferBuilder } from '@bitgo/abstract-eth';
import { TransferBuilder as EthTransferBuilder } from '@bitgo/abstract-eth';

/** ETH transfer builder */
export class TransferBuilder extends EthTransferBuilder {
/**
* Get the prefix used in generating an operation hash for sending tokens
*
* @returns the string prefix
*/
protected getTokenOperationHashPrefix(): string {
return 'ERC20';
}

/**
* Get the prefix used in generating an operation hash for sending native coins
*
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'ETHER';
}
}
25 changes: 24 additions & 1 deletion modules/sdk-coin-polygon/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
export { TransferBuilder } from '@bitgo/abstract-eth';
import { TransferBuilder as EthTransferBuilder } from '@bitgo/abstract-eth';

/** POLYGON transfer builder */
export class TransferBuilder extends EthTransferBuilder {
/**
* Get the prefix used in generating an operation hash for sending tokens
* See https://github.com/BitGo/eth-multisig-v4/blob/master/contracts/coins/PolygonWalletSimple.sol
*
* @returns the string prefix
*/
protected getTokenOperationHashPrefix(): string {
return 'POLYGON-ERC20';
}

/**
* Get the prefix used in generating an operation hash for sending native coins
* See https://github.com/BitGo/eth-multisig-v4/blob/master/contracts/coins/PolygonWalletSimple.sol
*
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'POLYGON';
}
}
4 changes: 2 additions & 2 deletions modules/sdk-coin-polygon/test/unit/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('Polygon', function () {
builder.type(TransactionType.Send);
builder.contract(account_1.address);
const transferBuilder = builder.transfer() as TransferBuilder;
transferBuilder.coin('tpolygon').amount('1').to(account_2.address).expirationTime(10000).contractSequenceId(1);
transferBuilder.amount('1').to(account_2.address).expirationTime(10000).contractSequenceId(1);

const unsignedTx = await builder.build();
const unsignedTxForBroadcasting = unsignedTx.toBroadcastFormat();
Expand Down Expand Up @@ -196,7 +196,7 @@ describe('Polygon', function () {
builder.type(TransactionType.Send);
builder.contract(account_1.address);
const transferBuilder = builder.transfer() as TransferBuilder;
transferBuilder.coin('tpolygon').amount('1').to(account_2.address).expirationTime(10000).contractSequenceId(1);
transferBuilder.amount('1').to(account_2.address).expirationTime(10000).contractSequenceId(1);

const unsignedTx = await builder.build();
const unsignedTxForBroadcasting = unsignedTx.toBroadcastFormat();
Expand Down
25 changes: 24 additions & 1 deletion modules/sdk-coin-rbtc/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
export { TransferBuilder } from '@bitgo/sdk-coin-eth';
import { TransferBuilder as EthTransferBuilder } from '@bitgo/sdk-coin-eth';

/** RBTC transfer builder */
export class TransferBuilder extends EthTransferBuilder {
/**
* Get the prefix used in generating an operation hash for sending tokens
* See https://github.com/BitGo/eth-multisig-v2/blob/master/contracts/coins/RskWalletSimple.sol
*
* @returns the string prefix
*/
protected getTokenOperationHashPrefix(): string {
return 'RSK-ERC20';
}

/**
* Get the prefix used in generating an operation hash for sending native coins
* See https://github.com/BitGo/eth-multisig-v2/blob/master/contracts/coins/RskWalletSimple.sol
*
* @returns the string prefix
*/
protected getNativeOperationHashPrefix(): string {
return 'RSK';
}
}
27 changes: 3 additions & 24 deletions modules/sdk-coin-rbtc/test/unit/transactionBuilder/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ describe('Rbtc send transaction', function () {
initTxBuilder();
const recipient = testData.ACCOUNT_2;
const amount = '1000000000';
txBuilder
.transfer()
.coin('trbtc')
.amount(amount)
.to(recipient)
.expirationTime(1590066728)
.contractSequenceId(5)
.key(key);
txBuilder.transfer().amount(amount).to(recipient).expirationTime(1590066728).contractSequenceId(5).key(key);
txBuilder.sign({ key: testData.PRIVATE_KEY_1 });
const tx = await txBuilder.build();
should.equal(tx.toJson().chainId, 31);
Expand All @@ -70,28 +63,14 @@ describe('Rbtc send transaction', function () {
initTxBuilder();
const recipient = testData.ACCOUNT_2;
const amount = '1000000000';
txBuilder
.transfer()
.coin('trbtc')
.amount(amount)
.to(recipient)
.expirationTime(1590066728)
.contractSequenceId(5)
.key(key);
txBuilder.transfer().amount(amount).to(recipient).expirationTime(1590066728).contractSequenceId(5).key(key);
const tx = await txBuilder.build();
should.equal(tx.toJson().v, '0x61');
});

it('a send funds with amount 0 transaction', async () => {
initTxBuilder();
txBuilder
.transfer()
.coin('trbtc')
.amount('0')
.to(testData.ACCOUNT_2)
.expirationTime(1590066728)
.contractSequenceId(5)
.key(key);
txBuilder.transfer().amount('0').to(testData.ACCOUNT_2).expirationTime(1590066728).contractSequenceId(5).key(key);
txBuilder.sign({ key: testData.PRIVATE_KEY_1 });
const tx = await txBuilder.build();
should.equal(tx.toBroadcastFormat(), testData.SEND_TX_AMOUNT_ZERO_BROADCAST);
Expand Down

0 comments on commit f21c4d0

Please sign in to comment.