diff --git a/package.json b/package.json index 3434149f..dc66f429 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "ripemd160": "^2.0.1", "rlp": "^2.1.0", "rpc-websockets": "^4.1.1", - "tronweb": "^2.5.3", "tslib": "^1.9.3", "tweetnacl": "^1.0.0", "web3": "1.0.0-beta.33" diff --git a/src/contracts/index.ts b/src/contracts/index.ts index 466fa6f5..f55113ea 100644 --- a/src/contracts/index.ts +++ b/src/contracts/index.ts @@ -6,6 +6,5 @@ export { DPOS3 } from './dpos3' export { AddressMapper } from './address-mapper' export { TransferGateway } from './transfer-gateway' export { LoomCoinTransferGateway } from './loomcoin-gateway' -export { TronTransferGateway } from './tron-gateway' export { BinanceTransferGateway } from './binance-gateway' export { UserDeployerWhitelist } from './user-deployer-whitelist' diff --git a/src/contracts/tron-gateway.ts b/src/contracts/tron-gateway.ts deleted file mode 100644 index f75f8bb6..00000000 --- a/src/contracts/tron-gateway.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { TransferGateway } from './transfer-gateway' -import { Client } from '../client' -import { Address } from '../address' - -export class TronTransferGateway extends TransferGateway { - static async createAsync(client: Client, callerAddr: Address): Promise { - const contractAddr = await client.getContractAddressAsync('tron-gateway') - if (!contractAddr) { - throw Error('Failed to resolve contract address for Tron TransferGateway') - } - - return new TronTransferGateway({ contractAddr, callerAddr, client }) - } -} diff --git a/src/index.ts b/src/index.ts index ee2377bf..4aede8f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,6 @@ export { unmarshalBigUIntPB, marshalBigUIntPB } from './big-uint' export { SignedTxMiddleware, SignedEthTxMiddleware, - SignedTronTxMiddleware, SignedBinanceTxMiddleware, NonceTxMiddleware, CachedNonceTxMiddleware, @@ -48,6 +47,4 @@ export { DPOSUser } from './dpos-user' export { DPOSUserV3 } from './dpos-user-v3' export { GatewayVersion, GatewayUser } from './gateway-user' export { CrossChainUser } from './crosschain-user' - -export { TronWebSigner } from './tron-web-signer' export { BinanceSigner } from './binance-signer' diff --git a/src/middleware/index.ts b/src/middleware/index.ts index 3a962f7e..4f435dfb 100644 --- a/src/middleware/index.ts +++ b/src/middleware/index.ts @@ -3,5 +3,4 @@ export { NonceTxMiddleware, isInvalidTxNonceError } from './nonce-tx-middleware' export { CachedNonceTxMiddleware } from './cached-nonce-tx-middleware' export { SpeculativeNonceTxMiddleware } from './speculative-nonce-tx-middleware' export { SignedEthTxMiddleware } from './signed-eth-tx-middleware' -export { SignedTronTxMiddleware } from './signed-tron-tx-middleware' export { SignedBinanceTxMiddleware } from './signed-binance-tx-middleware' diff --git a/src/middleware/signed-tron-tx-middleware.ts b/src/middleware/signed-tron-tx-middleware.ts deleted file mode 100644 index 6ee07099..00000000 --- a/src/middleware/signed-tron-tx-middleware.ts +++ /dev/null @@ -1,49 +0,0 @@ -import debug from 'debug' -import { TronWebSigner } from '../tron-web-signer' - -import { SignedTx } from '../proto/loom_pb' -import { ITxMiddlewareHandler } from '../client' -import { soliditySha3 } from '../solidity-helpers' -import { bytesToHex } from '../crypto-utils' - -const log = debug('signed-tron-tx-middleware') - -/** - * Signs transactions using a TRON compatible (secp256k1) private key. - */ -export class SignedTronTxMiddleware implements ITxMiddlewareHandler { - signer: TronWebSigner - signerAddress?: string - - /** - * @param signer tronweb signer to use for signing txs. - */ - constructor(signer: TronWebSigner) { - this.signer = signer - } - - async Handle(txData: Readonly): Promise { - if (!this.signerAddress) { - // Getting the public key address - this.signerAddress = await this.signer.getAddress() - } - - // Get hash to be signed - const hash = soliditySha3({ - type: 'bytes', - value: bytesToHex(txData) - }) - - // Signing the transaction - const sig = await this.signer.signAsync(hash) - - log(`signer: ${this.signerAddress}, signature: 0x${bytesToHex(sig.slice(1))}`) - - const signedTx = new SignedTx() - signedTx.setInner(txData as Uint8Array) - signedTx.setSignature(sig) - // NOTE: don't need to send secp256k1 public key sig since it can be recovered from hash + sig - - return signedTx.serializeBinary() - } -} diff --git a/src/tests/e2e/tron-test-tx-middleware.ts b/src/tests/e2e/tron-test-tx-middleware.ts deleted file mode 100644 index 751d06d2..00000000 --- a/src/tests/e2e/tron-test-tx-middleware.ts +++ /dev/null @@ -1,186 +0,0 @@ -import test from 'tape' - -import { CachedNonceTxMiddleware, SignedTronTxMiddleware, CryptoUtils, Client } from '../../index' -import { LoomProvider } from '../../loom-provider' -import { deployContract } from '../evm-helpers' -import { Address, LocalAddress } from '../../address' -import { createDefaultTxMiddleware } from '../../helpers' -import { TronWebSigner } from '../../tron-web-signer' -import { createTestHttpClient } from '../helpers' -import { AddressMapper } from '../../contracts' - -const Web3 = require('web3') -const TronWeb = require('tronweb') - -async function bootstrapTest( - createClient: () => Client -): Promise<{ - client: Client - pubKey: Uint8Array - privKey: Uint8Array - signer: TronWebSigner - loomProvider: LoomProvider - contract: any - ABI: any[] - account: Address -}> { - // Create the client - const privKey = CryptoUtils.B64ToUint8Array( - '5IKwJ+M1zSrqF/eJ99OeEWGRQH1ND0GURLu8ukm8yx0QmNQm5rOoPUpBLhiR327Ac3IpLts9+ZKAzdRykkMmwg==' - ) - const pubKey = CryptoUtils.publicKeyFromPrivateKey(privKey) - const client = createClient() - client.on('error', err => console.error(err)) - client.txMiddleware = createDefaultTxMiddleware(client, privKey) - - const account = new Address(client.chainId, LocalAddress.fromPublicKey(pubKey)) - - // Create LoomProvider instance - const loomProvider = new LoomProvider(client, privKey) - - // Contract data and ABI - const contractData = - '608060405234801561001057600080fd5b50600a60008190555061014e806100286000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b1146100515780636d4ce63c1461007e575b600080fd5b34801561005d57600080fd5b5061007c600480360381019080803590602001909291905050506100a9565b005b34801561008a57600080fd5b50610093610119565b6040518082815260200191505060405180910390f35b806000819055506000547f7e0b7a35f017ec94e71d7012fe8fa8011f1dab6090674f92de08f8092ab30dda33604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390a250565b600080549050905600a165627a7a7230582041f33d6a8b78928e192affcb980ca6bef9b6f5b7da5aa4b2d75b1208720caeeb0029' - - const ABI = [ - { - constant: false, - inputs: [ - { - name: '_value', - type: 'uint256' - } - ], - name: 'set', - outputs: [], - payable: false, - stateMutability: 'nonpayable', - type: 'function' - }, - { - constant: true, - inputs: [], - name: 'get', - outputs: [ - { - name: '', - type: 'uint256' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - }, - { - inputs: [], - payable: false, - stateMutability: 'nonpayable', - type: 'constructor' - }, - { - anonymous: false, - inputs: [ - { - indexed: true, - name: '_value', - type: 'uint256' - }, - { - indexed: false, - name: 'sender', - type: 'address' - } - ], - name: 'NewValueSet', - type: 'event' - } - ] - - // Deploy the contract using loom provider - const result = await deployContract(loomProvider, contractData) - - // Instantiate Contract using web3 - const web3 = new Web3(loomProvider) - const contract = new web3.eth.Contract(ABI, result.contractAddress, { - from: LocalAddress.fromPublicKey(pubKey).toString() - }) - - // And get the tron signer - const fullNode = new TronWeb.providers.HttpProvider('http://127.0.0.1:9090') - const solidityNode = new TronWeb.providers.HttpProvider('http://127.0.0.1:9090') - const eventServer = 'http://127.0.0.1:9090' - const privateKey = '11b5b4c7ff9e69e89e66ef614d8d63a81f2799c3b2bad938a6ae3b0a10a12772' - const tronWeb = new TronWeb(fullNode, solidityNode, eventServer, privateKey) - const tronDefaultAddr = tronWeb.defaultAddress.base58 - let trxAddress = tronWeb.address.toHex(tronDefaultAddr) - let trxAddressHex = '0x' + trxAddress.substring(2, 100) - - const signer = new TronWebSigner(tronWeb, trxAddressHex) - - return { client, pubKey, privKey, signer, loomProvider, contract, ABI, account } -} - -test('Test Signed Tron Tx Middleware Type 2', async t => { - try { - const { client, signer, pubKey, loomProvider, contract, account } = await bootstrapTest( - createTestHttpClient - ) - - const addressMapper = await AddressMapper.createAsync( - client, - new Address(client.chainId, LocalAddress.fromPublicKey(pubKey)) - ) - - // Set the mapping - const trxAddress = await signer.getAddress() - const from = new Address(client.chainId, LocalAddress.fromPublicKey(pubKey)) - const to = new Address('tron', LocalAddress.fromHexString(trxAddress)) - - // Add mapping if not added yet - if (!(await addressMapper.hasMappingAsync(from))) { - await addressMapper.addIdentityMappingAsync(from, to, signer) - } - - try { - const addressMapped = await addressMapper.getMappingAsync(from) - t.assert(addressMapped.from.equals(from), 'Address mapper should map dappchain address') - t.assert(addressMapped.to.equals(to), 'Address mapper should map tron address') - } catch (err) { - t.error(err) - } - - const callerChainId = 'tron' - // Override the default caller chain ID - loomProvider.callerChainId = callerChainId - // Tron account needs its own middleware - loomProvider.setMiddlewaresForAddress(to.local.toString(), [ - new CachedNonceTxMiddleware(account, client), - new SignedTronTxMiddleware(signer) - ]) - - const middlewaresUsed = loomProvider.accountMiddlewares.get(trxAddress.toLowerCase()) - t.assert( - middlewaresUsed![0] instanceof CachedNonceTxMiddleware, - 'CachedNonceTxMiddleware used' - ) - t.assert(middlewaresUsed![1] instanceof SignedTronTxMiddleware, 'SignedTronTxMiddleware used') - - let tx = await contract.methods.set(1).send({ from: to.local.toString() }) - t.equal( - tx.status, - '0x1', - `SimpleStore.set should return correct status for address (to) ${to.local.toString()}` - ) - - t.equal( - tx.events.NewValueSet.returnValues.sender.toLowerCase(), - from.local.toString(), - `Should be the same sender from loomchain ${from.local.toString()}` - ) - } catch (err) { - console.error(err) - t.fail(err.message) - } - - t.end() -}) diff --git a/src/tests/e2e_tests.ts b/src/tests/e2e_tests.ts index f87b081a..7b9e31c0 100644 --- a/src/tests/e2e_tests.ts +++ b/src/tests/e2e_tests.ts @@ -19,7 +19,6 @@ import './e2e/client-evm-event-tests-2' // Middlewares import './e2e/client-test-tx-cache' import './e2e/client-test-tx-middleware' -import './e2e/tron-test-tx-middleware' import './e2e/binance-test-tx-middleware' // Events diff --git a/src/tron-web-signer.ts b/src/tron-web-signer.ts deleted file mode 100644 index 028ff7d9..00000000 --- a/src/tron-web-signer.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { SIGNATURE_TYPE } from './crypto-utils' -import ethutil from 'ethereumjs-util' -import TronWeb from 'tronweb' -import { IEthereumSigner } from './solidity-helpers' - -/** - * Signs message using a TronWeb account. - * This signer should be used for interactive signing in the browser with TronLink. - */ -export class TronWebSigner implements IEthereumSigner { - private _tronWeb: TronWeb - private _address: string - - /** - * @param tronweb tronweb instance to use for signing. - * @param accountAddress Address of tronweb account to sign with. - */ - constructor(tronWeb: TronWeb, accountAddress: string) { - this._tronWeb = tronWeb - this._address = accountAddress - } - - /** - * Signs a message. - * @param msg Message to sign. - * @returns Promise that will be resolved with the signature bytes. - */ - async signAsync(msg: string): Promise { - const signature = await this._tronWeb.trx.sign(msg) - const sig = signature.slice(2) - - const r = ethutil.toBuffer('0x' + sig.substring(0, 64)) as Buffer - const s = ethutil.toBuffer('0x' + sig.substring(64, 128)) as Buffer - let v = parseInt(sig.substring(128, 130), 16) - - if (v === 0 || v === 1) { - v += 27 - } - - return Buffer.concat([ - ethutil.toBuffer(SIGNATURE_TYPE.TRON) as Buffer, - r, - s, - ethutil.toBuffer(v) as Buffer - ]) - } - - /** - * Returns signer address - */ - async getAddress(): Promise { - return this._address - } -} diff --git a/src/types/tronweb.d.ts b/src/types/tronweb.d.ts deleted file mode 100644 index 85c7ff4f..00000000 --- a/src/types/tronweb.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -declare module 'tronweb' { - export class TronWeb { - constructor(e: any, r: any, ...args: any[]) - getEventResult(contractAddress: string, contractName: string) - setAddress(publicAddress: string) - setPrivateKey(privatekey: string) - sha3(msg: string) - trx: { - sign(transaction: string, ...args: any[]) - signMessage(...args: any[]) - } - } - - export default TronWeb -}