@exodus/solana-lib 1.6.8 → 1.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-lib",
3
- "version": "1.6.8",
3
+ "version": "1.6.9",
4
4
  "description": "Exodus internal Solana low-level library",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -26,6 +26,5 @@
26
26
  "create-hash": "^1.1.3",
27
27
  "lodash": "^4.17.11",
28
28
  "tweetnacl": "^1.0.3"
29
- },
30
- "gitHead": "846ed76714355879216f43c6be8ec2b072169635"
29
+ }
31
30
  }
@@ -1,11 +1,45 @@
1
+ import assert from 'assert'
1
2
  import nacl from 'tweetnacl'
2
3
  import bs58 from 'bs58'
3
4
 
5
+ import { Message } from '../vendor/message'
4
6
  import { getKeyPairFromPrivateKey } from '../keypair'
5
7
 
8
+ function isLegacyMessage(data) {
9
+ try {
10
+ const message = Message.from(data)
11
+ message.serialize() // Invalid messages will throw on serialization.
12
+ return true
13
+ } catch (err) {
14
+ return false
15
+ }
16
+ }
17
+
18
+ function isVersionedMessage(data) {
19
+ if (data.length > 1) {
20
+ // We deploy a heuristic to detect transaction messages.
21
+ // The first bytes of a transaction message contains its version number
22
+ // so we ban all bytes starting at 0x80 and ending at 0xFE
23
+ // 0xFF is allowed because that is used for offchain messages
24
+ return data[0] >= 0x80 && data[0] !== 0xff
25
+ } else {
26
+ return false
27
+ }
28
+ }
29
+
30
+ export function isTransactionMessage(data) {
31
+ const messageBuffer = Buffer.from(data)
32
+ return isLegacyMessage(messageBuffer) || isVersionedMessage(messageBuffer)
33
+ }
34
+
6
35
  export default function signMessage({ message, privateKey }) {
7
36
  const { secretKey } = getKeyPairFromPrivateKey(privateKey)
8
- const signature = nacl.sign.detached(bs58.decode(message), secretKey)
37
+ const messageBuffer = bs58.decode(message)
38
+ assert(
39
+ !isTransactionMessage(messageBuffer),
40
+ 'attempted to sign transaction using message signing'
41
+ )
42
+ const signature = nacl.sign.detached(messageBuffer, secretKey)
9
43
  const bs58Signature = bs58.encode(signature)
10
44
  return bs58Signature
11
45
  }