@neuraiproject/neurai-message 0.7.0
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/LICENSE +21 -0
- package/README.md +52 -0
- package/index.ts +23 -0
- package/package.json +39 -0
- package/test.js +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Neurai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# neurai-message
|
|
2
|
+
|
|
3
|
+
Sign and Verify messages in Neurai in JavaScript, primarly for Node.js
|
|
4
|
+
|
|
5
|
+
## If you want to use it in the browser, use Browserify
|
|
6
|
+
|
|
7
|
+
@neuraiproject/neurai-message is based on 'bitcoinjs-lib' which uses tons of Node stuff.
|
|
8
|
+
|
|
9
|
+
To make that work in the browser you need to use Browserify
|
|
10
|
+
|
|
11
|
+
## install
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm install @neuraiproject/neurai-message
|
|
15
|
+
|
|
16
|
+
//If you need to sign messages, install CoinKey
|
|
17
|
+
npm install coinkey
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## How to use
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
const { sign, verifyMessage } = require("@neuraiproject/neurai-message");
|
|
24
|
+
|
|
25
|
+
//coinkey helps us convert from WIF to privatekey
|
|
26
|
+
const CoinKey = require("coinkey");
|
|
27
|
+
|
|
28
|
+
//Sign
|
|
29
|
+
{
|
|
30
|
+
//Address RVDUQTULaceEudDsgqCQBT6bfcdqUSvJPV
|
|
31
|
+
//Public Key 031c5142f11f629bad27dd567c41e189ee23eccd9b57561fd0ff7c96b2cc9a0a0f
|
|
32
|
+
const privateKeyWIF = "L1JHsDosNU9FeUYB24Pixwkxs56pwCrj5rdtuKHXTcWBJTDLGNa7";
|
|
33
|
+
|
|
34
|
+
//Convert WIF to private key
|
|
35
|
+
const privateKey = CoinKey.fromWif(privateKeyWIF).privateKey;
|
|
36
|
+
const message = "Hello world";
|
|
37
|
+
|
|
38
|
+
const signature = sign(message, privateKey);
|
|
39
|
+
console.log("Signature", signature);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//Verify
|
|
43
|
+
{
|
|
44
|
+
const address = "RS4EYELZhxMtDAuyrQimVrcSnaeaLCXeo6";
|
|
45
|
+
const message = "Hello world";
|
|
46
|
+
const signature =
|
|
47
|
+
"H2zo48+tI/KT9eJrHt7PLiEBMaRn1A1Eh49IFu0MbfhAFBxVc0FG2UE5E79PCbhd9KexijsQxYvNM6AsVn9EAEo=";
|
|
48
|
+
|
|
49
|
+
console.log("Verify", verifyMessage(message, address, signature));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
```
|
package/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import bitcoinMessage from "bitcoinjs-message";
|
|
2
|
+
const MESSAGE_PREFIX = "\x16Neurai Signed Message:\n";
|
|
3
|
+
|
|
4
|
+
/** returns a base64 encoded string representation of the signature */
|
|
5
|
+
export function sign(message: string, privateKey: any, compressed = true) {
|
|
6
|
+
const signature = bitcoinMessage.sign(
|
|
7
|
+
message,
|
|
8
|
+
privateKey,
|
|
9
|
+
compressed,
|
|
10
|
+
MESSAGE_PREFIX
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
return signature.toString("base64");
|
|
14
|
+
}
|
|
15
|
+
export function verifyMessage(
|
|
16
|
+
message: string,
|
|
17
|
+
address: string,
|
|
18
|
+
signature: string
|
|
19
|
+
) {
|
|
20
|
+
const m = Buffer.from(message).toString("ascii");
|
|
21
|
+
const result = bitcoinMessage.verify(m, address, signature, MESSAGE_PREFIX);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@neuraiproject/neurai-message",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Sign and Verify messages in Neurai",
|
|
5
|
+
"source": "index.ts",
|
|
6
|
+
"main": "dist/main.js",
|
|
7
|
+
"module": "dist/module.js",
|
|
8
|
+
"types": "dist/types.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"build": "npx parcel build"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/neuraiproject/neurai-message.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"Neurai",
|
|
19
|
+
"XNA"
|
|
20
|
+
],
|
|
21
|
+
"author": "Dick Henrik Larsson / Neurai",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/neuraiproject/neurai-message/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/neuraiproject/neurai-message#readme",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"bitcoinjs-message": "^2.2.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@parcel/packager-ts": "^2.8.3",
|
|
32
|
+
"@parcel/transformer-typescript-types": "^2.8.3",
|
|
33
|
+
"@types/jest": "^28.1.4",
|
|
34
|
+
"@types/node": "^18.0.0",
|
|
35
|
+
"jest": "^29.4.0",
|
|
36
|
+
"parcel": "^2.8.3",
|
|
37
|
+
"typescript": "^4.9.4"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const { verifyMessage } = require("./dist/main");
|
|
2
|
+
|
|
3
|
+
const address = "RS4EYELZhxMtDAuyrQimVrcSnaeaLCXeo6";
|
|
4
|
+
const message = "Hello world";
|
|
5
|
+
const signature =
|
|
6
|
+
"H2zo48+tI/KT9eJrHt7PLiEBMaRn1A1Eh49IFu0MbfhAFBxVc0FG2UE5E79PCbhd9KexijsQxYvNM6AsVn9EAEo=";
|
|
7
|
+
|
|
8
|
+
test("Verify valid message signature", () => {
|
|
9
|
+
const result = verifyMessage(message, address, signature);
|
|
10
|
+
|
|
11
|
+
expect(result).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("Verify unvalid message signature", () => {
|
|
15
|
+
const result = verifyMessage(
|
|
16
|
+
message + " change the message",
|
|
17
|
+
address,
|
|
18
|
+
signature
|
|
19
|
+
);
|
|
20
|
+
expect(result).toBe(false);
|
|
21
|
+
});
|