Skip to content

Commit c81d0b1

Browse files
fix: use better string conversion for message signing
It appears that `v instanceof Buffer` is not a reliable type guard in Typescript 5. The new type guard is equivalent. Issue: BTC-1450
1 parent 7e25872 commit c81d0b1

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

modules/abstract-eth/src/lib/transferBuilder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ export class TransferBuilder {
155155

156156
if (this._coinUsesNonPackedEncodingForTxData) {
157157
const types: string[] = operationData[0] as string[];
158-
const values: string[] = operationData[1].map((item) =>
159-
item instanceof Buffer ? '0x' + item.toString('hex') : item
158+
const values: (string | number)[] = operationData[1].map((item) =>
159+
typeof item === 'string' || typeof item === 'number' ? item : '0x' + item.toString('hex')
160160
);
161161
operationHash = keccak256(defaultAbiCoder.encode(types, values));
162162
} else {
@@ -166,7 +166,7 @@ export class TransferBuilder {
166166
return operationHash;
167167
}
168168

169-
protected getOperationData(): (string | Buffer)[][] {
169+
protected getOperationData(): (string | number | Buffer)[][] {
170170
let operationData;
171171
const prefix = this.getOperationHashPrefix();
172172
if (this._tokenContractAddress !== undefined) {

modules/sdk-coin-ada/src/ada.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export class Ada extends BaseCoin {
161161
/** @inheritDoc */
162162
async signMessage(key: KeyPair, message: string | Buffer): Promise<Buffer> {
163163
const adaKeypair = new AdaKeyPair({ prv: key.prv });
164-
const messageHex = message instanceof Buffer ? message.toString('hex') : message;
164+
const messageHex = typeof message === 'string' ? message : message.toString('hex');
165165

166166
return Buffer.from(adaKeypair.signMessage(messageHex));
167167
}

modules/sdk-coin-cspr/src/cspr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class Cspr extends BaseCoin {
247247
*/
248248
async signMessage(key: KeyPair, message: string | Buffer): Promise<Buffer> {
249249
const keyPair = new CsprLib.KeyPair({ prv: key.prv });
250-
const messageHex = message instanceof Buffer ? message.toString('hex') : message;
250+
const messageHex = typeof message === 'string' ? message : message.toString('hex');
251251
const signatureData = CsprLib.Utils.signMessage(keyPair, messageHex);
252252
return Buffer.from(signatureData.signature);
253253
}

modules/sdk-coin-polygon/test/unit/transactionBuilder/send.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Polygon transaction builder send', () => {
2323
];
2424
const types: string[] = operationParams[0] as string[];
2525
const values: (string | number)[] = operationParams[1].map((item) =>
26-
item instanceof Buffer ? '0x' + item.toString('hex') : item
26+
typeof item === 'string' || typeof item === 'number' ? item : '0x' + item.toString('hex')
2727
);
2828
return keccak256(defaultAbiCoder.encode(types, values));
2929
};

0 commit comments

Comments
 (0)