-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.ts
91 lines (83 loc) · 2.93 KB
/
crypto.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as Account from 'eth-lib/lib/account'
import * as StellarBase from 'stellar-base'
import * as Hash from 'eth-lib/lib/hash'
import * as web3Utils from 'web3-utils'
const sha256 = require('sha256')
import { toEthHex, cleanEthHex, hexToUint8Array, debugLog } from './utils'
import * as assert from 'assert'
import { Bytes64 } from './runtimeTypes'
const Web3 = require('web3')
/**
* Like web3.eth.accounts.hashMessage without the envelope.
*
* @param {*} data
* A message to hash - if it is hex it'll be UTF8 decoded.
*
* @returns {*}
* The hashed message (using keccak256)
*/
export const hashMsgRaw = (data: string | number[]): string => {
const msg = web3Utils.isHexStrict(data) ? web3Utils.hexToBytes(data) : data
const msgBuffer = Buffer.from(msg)
return Hash.keccak256s(msgBuffer)
}
/**
* Sign a message such that it can be verified with `ecrecover`.
* Similar to `web3.eth.accounts.sign` except that we sign the hash directly.
*
* @param {*} messageHash
* Hash of a message, as returned by `web3.utils.soliditySha3` or similar.
* @param {*} privateKey
* Privkey to sign with.
*
* @returns {{messageHash: string, r: string, s: string, v: string}}
*/
export const ethSignHash = (messageHash: string, privateKey: string) => {
// near identical to web3-eth-accounts (web3 v1)
// the main difference is we don't envelop the data.
const signature = Account.sign(messageHash, privateKey)
const vrs = Account.decodeSignature(signature)
return {
messageHash,
v: vrs[0],
r: vrs[1],
s: vrs[2],
signature
}
}
export /**
*
* Operates `ecrecover` over the provided signature
*
* @param {string} messageHash This should be an Ethereum HexString
* @param {string[]} [v, r, s] Components for the secp256k1 signature
* @returns {{verified: bool, address: EthAddress}}
*/
const ethVerifySig = (messageHash: string, [v, r, s]: string[]) => {
debugLog('ethVerifySig', `verifying messageHash: ${messageHash}`);
const address = Account.recover(messageHash, Account.encodeSignature([v, r, s]))
return {
verified: true,
address
}
}
export const sha256HashString = (stringToHash: string): Bytes64 => {
return toEthHex(sha256(stringToHash))
}
/**
* Verify a ed25519 signature
* @param signedData eth hex string of the dlgt request
* @param pubKey stellar pubkey
* @param signature 64 byte signature as eth hex
* @returns {boolean}
*/
export const ed25519SignatureIsValid = (signedData: string, pubKey: string, signature: string) => {
const _sig = cleanEthHex(signature)
assert.equal(_sig.length, 128, 'Invalid signature, should be a 64 byte hex string')
// Create the keypair from the public key
const kp = StellarBase.Keypair.fromPublicKey(pubKey)
// Create a buffer from the signature
const sigBuffer = Buffer.from(hexToUint8Array(_sig))
// Verify the request against the signature
return kp.verify(signedData, sigBuffer)
}