Class provide message sign utilities.
Kind: global class
- Message
- new Message(message)
- instance
- static
- .sign(privateKey, messageHash) ⇒
string
- .recover(signature, messageHash) ⇒
string
- .sign(privateKey, messageHash) ⇒
Param | Type |
---|---|
message | string |
Example
> msg = new Message('Hello World');
Message {
message: 'Hello World',
}
> msg.sign('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef');
Message {
message: 'Hello World',
signature: '0x6e913e2b76459f19ebd269b82b51a70e912e909b2f5c002312efc27bcc280f3c29134d382aad0dbd3f0ccc9f0eb8f1dbe3f90141d81574ebb6504156b0d7b95f01'
}
> msg.signature
"0x6e913e2b76459f19ebd269b82b51a70e912e909b2f5c002312efc27bcc280f3c29134d382aad0dbd3f0ccc9f0eb8f1dbe3f90141d81574ebb6504156b0d7b95f01"
> msg.hash
"0x592fa743889fc7f92ac2a37bb1f5ba1daf2a5c84741ca0e0061d243a2e6707ba"
> msg.from
"cfxtest:aasm4c231py7j34fghntcfkdt2nm9xv1tu6jd3r1s7"
> msg.r
"0x6e913e2b76459f19ebd269b82b51a70e912e909b2f5c002312efc27bcc280f3c"
> msg.s
"0x29134d382aad0dbd3f0ccc9f0eb8f1dbe3f90141d81574ebb6504156b0d7b95f"
> msg.v
1
Getter of message hash include signature.
Note: calculate every time.
Kind: instance property of Message
Getter of sender address.
Note: calculate every time.
Kind: instance property of Message
Returns: string
| undefined
- If ECDSA recover success return address, else return undefined.
Get signatures r
Kind: instance property of Message
Get signatures s
Kind: instance property of Message
Get signatures v
Kind: instance property of Message
message.sign(privateKey, networkId) ⇒ Message
Sign message and set 'r','s','v' and 'hash'.
Kind: instance method of Message
Param | Type | Description |
---|---|---|
privateKey | string |
Private key hex string. |
networkId | number |
Network id of account |
Signs the hash with the privateKey.
Kind: static method of Message
Returns: string
- The signature as hex string.
Param | Type | Description |
---|---|---|
privateKey | string | Buffer |
Private key used to sign message |
messageHash | string | Buffer |
The message hash need to be signed |
Example
> Message.sign(
'0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', // privateKey
'0x592fa743889fc7f92ac2a37bb1f5ba1daf2a5c84741ca0e0061d243a2e6707ba',
)
"0x6e913e2b76459f19ebd269b82b51a70e912e909b2f5c002312efc27bcc280f3c29134d382aad0dbd3f0ccc9f0eb8f1dbe3f90141d81574ebb6504156b0d7b95f01"
Recovers the signers publicKey from the signature.
Kind: static method of Message
Returns: string
- The publicKey as hex string.
Param | Type |
---|---|
signature | string | Buffer |
messageHash | string | Buffer |
Example
> Message.recover(
'0x6e913e2b76459f19ebd269b82b51a70e912e909b2f5c002312efc27bcc280f3c29134d382aad0dbd3f0ccc9f0eb8f1dbe3f90141d81574ebb6504156b0d7b95f01',
'0x592fa743889fc7f92ac2a37bb1f5ba1daf2a5c84741ca0e0061d243a2e6707ba',
)
"0x4646ae5047316b4230d0086c8acec687f00b1cd9d1dc634f6cb358ac0a9a8ffffe77b4dd0a4bfb95851f3b7355c781dd60f8418fc8a65d14907aff47c903a559"