Skip to content

Commit

Permalink
fix: personal sign now allows strings for compatibility between walle…
Browse files Browse the repository at this point in the history
…ts (#173)
  • Loading branch information
menduz authored Aug 25, 2021
1 parent 5d68042 commit b060129
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/eth-connect.tostringdata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [eth-connect](./eth-connect.md) &gt; [toStringData](./eth-connect.tostringdata.md)

## toStringData() function

Converts a UTF8 string to it's hex representation as a 0x string. If the argument is already a 0xHEX prefixed string, the conversion is skipped.

<b>Signature:</b>

```typescript
export declare function toStringData(val: BigNumber.Value): string;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| val | [BigNumber.Value](./eth-connect.bignumber.value.md) | |

<b>Returns:</b>

string

1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
| [toJsonRpcRequest(method, params)](./eth-connect.tojsonrpcrequest.md) | Should be called to valid json create payload object |
| [toNullDecimal(value)](./eth-connect.tonulldecimal.md) | Converts value to it's decimal representation in string |
| [toString\_2(value)](./eth-connect.tostring_2.md) | Converts value to string |
| [toStringData(val)](./eth-connect.tostringdata.md) | Converts a UTF8 string to it's hex representation as a 0x string. If the argument is already a 0xHEX prefixed string, the conversion is skipped. |
| [toTwosComplement(num, bits)](./eth-connect.totwoscomplement.md) | Takes and input transforms it into bignumber and if it is negative value, into two's complement |
| [toWei(num, unit)](./eth-connect.towei.md) | Takes a number of a unit and converts it to wei.<!-- -->Possible units are: SI Short SI Full Effigy Other - kwei femtoether babbage - mwei picoether lovelace - gwei nanoether shannon nano - -- microether szabo micro - -- milliether finney milli - ether -- -- - kether -- grand - mether - gether - tether |
| [transformToFullName(json)](./eth-connect.transformtofullname.md) | Should be used to create full function/event name from json abi |
Expand Down
3 changes: 3 additions & 0 deletions report/eth-connect.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,9 @@ function toString_2(value: BigNumber.Value): string;

export { toString_2 as toString }

// @public
export function toStringData(val: BigNumber.Value): string;

// @public
export function toTwosComplement(num: BigNumber.Value, bits?: number): BigNumber;

Expand Down
4 changes: 2 additions & 2 deletions src/methods/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ export namespace eth {
export const personal_sign = new Method({
callName: 'personal_sign',
params: 3,
inputFormatter: [null, formatters.inputAddressFormatter, null],
inputFormatter: [utils.toStringData, formatters.inputAddressFormatter, null],
outputFormatter: utils.toData
})

export const personal_ecRecover = new Method({
callName: 'personal_ecRecover',
params: 2,
inputFormatter: [null /* message */, null /* signature */],
inputFormatter: [utils.toStringData /* message */, null /* signature */],
outputFormatter: utils.toAddress
})

Expand Down
18 changes: 18 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,24 @@ export function toData(val: BigNumber.Value) {
return toHex(val)
}

/**
* @public
* Converts a UTF8 string to it's hex representation as a 0x string.
* If the argument is already a 0xHEX prefixed string, the conversion is skipped.
*/
export function toStringData(val: BigNumber.Value) {
if (typeof val === 'string') {
if (val.startsWith('0x') && /^[A-Za-z0-9]+$/.test(val)) {
return toHex(val)
}
return '0x' + bytesToHex(stringToUtf8Bytes(val))
}
if (val instanceof Uint8Array) {
return '0x' + bytesToHex(val)
}
throw new Error(`toStringData: Error trying to convert ${val} (${typeof val}) to a hex string.`)
}

/**
* @public
* Converts value to it's boolean representation (x != 0)
Expand Down
25 changes: 25 additions & 0 deletions test/integration.personal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@ function doTest(requestManager: RequestManager) {
}
})

it('should sign a string message (geth only)', async () => {
if (requestManager.provider instanceof WebSocketProvider /* test in geth node only */) {
const message = 'TEST MESSAGE'
await requestManager.personal_sign(message, account, 'test')
}
})

it('should sign a string message (geth only) and recover the signer address', async () => {
if (requestManager.provider instanceof WebSocketProvider /* test in geth node only */) {
const message = 'TEST MESSAGE'
const signature = await requestManager.personal_sign(message, account, 'test')
const signerAddress = await requestManager.personal_ecRecover(message, signature)
expect(signerAddress).toEqual(account)
}
})

it('should sign a Uint8Array message (geth only) and recover the signer address', async () => {
if (requestManager.provider instanceof WebSocketProvider /* test in geth node only */) {
const message = new Uint8Array([14, 15, 99]) as any
const signature = await requestManager.personal_sign(message, account, 'test')
const signerAddress = await requestManager.personal_ecRecover(message, signature)
expect(signerAddress).toEqual(account)
}
})

it('should unlock the account', async () => {
const unlocked = await requestManager.personal_unlockAccount(account, 'test')
expect(unlocked).toEqual(true) // 'must unlock'
Expand Down

0 comments on commit b060129

Please sign in to comment.