Skip to content

Commit 69437cd

Browse files
authored
Merge pull request #39 from Ryun1/fix-signdata
2 parents 2a543e3 + 36e9a54 commit 69437cd

File tree

2 files changed

+78
-20
lines changed

2 files changed

+78
-20
lines changed

src/components/cards/signDataCard.js

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ import ApiCardWithModal from './apiCardWithModal'
33
import {Buffer} from 'buffer'
44
import {CommonStyles, ModalWindowContent} from '../ui-constants'
55
import {getBech32AddressFromHex} from '../../utils/cslTools'
6+
import SelectWithLabel from '../selectWithLabel'
67

78
const SignDataCard = ({api, onRawResponse, onResponse, onWaiting}) => {
89
const [message, setMessage] = useState('')
10+
const [address, setAddress] = useState('')
11+
const [encodingType, setEncodingType] = useState('hex')
912

1013
const getAddress = async () => {
14+
// If user provided an address, use it directly
15+
if (address.trim()) {
16+
return address.trim()
17+
}
18+
// Otherwise, use the reward address as default
1119
try {
1220
const rewardHexAddress = (await api?.getRewardAddresses())[0]
1321
return getBech32AddressFromHex(rewardHexAddress)
@@ -16,17 +24,24 @@ const SignDataCard = ({api, onRawResponse, onResponse, onWaiting}) => {
1624
}
1725
}
1826

19-
const getPayloadHex = (payload) => {
20-
if (payload.startsWith('0x')) {
21-
const reg = /^[0-9A-Fa-f]*$/g
22-
const remainingPart = payload.substring(2)
23-
if (reg.test(remainingPart)) {
24-
return Buffer.from(remainingPart, 'hex').toString('hex')
27+
const getPayloadHex = (payload, encoding) => {
28+
if (encoding === 'hex') {
29+
// Remove '0x' prefix if present
30+
const hexString = payload.startsWith('0x') ? payload.substring(2) : payload
31+
const reg = /^[0-9A-Fa-f]+$/i
32+
if (reg.test(hexString)) {
33+
// Validate hex string length is even (required for proper byte conversion)
34+
if (hexString.length % 2 !== 0) {
35+
throw new Error(`!!!ERROR!!!\nHex string must have even length: "${payload}"`)
36+
}
37+
// Return the hex string directly (without 0x prefix) as wallet expects hex string
38+
return hexString.toLowerCase()
2539
} else {
26-
throw new Error(`!!!ERROR!!!\nIt is not a suitable payload: "${payload}"`)
40+
throw new Error(`!!!ERROR!!!\nIt is not a valid hex string: "${payload}"`)
2741
}
2842
}
2943

44+
// Convert UTF-8 string to hex string
3045
return Buffer.from(payload, 'utf8').toString('hex')
3146
}
3247

@@ -35,9 +50,25 @@ const SignDataCard = ({api, onRawResponse, onResponse, onWaiting}) => {
3550

3651
try {
3752
const address = await getAddress()
38-
const payloadHex = getPayloadHex(message)
53+
const payloadHex = getPayloadHex(message, encodingType)
54+
55+
// Log the inputs for debugging
56+
console.log('SignData inputs:', { address, payloadHex, originalMessage: message, encodingType })
57+
3958
const signDataResponse = await api?.signData(address, payloadHex)
40-
onRawResponse('')
59+
60+
// Show raw response with inputs for debugging
61+
const debugInfo = {
62+
inputs: {
63+
address,
64+
payloadHex,
65+
originalMessage: message,
66+
encodingType
67+
},
68+
response: signDataResponse
69+
}
70+
const rawResponseString = JSON.stringify(debugInfo, null, 2)
71+
onRawResponse(rawResponseString)
4172
onResponse(signDataResponse)
4273
} catch (error) {
4374
onRawResponse('')
@@ -57,20 +88,46 @@ const SignDataCard = ({api, onRawResponse, onResponse, onWaiting}) => {
5788
clickFunction: signDataClick,
5889
}
5990

91+
const encodingOptions = [
92+
{label: 'UTF-8', value: 'utf8'},
93+
{label: 'Hex', value: 'hex'},
94+
]
95+
6096
return (
6197
<ApiCardWithModal {...apiProps}>
6298
<div className={ModalWindowContent.contentPadding}>
63-
<label htmlFor="signMessage" className={ModalWindowContent.contentLabelStyle}>
64-
Sign Data
65-
</label>
66-
<input
67-
type="text"
68-
id="signMessage"
69-
className={CommonStyles.inputStyles}
70-
placeholder=""
71-
value={message}
72-
onChange={(event) => setMessage(event.target.value)}
99+
<div>
100+
<label htmlFor="signAddress" className={ModalWindowContent.contentLabelStyle}>
101+
Address (optional - defaults to reward address)
102+
</label>
103+
<input
104+
type="text"
105+
id="signAddress"
106+
className={CommonStyles.inputStyles}
107+
placeholder=""
108+
value={address}
109+
onChange={(event) => setAddress(event.target.value)}
110+
/>
111+
</div>
112+
<SelectWithLabel
113+
selectName="Message Encoding"
114+
selectArray={encodingOptions}
115+
onChangeFunction={(event) => setEncodingType(event.target.value)}
116+
defaultValue={encodingType}
73117
/>
118+
<div className="mt-3">
119+
<label htmlFor="signMessage" className={ModalWindowContent.contentLabelStyle}>
120+
Message to sign
121+
</label>
122+
<input
123+
type="text"
124+
id="signMessage"
125+
className={CommonStyles.inputStyles}
126+
placeholder={encodingType === 'hex' ? 'e.g., 0x48656c6c6f or 48656c6c6f' : 'e.g., Hello'}
127+
value={message}
128+
onChange={(event) => setMessage(event.target.value)}
129+
/>
130+
</div>
74131
</div>
75132
</ApiCardWithModal>
76133
)

src/components/selectWithLabel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import {ModalWindowContent} from './ui-constants'
33

44
const SelectWithLabel = (props) => {
5-
const {selectName, selectArray, onChangeFunction} = props
5+
const {selectName, selectArray, onChangeFunction, defaultValue} = props
66
const selectID = selectName.split(' ').join('')
77

88
return (
@@ -15,6 +15,7 @@ const SelectWithLabel = (props) => {
1515
onChange={onChangeFunction}
1616
name={selectID}
1717
id={selectID}
18+
defaultValue={defaultValue}
1819
>
1920
{selectArray.map((arrayItem) => (
2021
<option className="border text-white" key={arrayItem.label + 'Key'} value={arrayItem.value}>

0 commit comments

Comments
 (0)