Skip to content

Commit

Permalink
Support blob txs by upgrading to latest dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkvi committed May 10, 2024
1 parent 471fd93 commit 9560e47
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 52 deletions.
74 changes: 43 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"@ethereumjs/common": "^3.2.0",
"@ethereumjs/tx": "^4.2.0",
"@ethereumjs/util": "^8.1.0",
"@ethereumjs/common": "^4.3.0",
"@ethereumjs/tx": "^5.3.0",
"@ethereumjs/util": "^9.0.3",
"@google-cloud/kms": "^4.2.0",
"asn1js": "^3.0.5",
"axios": "^1.6.8",
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function handleEthSignTransaction(transactionArgs: TransactionArgs) {

const start = async () => {
try {
await app.listen({host:"0.0.0.0", port: 8000});
await app.listen({port: 8000});
console.log('Server running at https://localhost:8000/');
} catch (err) {
console.error(err);
Expand Down
23 changes: 11 additions & 12 deletions src/web3-kms-signer/core/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import {
addHexPrefix,
fromSigned,
toUnsigned,
bigIntToBuffer,
hashPersonalMessage,
bufferToBigInt, toBuffer
bigIntToBytes
} from '@ethereumjs/util'
import { TxData } from '@ethereumjs/tx';
import { TypedTxData } from '@ethereumjs/tx';
import { Common } from '@ethereumjs/common'
import { Wallets } from './Wallets';
import { UBuffer } from './Utils/UBuffer';
Expand Down Expand Up @@ -43,13 +42,13 @@ export class Signer {
* @param txData The transaction data to sign.
* @returns A Promise that resolves to the serialized transaction as a '0x'-prefixed hex string.
*/
public async signTransaction(account: { keyId: string, address?: Buffer }, txData: TxData) {
public async signTransaction(account: { keyId: string, address?: Buffer }, txData: TypedTxData) {
let tx = TransactionFactory.fromTxData(txData, { common: this.common });
const digest = tx.getMessageToSign()
const txType = Number(bufferToBigInt(toBuffer(txData.type)))
const {r, s, v} = await this.wallets.ecsign(account, digest, txType, this.common?.chainId());
const digest = Buffer.from(tx.getHashedMessageToSign())

const {r, s, v} = await this.wallets.ecsign(account, digest, tx.type, this.common?.chainId());
const signed = TransactionFactory.fromTxData({...txData, r, s, v}, { common: this.common });
return addHexPrefix(signed.serialize().toString('hex'));
return addHexPrefix(Buffer.from(signed.serialize()).toString('hex'));
}

/**
Expand All @@ -61,7 +60,7 @@ export class Signer {
*/
public async signMessage(account: { keyId: string, address?: Buffer }, message: string) {
const digest = hashPersonalMessage(Buffer.from(message));
return this.signDigest(account, digest);
return this.signDigest(account, Buffer.from(digest));
}

/**
Expand All @@ -73,9 +72,9 @@ export class Signer {
public async signDigest(account: { keyId: string, address?: Buffer }, digest: string | Buffer) {
const {r, s, v} = await this.wallets.ecsign(account, UBuffer.bufferOrHex(digest));

const rStr = toUnsigned(fromSigned(r)).toString('hex');
const sStr = toUnsigned(fromSigned(s)).toString('hex');
const vStr = bigIntToBuffer(v).toString('hex');
const rStr = Buffer.from(toUnsigned(fromSigned(r))).toString('hex');
const sStr = Buffer.from(toUnsigned(fromSigned(s))).toString('hex');
const vStr = Buffer.from(bigIntToBytes(v)).toString('hex');

return addHexPrefix(rStr.concat(sStr, vStr));
}
Expand Down
4 changes: 2 additions & 2 deletions src/web3-kms-signer/core/Utils/UAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class UAddress {
* Returns the wallet's address as a "0x" prefixed hex
*/
public getAddressHex(): string {
return ethutil.bufferToHex(this.getAddress()).toLowerCase();
return UBuffer.bufferToHex(this.getAddress()).toLowerCase();
}

/**
Expand All @@ -35,7 +35,7 @@ export class UAddress {
*/

public static fromPublickey(input: Buffer | string) {
return new UAddress(ethutil.publicToAddress(UBuffer.bufferOrHex(input)));
return new UAddress(Buffer.from(ethutil.publicToAddress(UBuffer.bufferOrHex(input))));
}

public static fromAddress(input: Buffer | string) {
Expand Down
4 changes: 4 additions & 0 deletions src/web3-kms-signer/core/Utils/UBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ export class UBuffer {
public static bufferOrHex(input: Buffer | string) {
return (input instanceof Buffer) ? input : Buffer.from(input.replace('0x', ''), 'hex');
}

public static bufferToHex = function (buf: Buffer): string {
return '0x' + buf.toString('hex')
}
}
6 changes: 3 additions & 3 deletions src/web3-kms-signer/core/Utils/UPublickey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class UPublickey {
* Returns the wallet's public key as a "0x" prefixed hex
*/
public getPublickeyHex(): string {
return ethutil.bufferToHex(this.getPublickey()).toLowerCase();
return UBuffer.bufferToHex(this.getPublickey()).toLowerCase();
}

/**
Expand Down Expand Up @@ -54,14 +54,14 @@ export class UPublickey {
*/

public static fromPrivatekey(input: Buffer | string) {
return new UPublickey(ethutil.privateToPublic(UBuffer.bufferOrHex(input)));
return new UPublickey(Buffer.from(ethutil.privateToPublic(UBuffer.bufferOrHex(input))));
}

public static fromPublickey(input: Buffer | string) {
return new UPublickey(UBuffer.bufferOrHex(input));
}

public static fromVRS(digest: Buffer, v: bigint, r: Buffer, s: Buffer, chainId?: bigint) {
return new UPublickey(ethutil.ecrecover(digest, v, r, s, chainId));
return new UPublickey(Buffer.from(ethutil.ecrecover(digest, v, r, s, chainId)));
}
}

0 comments on commit 9560e47

Please sign in to comment.