@msgboard/core 0.0.1
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/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +101 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +68 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +165 -0
- package/dist/utils.js.map +1 -0
- package/package.json +42 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./utils.js"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,6CAA0B"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Hex } from 'viem';
|
|
2
|
+
/** MessageSeed is the minimum set of inputs required for encoding and submitting a message to the board RPC. */
|
|
3
|
+
export type MessageSeed = {
|
|
4
|
+
/** The message/encoding version. */
|
|
5
|
+
version: number;
|
|
6
|
+
/** The hash of the block the message is rooted to. */
|
|
7
|
+
blockHash: Hex;
|
|
8
|
+
/** The 32-byte category hash. */
|
|
9
|
+
category: Hex;
|
|
10
|
+
/** The arbitrary message data. */
|
|
11
|
+
data: Hex;
|
|
12
|
+
/** The message nonce, discovered through PoW. */
|
|
13
|
+
nonce: bigint;
|
|
14
|
+
/** Factor to increase the required message work (set by RPC). */
|
|
15
|
+
workMultiplier: bigint;
|
|
16
|
+
/** Factor to decrease the required message work (set by RPC). */
|
|
17
|
+
workDivisor: bigint;
|
|
18
|
+
};
|
|
19
|
+
/** The full message object, with numerical values. */
|
|
20
|
+
export type Message = MessageSeed & {
|
|
21
|
+
/** The number of the block the message is rooted to. */
|
|
22
|
+
blockNumber: bigint;
|
|
23
|
+
/** The message hash. */
|
|
24
|
+
hash: Hex;
|
|
25
|
+
};
|
|
26
|
+
/** Stats about the message work process. */
|
|
27
|
+
export type WorkStats = {
|
|
28
|
+
/** The number of nonces that were attempted before a valid message was found. */
|
|
29
|
+
iterations: bigint;
|
|
30
|
+
/** The message difficulty, based on the message size and the configured difficulty factors. */
|
|
31
|
+
difficulty: bigint;
|
|
32
|
+
/** The duration of the message work process in milliseconds. */
|
|
33
|
+
duration: number;
|
|
34
|
+
/** True if the message is valid, false if not yet valid. */
|
|
35
|
+
isValid: boolean;
|
|
36
|
+
};
|
|
37
|
+
/** Result from the pow calculation. */
|
|
38
|
+
export type WorkResult = {
|
|
39
|
+
/** The resulting message object. */
|
|
40
|
+
message: Message;
|
|
41
|
+
/** Stats about the message work performed. */
|
|
42
|
+
stats: WorkStats;
|
|
43
|
+
};
|
|
44
|
+
/** Message is the hex-encoded message type returned from the board RPC. */
|
|
45
|
+
export type RPCMessage = {
|
|
46
|
+
[K in keyof Message]: Hex;
|
|
47
|
+
};
|
|
48
|
+
/** Categories is the type returned from the msgboard_categories RPC. */
|
|
49
|
+
export type Categories = Hex[];
|
|
50
|
+
/** Content filter can be used to filter board content responses from the RPC. */
|
|
51
|
+
export type ContentFilter = {
|
|
52
|
+
category?: Hex;
|
|
53
|
+
fromBlock?: bigint;
|
|
54
|
+
toBlock?: bigint;
|
|
55
|
+
};
|
|
56
|
+
/** Content is the type returned from the msgboard_content RPC. */
|
|
57
|
+
export type Content = {
|
|
58
|
+
[categoryHash: Hex]: RPCMessage[];
|
|
59
|
+
};
|
|
60
|
+
/** Status is the type returned from the msgboard_status RPC. */
|
|
61
|
+
export type Status = {
|
|
62
|
+
enabled: boolean;
|
|
63
|
+
/** Overall size of messages stored on the board. */
|
|
64
|
+
size: Hex;
|
|
65
|
+
/** Overall count of messages stored on the board. */
|
|
66
|
+
count: Hex;
|
|
67
|
+
/** The board's configured workMultiplier, required for valid messages. */
|
|
68
|
+
workMultiplier: Hex;
|
|
69
|
+
/** The board's configured workDivisor, required for valid messages. */
|
|
70
|
+
workDivisor: Hex;
|
|
71
|
+
};
|
|
72
|
+
/** The bare minimum provider interface required by the MsgBoardClient. */
|
|
73
|
+
export type Provider = {
|
|
74
|
+
request<T, U extends unknown[]>(arg: {
|
|
75
|
+
method: string;
|
|
76
|
+
params: U;
|
|
77
|
+
}): Promise<T>;
|
|
78
|
+
};
|
|
79
|
+
/** The bare minimum legacy provider interface required by the MsgBoardClient. */
|
|
80
|
+
export type LegacyProvider = {
|
|
81
|
+
send(method: string, params?: unknown[] | Record<string, any>): Promise<unknown>;
|
|
82
|
+
};
|
|
83
|
+
/** The logger type used by the MsgBoardClient. */
|
|
84
|
+
export type Logger = (formatter: any, ...args: any[]) => void;
|
|
85
|
+
/** DifficultyFactors are the modifiers for the message work, as required by the board RPC. */
|
|
86
|
+
export type DifficultyFactors = {
|
|
87
|
+
workMultiplier: bigint;
|
|
88
|
+
workDivisor: bigint;
|
|
89
|
+
};
|
|
90
|
+
/** MsgBoardClient config. */
|
|
91
|
+
export type Config = {
|
|
92
|
+
/** The difficulty factors for the msgboard (default: 10_000 / 1_000_000). */
|
|
93
|
+
difficultyFactors?: DifficultyFactors;
|
|
94
|
+
/** An optional logger (default: DEBUG logger). */
|
|
95
|
+
logger?: Logger;
|
|
96
|
+
/** The number of POW iterations between progress and block updates (default: 10s). */
|
|
97
|
+
breakInterval?: bigint;
|
|
98
|
+
/** Progress handler function called periodically when performing message work (default: none). */
|
|
99
|
+
progress?: (stats: WorkStats) => void;
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAA;AAE/B,gHAAgH;AAChH,MAAM,MAAM,WAAW,GAAG;IACxB,oCAAoC;IACpC,OAAO,EAAE,MAAM,CAAA;IACf,sDAAsD;IACtD,SAAS,EAAE,GAAG,CAAA;IACd,iCAAiC;IACjC,QAAQ,EAAE,GAAG,CAAA;IACb,kCAAkC;IAClC,IAAI,EAAE,GAAG,CAAA;IACT,iDAAiD;IACjD,KAAK,EAAE,MAAM,CAAA;IACb,iEAAiE;IACjE,cAAc,EAAE,MAAM,CAAA;IACtB,iEAAiE;IACjE,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,sDAAsD;AACtD,MAAM,MAAM,OAAO,GAAG,WAAW,GAAG;IAClC,wDAAwD;IACxD,WAAW,EAAE,MAAM,CAAA;IACnB,wBAAwB;IACxB,IAAI,EAAE,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AAE,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,CAAC,CAAA;KAAE,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;CAChF,CAAA;AAED,iFAAiF;AACjF,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;CACjF,CAAA;AAED,kDAAkD;AAElD,MAAM,MAAM,MAAM,GAAG,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;AAE7D,8FAA8F;AAC9F,MAAM,MAAM,iBAAiB,GAAG;IAC9B,cAAc,EAAE,MAAM,CAAA;IACtB,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,6BAA6B;AAC7B,MAAM,MAAM,MAAM,GAAG;IACnB,6EAA6E;IAC7E,iBAAiB,CAAC,EAAE,iBAAiB,CAAA;IACrC,kDAAkD;IAClD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,sFAAsF;IACtF,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,kGAAkG;IAClG,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAA;CACtC,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type ByteArray, type Hex } from 'viem';
|
|
2
|
+
import * as types from './types.js';
|
|
3
|
+
import { LegacyProvider } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Computes the message hash and checks it against the given difficulty.
|
|
6
|
+
* @param msg the message seed
|
|
7
|
+
* @param msgDifficulty the message difficulty
|
|
8
|
+
* @returns the message hash
|
|
9
|
+
* @throws error if the message nonce/work is not valid
|
|
10
|
+
*/
|
|
11
|
+
export declare function checkWork(msg: types.MessageSeed, msgDifficulty: bigint): `0x${string}` | null;
|
|
12
|
+
/**
|
|
13
|
+
* Returns the challenge component of the message hash calculation.
|
|
14
|
+
* @param msg the message seed
|
|
15
|
+
* @returns the challenge component
|
|
16
|
+
* @throws error if the challenge is invalid
|
|
17
|
+
*/
|
|
18
|
+
export declare function getChallenge(msg: types.MessageSeed): Uint8Array<ArrayBuffer>;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the modulus used for the PoW verification = (2^24)+(10k*dataLen).
|
|
21
|
+
* @param factors the message difficulty factors
|
|
22
|
+
* @param dataLen the length of message data
|
|
23
|
+
* @returns the computed message difficulty
|
|
24
|
+
*/
|
|
25
|
+
export declare function difficulty({ workMultiplier, workDivisor }: types.DifficultyFactors, dataLen: number): bigint;
|
|
26
|
+
/**
|
|
27
|
+
* Returns a partial digest from the combined difficulty factors.
|
|
28
|
+
* @param factors the message difficulty factors
|
|
29
|
+
* @returns a 16-byte partial digest in HEX form
|
|
30
|
+
*/
|
|
31
|
+
export declare function difficultyDigest({ workMultiplier, workDivisor }: types.DifficultyFactors): string;
|
|
32
|
+
/**
|
|
33
|
+
* Computes a category hash for creating a pow message.
|
|
34
|
+
* @param category the category string or byte array
|
|
35
|
+
* - if the input category is already in hex form it will be return as-is
|
|
36
|
+
* - if the input category is a byte array it will be truncated to 32 bytes and hex-encoded
|
|
37
|
+
* - if the input category is a string it will be passed through the keccak256 hash function
|
|
38
|
+
* @returns a 32-byte category hash
|
|
39
|
+
*/
|
|
40
|
+
export declare function categoryHash(category: string | Hex | ByteArray): Hex;
|
|
41
|
+
/**
|
|
42
|
+
* Encodes the given data into binary for creating a pow message.
|
|
43
|
+
* @param data the data string to encode
|
|
44
|
+
* @returns the hex-encoded data
|
|
45
|
+
*/
|
|
46
|
+
export declare function encodeData(data: string | ByteArray | Hex): `0x${string}`;
|
|
47
|
+
/**
|
|
48
|
+
* Encodes a MessageSeed as RLP for msgboard submission.
|
|
49
|
+
* @param msg the message inputs
|
|
50
|
+
* @returns the RLP-encoded byte array in hex
|
|
51
|
+
*/
|
|
52
|
+
export declare function toRLP(msg: types.MessageSeed): `0x${string}`;
|
|
53
|
+
/**
|
|
54
|
+
* Decodes a RLP-encoded message into its seed data.
|
|
55
|
+
* @param rlp the RLP-encoded byte array in hex
|
|
56
|
+
* @returns the encoded message seed
|
|
57
|
+
*/
|
|
58
|
+
export declare function fromRLP(rlp: Hex): types.MessageSeed;
|
|
59
|
+
/**
|
|
60
|
+
* Parses a hex-encoded RPCMessage.
|
|
61
|
+
* @param msg the hex-encoded RPC message
|
|
62
|
+
* @returns a parsed messaged type
|
|
63
|
+
*/
|
|
64
|
+
export declare function fromRPCMessage(msg: types.RPCMessage): types.Message;
|
|
65
|
+
export declare function toRPCMessage(msg: types.Message): types.RPCMessage;
|
|
66
|
+
/** Wraps a legacy provider to expose a standard request method. */
|
|
67
|
+
export declare function wrapLegacySend(provider: LegacyProvider): types.Provider;
|
|
68
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,SAAS,EAad,KAAK,GAAG,EAET,MAAM,MAAM,CAAA;AACb,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAM3C;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,WAAW,EAAE,aAAa,EAAE,MAAM,wBAWtE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,WAAW,2BASlD;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,EAAE,cAAc,EAAE,WAAW,EAAE,EAAE,KAAK,CAAC,iBAAiB,EAAE,OAAO,EAAE,MAAM,UAGnG;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,cAAc,EAAE,WAAW,EAAE,EAAE,KAAK,CAAC,iBAAiB,UAIxF;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,GAAG,GAAG,CAIpE;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,GAAG,iBAExD;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,WAAW,iBAU3C;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,KAAK,CAAC,WAAW,CAWnD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,OAAO,CASnE;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,UAAU,CASjE;AAED,mEAAmE;AACnE,wBAAgB,cAAc,CAAC,QAAQ,EAAE,cAAc,GAAG,KAAK,CAAC,QAAQ,CAMvE"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.checkWork = checkWork;
|
|
7
|
+
exports.getChallenge = getChallenge;
|
|
8
|
+
exports.difficulty = difficulty;
|
|
9
|
+
exports.difficultyDigest = difficultyDigest;
|
|
10
|
+
exports.categoryHash = categoryHash;
|
|
11
|
+
exports.encodeData = encodeData;
|
|
12
|
+
exports.toRLP = toRLP;
|
|
13
|
+
exports.fromRLP = fromRLP;
|
|
14
|
+
exports.fromRPCMessage = fromRPCMessage;
|
|
15
|
+
exports.toRPCMessage = toRPCMessage;
|
|
16
|
+
exports.wrapLegacySend = wrapLegacySend;
|
|
17
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
18
|
+
const elliptic_1 = __importDefault(require("elliptic"));
|
|
19
|
+
const viem_1 = require("viem");
|
|
20
|
+
const EC = elliptic_1.default.ec;
|
|
21
|
+
const ec = new EC('secp256k1');
|
|
22
|
+
const g = ec.g;
|
|
23
|
+
/**
|
|
24
|
+
* Computes the message hash and checks it against the given difficulty.
|
|
25
|
+
* @param msg the message seed
|
|
26
|
+
* @param msgDifficulty the message difficulty
|
|
27
|
+
* @returns the message hash
|
|
28
|
+
* @throws error if the message nonce/work is not valid
|
|
29
|
+
*/
|
|
30
|
+
function checkWork(msg, msgDifficulty) {
|
|
31
|
+
const bytes = new Uint8Array([
|
|
32
|
+
...getChallenge(msg),
|
|
33
|
+
...(0, viem_1.hexToBytes)(msg.category, { size: 32 }),
|
|
34
|
+
...(0, viem_1.hexToBytes)(msg.data),
|
|
35
|
+
]);
|
|
36
|
+
const hash = (0, viem_1.sha256)(bytes);
|
|
37
|
+
if (BigInt(hash) % msgDifficulty !== 0n) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
return hash;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns the challenge component of the message hash calculation.
|
|
44
|
+
* @param msg the message seed
|
|
45
|
+
* @returns the challenge component
|
|
46
|
+
* @throws error if the challenge is invalid
|
|
47
|
+
*/
|
|
48
|
+
function getChallenge(msg) {
|
|
49
|
+
const digest = BigInt(difficultyDigest(msg));
|
|
50
|
+
// nonce = msg.nonce * msg.difficultyDigest() + msg.blockHash
|
|
51
|
+
const nonce = new bn_js_1.default((msg.nonce * digest + BigInt(msg.blockHash)).toString());
|
|
52
|
+
const challenge = g.mul(nonce);
|
|
53
|
+
if (challenge.isInfinity()) {
|
|
54
|
+
throw new Error('unable to create challenge');
|
|
55
|
+
}
|
|
56
|
+
return Uint8Array.from(challenge.getX().toArray());
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns the modulus used for the PoW verification = (2^24)+(10k*dataLen).
|
|
60
|
+
* @param factors the message difficulty factors
|
|
61
|
+
* @param dataLen the length of message data
|
|
62
|
+
* @returns the computed message difficulty
|
|
63
|
+
*/
|
|
64
|
+
function difficulty({ workMultiplier, workDivisor }, dataLen) {
|
|
65
|
+
// difficulty is increased with the size of the message
|
|
66
|
+
return ((2n ** 24n + BigInt(dataLen) * 10000n) * workMultiplier) / workDivisor;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns a partial digest from the combined difficulty factors.
|
|
70
|
+
* @param factors the message difficulty factors
|
|
71
|
+
* @returns a 16-byte partial digest in HEX form
|
|
72
|
+
*/
|
|
73
|
+
function difficultyDigest({ workMultiplier, workDivisor }) {
|
|
74
|
+
return `0x${(0, viem_1.sha256)((0, viem_1.concatBytes)([(0, viem_1.numberToBytes)(workMultiplier, { size: 8 }), (0, viem_1.numberToBytes)(workDivisor, { size: 8 })])).slice(34)}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Computes a category hash for creating a pow message.
|
|
78
|
+
* @param category the category string or byte array
|
|
79
|
+
* - if the input category is already in hex form it will be return as-is
|
|
80
|
+
* - if the input category is a byte array it will be truncated to 32 bytes and hex-encoded
|
|
81
|
+
* - if the input category is a string it will be passed through the keccak256 hash function
|
|
82
|
+
* @returns a 32-byte category hash
|
|
83
|
+
*/
|
|
84
|
+
function categoryHash(category) {
|
|
85
|
+
if ((0, viem_1.isHex)(category))
|
|
86
|
+
return category;
|
|
87
|
+
if ((0, viem_1.isBytes)(category))
|
|
88
|
+
return (0, viem_1.bytesToHex)(category, { size: 32 });
|
|
89
|
+
return (0, viem_1.keccak256)((0, viem_1.stringToBytes)(category));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Encodes the given data into binary for creating a pow message.
|
|
93
|
+
* @param data the data string to encode
|
|
94
|
+
* @returns the hex-encoded data
|
|
95
|
+
*/
|
|
96
|
+
function encodeData(data) {
|
|
97
|
+
return (0, viem_1.isHex)(data) ? data : (0, viem_1.toHex)(data);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Encodes a MessageSeed as RLP for msgboard submission.
|
|
101
|
+
* @param msg the message inputs
|
|
102
|
+
* @returns the RLP-encoded byte array in hex
|
|
103
|
+
*/
|
|
104
|
+
function toRLP(msg) {
|
|
105
|
+
return (0, viem_1.toRlp)([
|
|
106
|
+
(0, viem_1.numberToBytes)(msg.version, { size: 1 }), // single byte
|
|
107
|
+
(0, viem_1.hexToBytes)(msg.blockHash, { size: 32 }), // 32-byte hash
|
|
108
|
+
(0, viem_1.numberToBytes)(msg.nonce),
|
|
109
|
+
(0, viem_1.numberToBytes)(msg.workMultiplier),
|
|
110
|
+
(0, viem_1.numberToBytes)(msg.workDivisor),
|
|
111
|
+
(0, viem_1.hexToBytes)(msg.category, { size: 32 }), // 32-byte hash
|
|
112
|
+
(0, viem_1.hexToBytes)(msg.data),
|
|
113
|
+
]);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Decodes a RLP-encoded message into its seed data.
|
|
117
|
+
* @param rlp the RLP-encoded byte array in hex
|
|
118
|
+
* @returns the encoded message seed
|
|
119
|
+
*/
|
|
120
|
+
function fromRLP(rlp) {
|
|
121
|
+
const [version, blockHash, nonce, workMultiplier, workDivisor, category, data] = (0, viem_1.fromRlp)(rlp);
|
|
122
|
+
return {
|
|
123
|
+
version: Number(version),
|
|
124
|
+
blockHash,
|
|
125
|
+
nonce: BigInt(nonce),
|
|
126
|
+
workMultiplier: BigInt(workMultiplier),
|
|
127
|
+
workDivisor: BigInt(workDivisor),
|
|
128
|
+
category,
|
|
129
|
+
data,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Parses a hex-encoded RPCMessage.
|
|
134
|
+
* @param msg the hex-encoded RPC message
|
|
135
|
+
* @returns a parsed messaged type
|
|
136
|
+
*/
|
|
137
|
+
function fromRPCMessage(msg) {
|
|
138
|
+
return {
|
|
139
|
+
...msg,
|
|
140
|
+
blockNumber: BigInt(msg.blockNumber),
|
|
141
|
+
nonce: BigInt(msg.nonce),
|
|
142
|
+
workMultiplier: BigInt(msg.workMultiplier),
|
|
143
|
+
workDivisor: BigInt(msg.workDivisor),
|
|
144
|
+
version: Number(msg.version),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function toRPCMessage(msg) {
|
|
148
|
+
return {
|
|
149
|
+
...msg,
|
|
150
|
+
blockNumber: (0, viem_1.numberToHex)(msg.blockNumber),
|
|
151
|
+
version: (0, viem_1.numberToHex)(msg.version),
|
|
152
|
+
nonce: (0, viem_1.numberToHex)(msg.nonce),
|
|
153
|
+
workMultiplier: (0, viem_1.numberToHex)(msg.workMultiplier),
|
|
154
|
+
workDivisor: (0, viem_1.numberToHex)(msg.workDivisor),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/** Wraps a legacy provider to expose a standard request method. */
|
|
158
|
+
function wrapLegacySend(provider) {
|
|
159
|
+
return {
|
|
160
|
+
request(args) {
|
|
161
|
+
return provider.send(args.method, args.params);
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;AAiCA,8BAWC;AAQD,oCASC;AAQD,gCAGC;AAOD,4CAIC;AAUD,oCAIC;AAOD,gCAEC;AAOD,sBAUC;AAOD,0BAWC;AAOD,wCASC;AAED,oCASC;AAGD,wCAMC;AAjLD,kDAAsB;AACtB,wDAA+B;AAC/B,+BAgBa;AAIb,MAAM,EAAE,GAAG,kBAAQ,CAAC,EAAE,CAA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,CAAC;AAED,SAAgB,YAAY,CAAC,GAAkB;IAC7C,OAAO;QACL,GAAG,GAAG;QACN,WAAW,EAAE,IAAA,kBAAW,EAAC,GAAG,CAAC,WAAW,CAAC;QACzC,OAAO,EAAE,IAAA,kBAAW,EAAC,GAAG,CAAC,OAAO,CAAC;QACjC,KAAK,EAAE,IAAA,kBAAW,EAAC,GAAG,CAAC,KAAK,CAAC;QAC7B,cAAc,EAAE,IAAA,kBAAW,EAAC,GAAG,CAAC,cAAc,CAAC;QAC/C,WAAW,EAAE,IAAA,kBAAW,EAAC,GAAG,CAAC,WAAW,CAAC;KAC1C,CAAA;AACH,CAAC;AAED,mEAAmE;AACnE,SAAgB,cAAc,CAAC,QAAwB;IACrD,OAAO;QACL,OAAO,CAAyB,IAAmC;YACjE,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAe,CAAA;QAC9D,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@msgboard/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "MsgBoard proof-of-work engine (msgpow): pure primitives and encoding",
|
|
5
|
+
"repository": "github:valve-tech/msgboard",
|
|
6
|
+
"author": "MsgBoard",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"commonjs": "./dist/index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"require": "./dist/index.js",
|
|
18
|
+
"default": "./dist/index.js",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"keywords": ["ethereum", "pow", "proof-of-work", "msgboard", "msgpow"],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prebuild": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"watch": "tsc -w",
|
|
28
|
+
"test": "vitest run"
|
|
29
|
+
},
|
|
30
|
+
"files": ["dist/"],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"bn.js": "^5.2.1",
|
|
33
|
+
"elliptic": "^6.6.1",
|
|
34
|
+
"viem": "^2.25.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/bn.js": "^5.1.6",
|
|
38
|
+
"@types/elliptic": "^6.4.18",
|
|
39
|
+
"typescript": "^5.8.2",
|
|
40
|
+
"vitest": "^3.1.1"
|
|
41
|
+
}
|
|
42
|
+
}
|