Skip to content

Latest commit

 

History

History
141 lines (110 loc) · 4.71 KB

Message.md

File metadata and controls

141 lines (110 loc) · 4.71 KB

Message

Class provide message sign utilities.

Kind: global class

new Message(message)

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

message.hash ⇒ string

Getter of message hash include signature.

Note: calculate every time.

Kind: instance property of Message

message.from ⇒ string | undefined

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.

message.r ⇒ string

Get signatures r

Kind: instance property of Message

message.s ⇒ string

Get signatures s

Kind: instance property of Message

message.v ⇒ number

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

Message.sign(privateKey, messageHash) ⇒ string

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"

Message.recover(signature, messageHash) ⇒ string

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"