@sig-net/midnight 0.21.0-rc.8 → 0.21.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/README.md +5 -3
- package/dist/constants.d.ts +5 -4
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +12 -7
- package/dist/constants.js.map +1 -1
- package/dist/ecdsa-attestation.d.ts +37 -8
- package/dist/ecdsa-attestation.d.ts.map +1 -1
- package/dist/ecdsa-attestation.js +88 -15
- package/dist/ecdsa-attestation.js.map +1 -1
- package/dist/epsilon-derivation.d.ts +21 -17
- package/dist/epsilon-derivation.d.ts.map +1 -1
- package/dist/epsilon-derivation.js +32 -29
- package/dist/epsilon-derivation.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/managed/compiler/contract-info.json +20 -4
- package/dist/managed/compiler/contract-manifest.json +11 -11
- package/dist/managed/contract/index.d.ts +2 -0
- package/dist/managed/contract/index.js +55 -46
- package/dist/managed/contract/index.js.map +1 -1
- package/dist/signet-evtype2tx-requests.d.ts +1 -1
- package/dist/signet-evtype2tx-requests.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/Signet.compact +16 -3
- package/src/circuits.compact +1 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The [Sig Network](https://sig.network) [Distributed MPC](https://github.com/sig-net/mpc) integration for the [Midnight blockchain](https://midnight.network) lets contracts on Midnight execute arbitrary transactions on foreign blockchains, and respond to the results of those transactions.
|
|
4
4
|
|
|
5
|
-
`@sig-net/midnight` is the client-agnostic protocol library for that integration. It carries everything a Midnight contract or off-chain client needs to speak the signet protocol (the MPC's [sign bidirectional flow](https://github.com/sig-net/midnight-integration/blob/main/README.md#sign-bidirectional-flow)). It does not bind to any particular contract.
|
|
5
|
+
`@sig-net/midnight` is the client-agnostic protocol library for that integration. It carries everything a Midnight contract or off-chain client needs to speak the signet protocol (the MPC's [sign bidirectional flow](https://github.com/sig-net/midnight-integration/blob/main/README.md#sign-bidirectional-protocol-flow)). It does not bind to any particular contract.
|
|
6
6
|
|
|
7
7
|
> ## 🚧 Under Construction 🚧
|
|
8
8
|
>
|
|
@@ -33,8 +33,9 @@ npm install @sig-net/midnight
|
|
|
33
33
|
|
|
34
34
|
The protocol and integration documentation lives in the [sig-net/midnight-integration README](https://github.com/sig-net/midnight-integration/blob/main/README.md):
|
|
35
35
|
|
|
36
|
-
- [Sign Bidirectional Flow](https://github.com/sig-net/midnight-integration/blob/main/README.md#sign-bidirectional-flow): the 5-step protocol this package speaks, with diagram, failure handling and output recovery.
|
|
36
|
+
- [Sign Bidirectional Flow](https://github.com/sig-net/midnight-integration/blob/main/README.md#sign-bidirectional-protocol-flow): the 5-step protocol this package speaks, with diagram, failure handling and output recovery.
|
|
37
37
|
- [Derived keys](https://github.com/sig-net/midnight-integration/blob/main/README.md#derived-keys): the request signing key and the response key, and how the MPC derives them.
|
|
38
|
+
- [Handling Failure](https://github.com/sig-net/midnight-integration/blob/main/README.md#handling-failure): the fixed 5-byte failure payload the MPC attests for a failed foreign transaction, how a contract tells it apart from a success, and which respond schemas cannot.
|
|
38
39
|
- [Integrator Guide](https://github.com/sig-net/midnight-integration/blob/main/README.md#integrator-guide): the once-off setup and the per-request runtime steps, built on this package's exports.
|
|
39
40
|
- [EVM Type 2 transactions and ABI calldata words](https://github.com/sig-net/midnight-integration/blob/main/README.md#evm-type-2-transactions-and-abi-calldata-words): building calldata words in-circuit and deserialising respond payloads.
|
|
40
41
|
|
|
@@ -66,10 +67,11 @@ What clients import from `@sig-net/midnight`:
|
|
|
66
67
|
| Derive the key the MPC signs your requests with | `deriveEvmAddress`: the expected signer address checked in step 2 (see [Derived keys](https://github.com/sig-net/midnight-integration/blob/main/README.md#derived-keys)). |
|
|
67
68
|
| Derive the response key your deploy pins | `deriveMidnightResponseKey`: the key `initialise` stores (setup step 4), derived from your contract's address. |
|
|
68
69
|
| Look up published counterparty values | `MidnightNetwork`, `getMpcRootPublicKey` and `getSignetContractAddress`: the fixed per-network values (see the notes in [Runtime](https://github.com/sig-net/midnight-integration/blob/main/README.md#runtime)). |
|
|
70
|
+
| Accept an MPC public key in any published spelling | `parseSecp256k1PublicKey` (to a Compact `Secp256k1Point`) and `normaliseSecp256k1PublicKey` (to the canonical `0x04…` uncompressed SEC1 hex): both take SEC1 hex, compressed or uncompressed with an optional `0x`, and NEAR's `secp256k1:<base58>`. |
|
|
69
71
|
| Compute a request id off chain | `calculateRequestId`: the TS twin of the on-chain circuit, plus `requestIdHex` and `parseRequestIdHex` for the hex form. |
|
|
70
72
|
| Compose expected calldata words off chain (UIs, expected-record builders, tests) | The builders `numericAbiWord`, `evmAddressAbiWord` and `boolAbiWord`, and the readers `abiWordToUint128` and `abiWordToBool`: TS twins of the circuits under identical names. |
|
|
71
73
|
| Convert a foreign execution output into respond bytes | `deserializeEvmOutput` (raw EVM return data to named values) and `serializeRespondOutput` (named values to the packed respond payload the MPC attests): together they rebuild the `serializedOutput` of steps 4 and 5. |
|
|
72
|
-
| Recognise a failed remote execution | `MPC_FAILURE_OUTPUT` and `isMpcFailureOutput`: the MPC's fixed 5-byte failure payload for reverted or replaced transactions. |
|
|
74
|
+
| Recognise a failed remote execution | `MPC_FAILURE_OUTPUT` and `isMpcFailureOutput`: the MPC's fixed 5-byte failure payload for reverted or replaced transactions (see [Handling Failure](https://github.com/sig-net/midnight-integration/blob/main/README.md#handling-failure)). |
|
|
73
75
|
| Verify attestations without the reader | `verifyRespondBidirectionalSignature`: the check the reader runs internally, exposed for custom pipelines. |
|
|
74
76
|
| Mint attestations in your contract's unit tests | The `@sig-net/midnight/testing` entry point, see [Testing entry point](#testing-entry-point). |
|
|
75
77
|
| Discover requests MPC-side (responders, background workers) | The discovery primitives: decode the signet contract's emitted notification events with `decodeSignBidirectionalEventNotificationPayload` and `decodeSignBidirectionalNotification`, then resolve each pointer against the named caller's own request map with `lookupSignetRequestAt` (the authenticated read). The polling loop belongs to the responder. |
|
package/dist/constants.d.ts
CHANGED
|
@@ -63,10 +63,11 @@ export declare enum MidnightNetwork {
|
|
|
63
63
|
*/
|
|
64
64
|
export type DeployedNetwork = Exclude<MidnightNetwork, MidnightNetwork.Undeployed>;
|
|
65
65
|
/**
|
|
66
|
-
* The MPC root public key of a deployed Midnight network, as
|
|
67
|
-
*
|
|
68
|
-
* `
|
|
69
|
-
*
|
|
66
|
+
* The MPC root public key of a deployed Midnight network, as `0x04…`
|
|
67
|
+
* uncompressed SEC1 hex (the canonical spelling of
|
|
68
|
+
* `normaliseSecp256k1PublicKey`): the `mpcRootPublicKey` every client key
|
|
69
|
+
* derivation starts from (see `deriveEvmAddress`). A local standalone stack
|
|
70
|
+
* has no fixed key: its setup generates a fresh `MPC_ROOT_KEY` per stack.
|
|
70
71
|
*
|
|
71
72
|
* @param networkId - The deployed network to look up.
|
|
72
73
|
* @returns The network's MPC root public key.
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AASA,+DAA+D;AAC/D,eAAO,MAAM,cAAc,KAAK,CAAC;AAEjC,8DAA8D;AAC9D,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC,wFAAwF;AACxF,eAAO,MAAM,cAAc,IAAI,CAAC;AAEhC;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,yBAAiD,CAAC;AAEjF;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,gBAAgB,EAAE,UAAU,GAAG,OAAO,CAKxE;AAED;;;GAGG;AACH,eAAO,MAAM,0BAA0B,KAAK,CAAC;AAE7C;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAUpE;AAED;;;GAGG;AACH,oBAAY,eAAe;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,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,eAAe,GAAG,MAAM,CAM3E"}
|
package/dist/constants.js
CHANGED
|
@@ -75,20 +75,25 @@ export var MidnightNetwork;
|
|
|
75
75
|
/** Production network (live, real value). */
|
|
76
76
|
MidnightNetwork["Mainnet"] = "mainnet";
|
|
77
77
|
})(MidnightNetwork || (MidnightNetwork = {}));
|
|
78
|
-
//
|
|
79
|
-
//
|
|
78
|
+
// Each network's MPC root public key in the canonical `0x04…` uncompressed
|
|
79
|
+
// SEC1 hex (`normaliseSecp256k1PublicKey`), filled in as the MPC operators
|
|
80
|
+
// publish it for that network. An empty string means "not yet published" and
|
|
80
81
|
// makes getMpcRootPublicKey throw for that network.
|
|
81
82
|
const mpcRootPublicKeys = {
|
|
82
|
-
[MidnightNetwork.Stagenet]:
|
|
83
|
+
[MidnightNetwork.Stagenet]:
|
|
84
|
+
// Canonical form of the signet.js TESTNET_DEV root key:
|
|
85
|
+
// secp256k1:54hU5wcCmVUPFWLDALXMh1fFToZsVXrx9BbTbHzSfQq1Kd1rJZi52iPa4QQxo6s5TgjWqgpY8HamYuUDzG6fAaUq
|
|
86
|
+
"0x04cb41bab8bc97121f4902514ca57a284f167b9239ecb8176831d1ef0fede87c61ca3e59da1c194aa90108098a9e5cdc55d3b3297cdefbc085ffafd0f2c34ae61a",
|
|
83
87
|
[MidnightNetwork.Preview]: "",
|
|
84
88
|
[MidnightNetwork.Preprod]: "",
|
|
85
89
|
[MidnightNetwork.Mainnet]: "",
|
|
86
90
|
};
|
|
87
91
|
/**
|
|
88
|
-
* The MPC root public key of a deployed Midnight network, as
|
|
89
|
-
*
|
|
90
|
-
* `
|
|
91
|
-
*
|
|
92
|
+
* The MPC root public key of a deployed Midnight network, as `0x04…`
|
|
93
|
+
* uncompressed SEC1 hex (the canonical spelling of
|
|
94
|
+
* `normaliseSecp256k1PublicKey`): the `mpcRootPublicKey` every client key
|
|
95
|
+
* derivation starts from (see `deriveEvmAddress`). A local standalone stack
|
|
96
|
+
* has no fixed key: its setup generates a fresh `MPC_ROOT_KEY` per stack.
|
|
92
97
|
*
|
|
93
98
|
* @param networkId - The deployed network to look up.
|
|
94
99
|
* @returns The network's MPC root public key.
|
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,2EAA2E;AAC3E,mEAAmE;AACnE,qEAAqE;AACrE,sDAAsD;AACtD,EAAE;AACF,0EAA0E;AAC1E,oBAAoB;AAEpB,+DAA+D;AAC/D,MAAM,CAAC,MAAM,cAAc,GAAG,EAAE,CAAC;AAEjC,8DAA8D;AAC9D,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAEnC,wFAAwF;AACxF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC;AAEhC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAEjF;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,gBAA4B;IAC7D,OAAO,CACL,gBAAgB,CAAC,MAAM,KAAK,kBAAkB,CAAC,MAAM;QACrD,kBAAkB,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,CAC5E,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,EAAE,CAAC;AAE7C;;;;;;;;;GASG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,MAAc;IACtD,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/C,IAAI,OAAO,CAAC,MAAM,GAAG,MAAM,E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eAAe,CAAC,OAAO,CAAC,EAAE,EAAE;IAC7B,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,EAAE;CAC9B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB,CAAC,SAA0B;IAC5D,MAAM,SAAS,GAAG,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAC/C,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,6CAA6C,SAAS,eAAe,CAAC,CAAC;IACzF,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,qEAAqE;AACrE,0EAA0E;AAC1E,wEAAwE;AACxE,MAAM,uBAAuB,GAAoC;IAC/D,CAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,kEAAkE;IAC9F,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,EAAE;IAC7B,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,EAAE;IAC7B,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,EAAE;CAC9B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,wBAAwB,CAAC,SAA0B;IACjE,MAAM,eAAe,GAAG,uBAAuB,CAAC,SAAS,CAAC,CAAC;IAC3D,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,iDAAiD,SAAS,eAAe,CAAC,CAAC;IAC7F,CAAC;IACD,OAAO,eAAe,CAAC;AACzB,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Secp256k1Point } from "@midnight-ntwrk/compact-runtime";
|
|
2
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
2
3
|
import { Signature } from "ethers";
|
|
3
4
|
import type { MpcSignature, RespondBidirectionalEvent, SignatureRespondedEvent } from "./signet-contract-events.ts";
|
|
4
5
|
import type { RequestId } from "./signet-requests.ts";
|
|
@@ -74,25 +75,53 @@ export declare function signatureRespondedEventToSignature(response: SignatureRe
|
|
|
74
75
|
* @throws {Error} If `r` is not the x coordinate of a secp256k1 point.
|
|
75
76
|
*/
|
|
76
77
|
export declare function signatureToSignatureRespondedEvent(signature: Pick<Signature, "r" | "s" | "yParity">): SignatureRespondedEvent;
|
|
78
|
+
/** A point in noble's own representation, for curve arithmetic. */
|
|
79
|
+
type NobleSecp256k1Point = ReturnType<typeof secp256k1.Point.fromHex>;
|
|
77
80
|
/**
|
|
78
|
-
* Parse a secp256k1 public key
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
+
* Parse a secp256k1 public key in any accepted spelling (see
|
|
82
|
+
* {@link parseSecp256k1PublicKey}) into noble's point, the shape the
|
|
83
|
+
* derivation arithmetic runs on. Package-internal: the public surface hands
|
|
84
|
+
* out the Compact runtime's `Secp256k1Point` instead.
|
|
81
85
|
*
|
|
82
|
-
* @param value - The SEC1 hex
|
|
83
|
-
* @returns The
|
|
86
|
+
* @param value - The public key, NEAR or SEC1 hex spelling.
|
|
87
|
+
* @returns The point on secp256k1.
|
|
84
88
|
* @throws {Error} If the value is not a valid secp256k1 public key.
|
|
85
89
|
*/
|
|
90
|
+
export declare function parseSecp256k1PublicKeyToNoblePoint(value: string): NobleSecp256k1Point;
|
|
91
|
+
/**
|
|
92
|
+
* Parse a secp256k1 public key into the Compact runtime's `Secp256k1Point`
|
|
93
|
+
* shape: how deploys receive the MPC response key to pin. Accepts every
|
|
94
|
+
* spelling a key is published in: SEC1 hex, compressed (`02`/`03`) or
|
|
95
|
+
* uncompressed (`04`) with an optional `0x`, and NEAR's `secp256k1:<base58>`
|
|
96
|
+
* (what signet.js and the MPC operators publish). The point is checked to
|
|
97
|
+
* lie on the curve.
|
|
98
|
+
*
|
|
99
|
+
* @param value - The public key in any accepted spelling.
|
|
100
|
+
* @returns The parsed point.
|
|
101
|
+
* @throws {Error} If the value is not a valid secp256k1 public key in any accepted spelling.
|
|
102
|
+
*/
|
|
86
103
|
export declare function parseSecp256k1PublicKey(value: string): Secp256k1Point;
|
|
87
104
|
/**
|
|
88
|
-
* Format a secp256k1 public key point as uncompressed SEC1 hex
|
|
89
|
-
* prefix
|
|
90
|
-
*
|
|
105
|
+
* Format a secp256k1 public key point as uncompressed SEC1 hex with a `0x`
|
|
106
|
+
* prefix, the canonical spelling this package publishes and prints keys in:
|
|
107
|
+
* the round-trip inverse of {@link parseSecp256k1PublicKey}, for handing a
|
|
108
|
+
* key to deploys via env/config.
|
|
91
109
|
*
|
|
92
110
|
* @param point - The point to format.
|
|
93
111
|
* @returns The `0x04…` uncompressed SEC1 hex string.
|
|
94
112
|
*/
|
|
95
113
|
export declare function formatSecp256k1PublicKey(point: Secp256k1Point): string;
|
|
114
|
+
/**
|
|
115
|
+
* Canonicalise a public key given in any accepted spelling (see
|
|
116
|
+
* {@link parseSecp256k1PublicKey}) to the `0x04…` uncompressed SEC1 hex of
|
|
117
|
+
* {@link formatSecp256k1PublicKey}: what to write into configuration and
|
|
118
|
+
* print, so one spelling reaches every derivation.
|
|
119
|
+
*
|
|
120
|
+
* @param value - The public key in any accepted spelling.
|
|
121
|
+
* @returns The same key as `0x04…` uncompressed SEC1 hex, lowercase.
|
|
122
|
+
* @throws {Error} If the value is not a valid secp256k1 public key in any accepted spelling.
|
|
123
|
+
*/
|
|
124
|
+
export declare function normaliseSecp256k1PublicKey(value: string): string;
|
|
96
125
|
/**
|
|
97
126
|
* Derive the `Secp256k1Point` of a secret key: a convenience for tests and
|
|
98
127
|
* signers that hold key material as raw bytes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecdsa-attestation.d.ts","sourceRoot":"","sources":["../src/ecdsa-attestation.ts"],"names":[],"mappings":"AAeA,OAAO,EACL,KAAK,cAAc,EAGpB,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"ecdsa-attestation.d.ts","sourceRoot":"","sources":["../src/ecdsa-attestation.ts"],"names":[],"mappings":"AAeA,OAAO,EACL,KAAK,cAAc,EAGpB,MAAM,iCAAiC,CAAC;A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;;;;;;;;;;;GAWG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAOrE;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,cAAc,GAAG,MAAM,CAGtE;AAED;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,UAAU,GAAG,cAAc,CAO1E;AAED;;;;;;;;;GASG;AACH,wBAAgB,gCAAgC,CAC9C,SAAS,EAAE,SAAS,EACpB,gBAAgB,EAAE,UAAU,GAC3B,UAAU,CAOZ;AAgBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uCAAuC,CACrD,KAAK,EAAE,yBAAyB,GAC/B,yBAAyB,CAQ3B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mCAAmC,CACjD,SAAS,EAAE,SAAS,EACpB,gBAAgB,EAAE,UAAU,EAC5B,KAAK,EAAE,yBAAyB,EAChC,cAAc,EAAE,cAAc,GAC7B,OAAO,CAyBT"}
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// kept here until upstreamed.
|
|
15
15
|
import { transientHash, upgradeFromTransient, } from "@midnight-ntwrk/compact-runtime";
|
|
16
16
|
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
17
|
-
import { Signature, toBeHex } from "ethers";
|
|
17
|
+
import { decodeBase58, Signature, toBeHex } from "ethers";
|
|
18
18
|
import { bigintToBytes32BE, bytesToBigintBE, stripHexPrefix } from "./byte-codecs.js";
|
|
19
19
|
import { attestationPreimageDescriptor } from "./compact-descriptors.js";
|
|
20
20
|
/** secp256k1 curve (group) order n. */
|
|
@@ -139,27 +139,86 @@ export function signatureToSignatureRespondedEvent(signature) {
|
|
|
139
139
|
}),
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
|
+
// The NEAR protocol's public-key string form: `secp256k1:` followed by base58
|
|
143
|
+
// of the raw 64-byte X||Y point, with no SEC1 prefix byte and no checksum.
|
|
144
|
+
// It is the spelling signet.js publishes the MPC root keys in so every key entry
|
|
145
|
+
// point here accepts it.
|
|
146
|
+
const NEAR_SECP256K1_PREFIX = "secp256k1:";
|
|
147
|
+
const SECP256K1_POINT_BYTES = 64;
|
|
142
148
|
/**
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
149
|
+
* The SEC1 hex (no `0x`) of a public key given in any accepted spelling:
|
|
150
|
+
* NEAR's `secp256k1:<base58>` becomes `04` followed by its 64-byte point,
|
|
151
|
+
* and hex loses its optional `0x`. Shape only: the curve check is the
|
|
152
|
+
* parser's.
|
|
146
153
|
*
|
|
147
|
-
* @param value - The SEC1 hex
|
|
148
|
-
* @returns The
|
|
149
|
-
* @throws {Error} If the value is
|
|
154
|
+
* @param value - The key in NEAR or SEC1 hex spelling.
|
|
155
|
+
* @returns The SEC1 hex without a `0x` prefix.
|
|
156
|
+
* @throws {Error} If the value is blank, its base58 does not decode, or the
|
|
157
|
+
* decoded point is wider than 64 bytes.
|
|
150
158
|
*/
|
|
151
|
-
|
|
152
|
-
const
|
|
159
|
+
function publicKeyToSec1Hex(value) {
|
|
160
|
+
const trimmed = value.trim();
|
|
161
|
+
if (trimmed === "") {
|
|
162
|
+
throw new Error("secp256k1 public key is blank");
|
|
163
|
+
}
|
|
164
|
+
if (!trimmed.startsWith(NEAR_SECP256K1_PREFIX)) {
|
|
165
|
+
return stripHexPrefix(trimmed);
|
|
166
|
+
}
|
|
167
|
+
const base58Point = trimmed.slice(NEAR_SECP256K1_PREFIX.length);
|
|
153
168
|
let point;
|
|
154
169
|
try {
|
|
155
|
-
point =
|
|
170
|
+
point = decodeBase58(base58Point);
|
|
156
171
|
}
|
|
157
172
|
catch (error) {
|
|
158
|
-
throw new Error(`
|
|
173
|
+
throw new Error(`"${trimmed}" is not base58 after its secp256k1: prefix (${String(error)})`, {
|
|
159
174
|
cause: error,
|
|
160
175
|
});
|
|
161
176
|
}
|
|
162
|
-
|
|
177
|
+
// Base58 folds leading zero bytes into leading `1` characters, so the point
|
|
178
|
+
// is rendered back at its fixed width, never at the integer's own width.
|
|
179
|
+
let pointHex;
|
|
180
|
+
try {
|
|
181
|
+
pointHex = toBeHex(point, SECP256K1_POINT_BYTES);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
throw new Error(`"${trimmed}" decodes wider than the ${String(SECP256K1_POINT_BYTES)}-byte point a NEAR secp256k1 key carries`, { cause: error });
|
|
185
|
+
}
|
|
186
|
+
return `04${pointHex.slice(2)}`;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Parse a secp256k1 public key in any accepted spelling (see
|
|
190
|
+
* {@link parseSecp256k1PublicKey}) into noble's point, the shape the
|
|
191
|
+
* derivation arithmetic runs on. Package-internal: the public surface hands
|
|
192
|
+
* out the Compact runtime's `Secp256k1Point` instead.
|
|
193
|
+
*
|
|
194
|
+
* @param value - The public key, NEAR or SEC1 hex spelling.
|
|
195
|
+
* @returns The point on secp256k1.
|
|
196
|
+
* @throws {Error} If the value is not a valid secp256k1 public key.
|
|
197
|
+
*/
|
|
198
|
+
export function parseSecp256k1PublicKeyToNoblePoint(value) {
|
|
199
|
+
const hex = publicKeyToSec1Hex(value);
|
|
200
|
+
try {
|
|
201
|
+
return secp256k1.Point.fromHex(hex);
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
throw new Error(`not a secp256k1 public key in SEC1 hex (02/03/04-prefixed, 0x optional) or NEAR ` +
|
|
205
|
+
`secp256k1:<base58> form: "${value}" (${String(error)})`, { cause: error });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Parse a secp256k1 public key into the Compact runtime's `Secp256k1Point`
|
|
210
|
+
* shape: how deploys receive the MPC response key to pin. Accepts every
|
|
211
|
+
* spelling a key is published in: SEC1 hex, compressed (`02`/`03`) or
|
|
212
|
+
* uncompressed (`04`) with an optional `0x`, and NEAR's `secp256k1:<base58>`
|
|
213
|
+
* (what signet.js and the MPC operators publish). The point is checked to
|
|
214
|
+
* lie on the curve.
|
|
215
|
+
*
|
|
216
|
+
* @param value - The public key in any accepted spelling.
|
|
217
|
+
* @returns The parsed point.
|
|
218
|
+
* @throws {Error} If the value is not a valid secp256k1 public key in any accepted spelling.
|
|
219
|
+
*/
|
|
220
|
+
export function parseSecp256k1PublicKey(value) {
|
|
221
|
+
const uncompressed = parseSecp256k1PublicKeyToNoblePoint(value).toBytes(false); // 0x04 || x || y
|
|
163
222
|
return {
|
|
164
223
|
x: bytesToBigintBE(uncompressed.slice(1, 33)),
|
|
165
224
|
y: bytesToBigintBE(uncompressed.slice(33, 65)),
|
|
@@ -167,9 +226,10 @@ export function parseSecp256k1PublicKey(value) {
|
|
|
167
226
|
};
|
|
168
227
|
}
|
|
169
228
|
/**
|
|
170
|
-
* Format a secp256k1 public key point as uncompressed SEC1 hex
|
|
171
|
-
* prefix
|
|
172
|
-
*
|
|
229
|
+
* Format a secp256k1 public key point as uncompressed SEC1 hex with a `0x`
|
|
230
|
+
* prefix, the canonical spelling this package publishes and prints keys in:
|
|
231
|
+
* the round-trip inverse of {@link parseSecp256k1PublicKey}, for handing a
|
|
232
|
+
* key to deploys via env/config.
|
|
173
233
|
*
|
|
174
234
|
* @param point - The point to format.
|
|
175
235
|
* @returns The `0x04…` uncompressed SEC1 hex string.
|
|
@@ -178,6 +238,19 @@ export function formatSecp256k1PublicKey(point) {
|
|
|
178
238
|
const toBE32 = (v) => v.toString(16).padStart(64, "0");
|
|
179
239
|
return `0x04${toBE32(point.x)}${toBE32(point.y)}`;
|
|
180
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Canonicalise a public key given in any accepted spelling (see
|
|
243
|
+
* {@link parseSecp256k1PublicKey}) to the `0x04…` uncompressed SEC1 hex of
|
|
244
|
+
* {@link formatSecp256k1PublicKey}: what to write into configuration and
|
|
245
|
+
* print, so one spelling reaches every derivation.
|
|
246
|
+
*
|
|
247
|
+
* @param value - The public key in any accepted spelling.
|
|
248
|
+
* @returns The same key as `0x04…` uncompressed SEC1 hex, lowercase.
|
|
249
|
+
* @throws {Error} If the value is not a valid secp256k1 public key in any accepted spelling.
|
|
250
|
+
*/
|
|
251
|
+
export function normaliseSecp256k1PublicKey(value) {
|
|
252
|
+
return formatSecp256k1PublicKey(parseSecp256k1PublicKey(value));
|
|
253
|
+
}
|
|
181
254
|
/**
|
|
182
255
|
* Derive the `Secp256k1Point` of a secret key: a convenience for tests and
|
|
183
256
|
* signers that hold key material as raw bytes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecdsa-attestation.js","sourceRoot":"","sources":["../src/ecdsa-attestation.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,0EAA0E;AAC1E,sEAAsE;AACtE,+CAA+C;AAC/C,kEAAkE;AAClE,2EAA2E;AAC3E,oEAAoE;AACpE,8BAA8B;AAC9B,oEAAoE;AACpE,4DAA4D;AAC5D,oCAAoC;AACpC,EAAE;AACF,wEAAwE;AACxE,8BAA8B;AAE9B,OAAO,EAEL,aAAa,EACb,oBAAoB,GACrB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"ecdsa-attestation.js","sourceRoot":"","sources":["../src/ecdsa-attestation.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,0EAA0E;AAC1E,sEAAsE;AACtE,+CAA+C;AAC/C,kEAAkE;AAClE,2EAA2E;AAC3E,oEAAoE;AACpE,8BAA8B;AAC9B,oEAAoE;AACpE,4DAA4D;AAC5D,oCAAoC;AACpC,EAAE;AACF,wEAAwE;AACxE,8BAA8B;AAE9B,OAAO,EAEL,aAAa,EACb,oBAAoB,GACrB,M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KAAK;SACZ,CAAC,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { type Secp256k1Point } from "./ecdsa-attestation.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Domain prefix of the sig-net v2.0.0 epsilon derivation scheme. The full
|
|
4
|
-
* derivation string is `<prefix
|
|
5
|
-
* matching `caip2_derivation_path` in the MPC's
|
|
4
|
+
* derivation string is `<prefix>:midnight:mainnet:<requester>:<path>`
|
|
5
|
+
* (colon-separated), matching `caip2_derivation_path` in the MPC's
|
|
6
|
+
* `signet-crypto/src/kdf.rs`.
|
|
6
7
|
*/
|
|
7
8
|
export declare const EPSILON_DERIVATION_PREFIX = "sig.network v2.0.0 epsilon derivation";
|
|
8
9
|
/**
|
|
9
|
-
* CAIP-2 chain id
|
|
10
|
-
*
|
|
10
|
+
* CAIP-2 chain id component of every Midnight derivation string. The MPC
|
|
11
|
+
* renders `Chain::Midnight` as this one literal for every Midnight network
|
|
12
|
+
* (sig-net/mpc signet-primitives/src/chain.rs), so it never varies with the
|
|
13
|
+
* network a contract is deployed on.
|
|
11
14
|
*/
|
|
12
|
-
export declare const
|
|
15
|
+
export declare const MIDNIGHT_CAIP2_ID = "midnight:mainnet";
|
|
13
16
|
/**
|
|
14
17
|
* The FIXED derivation path of the MPC's respond-bidirectional RESPONSE key
|
|
15
18
|
* for Midnight client contracts. It enters the derivation string verbatim,
|
|
@@ -25,12 +28,13 @@ export declare const MIDNIGHT_RESPOND_BIDIRECTIONAL_PATH = "midnight response ke
|
|
|
25
28
|
/**
|
|
26
29
|
* Derive the EVM address the MPC network signs from for a given Midnight
|
|
27
30
|
* contract and derivation path, using the sig-net v2.0.0 epsilon scheme:
|
|
28
|
-
* `epsilon = keccak256("<prefix
|
|
31
|
+
* `epsilon = keccak256("<prefix>:midnight:mainnet:<requester>:<path>")` and
|
|
29
32
|
* `derivedPubKey = mpcRootPubKey + epsilon * G` on secp256k1. The MPC
|
|
30
33
|
* treats `path` as an opaque string.
|
|
31
34
|
*
|
|
32
|
-
* @param
|
|
33
|
-
* (
|
|
35
|
+
* @param mpcSecp256k1PublicKey - The MPC root secp256k1 public key, in any
|
|
36
|
+
* spelling `parseSecp256k1PublicKey` accepts (SEC1 hex or NEAR
|
|
37
|
+
* `secp256k1:<base58>`).
|
|
34
38
|
* @param contractAddress - The Midnight contract address the request
|
|
35
39
|
* originates from (`0x` prefix optional, case-insensitive: it enters the
|
|
36
40
|
* derivation string through {@link normaliseRequesterAddress}).
|
|
@@ -39,36 +43,36 @@ export declare const MIDNIGHT_RESPOND_BIDIRECTIONAL_PATH = "midnight response ke
|
|
|
39
43
|
* `path: Bytes<32>`: the lowercase hex of the FULL 32 bytes, no `0x`
|
|
40
44
|
* prefix and no trimming ({@link bytesToHex} of the raw bytes), so
|
|
41
45
|
* `0xab..00` and `0xab..` derive different accounts.
|
|
42
|
-
* @param chainId - CAIP-2 chain id component of the derivation string.
|
|
43
46
|
* @returns The derived EVM address as a 0x-prefixed EIP-55 checksummed string.
|
|
44
47
|
*/
|
|
45
|
-
export declare function deriveEvmAddress(
|
|
48
|
+
export declare function deriveEvmAddress(mpcSecp256k1PublicKey: string, contractAddress: string, path: string): string;
|
|
46
49
|
/**
|
|
47
50
|
* The epsilon scalar of the sig-net v2.0.0 derivation scheme:
|
|
48
|
-
* `keccak256("<prefix
|
|
49
|
-
* secp256k1 curve order
|
|
51
|
+
* `keccak256("<prefix>:midnight:mainnet:<requester>:<path>")` reduced mod
|
|
52
|
+
* the secp256k1 curve order, the chain id fixed to
|
|
53
|
+
* {@link MIDNIGHT_CAIP2_ID}. Child keys are `root + epsilon` (secret side) and
|
|
50
54
|
* `rootPubKey + epsilon * G` (public side).
|
|
51
55
|
*
|
|
52
56
|
* @param requester - The requester component of the derivation string,
|
|
53
57
|
* verbatim (no normalisation: callers must agree on the exact rendering).
|
|
54
58
|
* @param path - The derivation path string.
|
|
55
|
-
* @param chainId - CAIP-2 chain id component of the derivation string.
|
|
56
59
|
* @returns The epsilon scalar, in `[0, n)`.
|
|
57
60
|
*/
|
|
58
|
-
export declare function deriveEpsilon(requester: string, path: string
|
|
61
|
+
export declare function deriveEpsilon(requester: string, path: string): bigint;
|
|
59
62
|
/**
|
|
60
63
|
* Derive the MPC's respond-bidirectional RESPONSE key for one client
|
|
61
64
|
* contract, public side: what the client pins via its `initialise` circuit
|
|
62
65
|
* and what response verification checks against. See
|
|
63
66
|
* {@link MIDNIGHT_RESPOND_BIDIRECTIONAL_PATH} for the scheme.
|
|
64
67
|
*
|
|
65
|
-
* @param
|
|
66
|
-
* (
|
|
68
|
+
* @param mpcSecp256k1PublicKey - The MPC root secp256k1 public key, in any
|
|
69
|
+
* spelling `parseSecp256k1PublicKey` accepts (SEC1 hex or NEAR
|
|
70
|
+
* `secp256k1:<base58>`).
|
|
67
71
|
* @param clientContractAddress - The client contract's Midnight address
|
|
68
72
|
* (`0x` prefix optional, case-insensitive).
|
|
69
73
|
* @returns The response public key as a Compact-runtime `Secp256k1Point`.
|
|
70
74
|
*/
|
|
71
|
-
export declare function deriveMidnightResponseKey(
|
|
75
|
+
export declare function deriveMidnightResponseKey(mpcSecp256k1PublicKey: string, clientContractAddress: string): Secp256k1Point;
|
|
72
76
|
/**
|
|
73
77
|
* Derive the MPC's respond-bidirectional RESPONSE key for one client
|
|
74
78
|
* contract, secret side: `(rootSecret + epsilon) mod n`. MPC-side only
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"epsilon-derivation.d.ts","sourceRoot":"","sources":["../src/epsilon-derivation.ts"],"names":[],"mappings":"AAWA,OAAO,
|
|
1
|
+
{"version":3,"file":"epsilon-derivation.d.ts","sourceRoot":"","sources":["../src/epsilon-derivation.ts"],"names":[],"mappings":"AAWA,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,wBAAwB,CAAC;AAEhC;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,0CAA0C,CAAC;AAEjF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,qBAAqB,CAAC;AAEpD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mCAAmC,0BAA0B,CAAC;AAe3E;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAC9B,qBAAqB,EAAE,MAAM,EAC7B,eAAe,EAAE,MAAM,EACvB,IAAI,EAAE,MAAM,GACX,MAAM,CAOR;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAKrE;AAiBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,yBAAyB,CACvC,qBAAqB,EAAE,MAAM,EAC7B,qBAAqB,EAAE,MAAM,GAC5B,cAAc,CAOhB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,+BAA+B,CAC7C,gBAAgB,EAAE,UAAU,EAC5B,qBAAqB,EAAE,MAAM,GAC5B,UAAU,CAcZ"}
|
|
@@ -5,20 +5,23 @@
|
|
|
5
5
|
// v2.0.0 (COLON-separated) is the only scheme the MPC answers: the Compact
|
|
6
6
|
// contracts assert `keyVersion >= 1`, which selects v2.
|
|
7
7
|
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
8
|
-
import { computeAddress, keccak256,
|
|
8
|
+
import { computeAddress, keccak256, toUtf8Bytes } from "ethers";
|
|
9
9
|
import { bigintToBytes32BE, bytesToBigintBE, stripHexPrefix } from "./byte-codecs.js";
|
|
10
|
-
import { SECP256K1_ORDER } from "./ecdsa-attestation.js";
|
|
10
|
+
import { parseSecp256k1PublicKeyToNoblePoint, SECP256K1_ORDER, } from "./ecdsa-attestation.js";
|
|
11
11
|
/**
|
|
12
12
|
* Domain prefix of the sig-net v2.0.0 epsilon derivation scheme. The full
|
|
13
|
-
* derivation string is `<prefix
|
|
14
|
-
* matching `caip2_derivation_path` in the MPC's
|
|
13
|
+
* derivation string is `<prefix>:midnight:mainnet:<requester>:<path>`
|
|
14
|
+
* (colon-separated), matching `caip2_derivation_path` in the MPC's
|
|
15
|
+
* `signet-crypto/src/kdf.rs`.
|
|
15
16
|
*/
|
|
16
17
|
export const EPSILON_DERIVATION_PREFIX = "sig.network v2.0.0 epsilon derivation";
|
|
17
18
|
/**
|
|
18
|
-
* CAIP-2 chain id
|
|
19
|
-
*
|
|
19
|
+
* CAIP-2 chain id component of every Midnight derivation string. The MPC
|
|
20
|
+
* renders `Chain::Midnight` as this one literal for every Midnight network
|
|
21
|
+
* (sig-net/mpc signet-primitives/src/chain.rs), so it never varies with the
|
|
22
|
+
* network a contract is deployed on.
|
|
20
23
|
*/
|
|
21
|
-
export const
|
|
24
|
+
export const MIDNIGHT_CAIP2_ID = "midnight:mainnet";
|
|
22
25
|
/**
|
|
23
26
|
* The FIXED derivation path of the MPC's respond-bidirectional RESPONSE key
|
|
24
27
|
* for Midnight client contracts. It enters the derivation string verbatim,
|
|
@@ -46,12 +49,13 @@ function normaliseRequesterAddress(contractAddress) {
|
|
|
46
49
|
/**
|
|
47
50
|
* Derive the EVM address the MPC network signs from for a given Midnight
|
|
48
51
|
* contract and derivation path, using the sig-net v2.0.0 epsilon scheme:
|
|
49
|
-
* `epsilon = keccak256("<prefix
|
|
52
|
+
* `epsilon = keccak256("<prefix>:midnight:mainnet:<requester>:<path>")` and
|
|
50
53
|
* `derivedPubKey = mpcRootPubKey + epsilon * G` on secp256k1. The MPC
|
|
51
54
|
* treats `path` as an opaque string.
|
|
52
55
|
*
|
|
53
|
-
* @param
|
|
54
|
-
* (
|
|
56
|
+
* @param mpcSecp256k1PublicKey - The MPC root secp256k1 public key, in any
|
|
57
|
+
* spelling `parseSecp256k1PublicKey` accepts (SEC1 hex or NEAR
|
|
58
|
+
* `secp256k1:<base58>`).
|
|
55
59
|
* @param contractAddress - The Midnight contract address the request
|
|
56
60
|
* originates from (`0x` prefix optional, case-insensitive: it enters the
|
|
57
61
|
* derivation string through {@link normaliseRequesterAddress}).
|
|
@@ -60,27 +64,26 @@ function normaliseRequesterAddress(contractAddress) {
|
|
|
60
64
|
* `path: Bytes<32>`: the lowercase hex of the FULL 32 bytes, no `0x`
|
|
61
65
|
* prefix and no trimming ({@link bytesToHex} of the raw bytes), so
|
|
62
66
|
* `0xab..00` and `0xab..` derive different accounts.
|
|
63
|
-
* @param chainId - CAIP-2 chain id component of the derivation string.
|
|
64
67
|
* @returns The derived EVM address as a 0x-prefixed EIP-55 checksummed string.
|
|
65
68
|
*/
|
|
66
|
-
export function deriveEvmAddress(
|
|
67
|
-
const derivedPoint = deriveChildPoint(
|
|
69
|
+
export function deriveEvmAddress(mpcSecp256k1PublicKey, contractAddress, path) {
|
|
70
|
+
const derivedPoint = deriveChildPoint(mpcSecp256k1PublicKey, normaliseRequesterAddress(contractAddress), path);
|
|
68
71
|
return computeAddress(`0x${derivedPoint.toHex(false)}`);
|
|
69
72
|
}
|
|
70
73
|
/**
|
|
71
74
|
* The epsilon scalar of the sig-net v2.0.0 derivation scheme:
|
|
72
|
-
* `keccak256("<prefix
|
|
73
|
-
* secp256k1 curve order
|
|
75
|
+
* `keccak256("<prefix>:midnight:mainnet:<requester>:<path>")` reduced mod
|
|
76
|
+
* the secp256k1 curve order, the chain id fixed to
|
|
77
|
+
* {@link MIDNIGHT_CAIP2_ID}. Child keys are `root + epsilon` (secret side) and
|
|
74
78
|
* `rootPubKey + epsilon * G` (public side).
|
|
75
79
|
*
|
|
76
80
|
* @param requester - The requester component of the derivation string,
|
|
77
81
|
* verbatim (no normalisation: callers must agree on the exact rendering).
|
|
78
82
|
* @param path - The derivation path string.
|
|
79
|
-
* @param chainId - CAIP-2 chain id component of the derivation string.
|
|
80
83
|
* @returns The epsilon scalar, in `[0, n)`.
|
|
81
84
|
*/
|
|
82
|
-
export function deriveEpsilon(requester, path
|
|
83
|
-
const fullPath = `${EPSILON_DERIVATION_PREFIX}:${
|
|
85
|
+
export function deriveEpsilon(requester, path) {
|
|
86
|
+
const fullPath = `${EPSILON_DERIVATION_PREFIX}:${MIDNIGHT_CAIP2_ID}:${requester}:${path}`;
|
|
84
87
|
// Reduce mod n before using: noble throws on scalars >= n, whereas the
|
|
85
88
|
// server's scalar arithmetic reduces implicitly.
|
|
86
89
|
return BigInt(keccak256(toUtf8Bytes(fullPath))) % SECP256K1_ORDER;
|
|
@@ -88,16 +91,15 @@ export function deriveEpsilon(requester, path, chainId = MIDNIGHT_TESTNET_CHAIN_
|
|
|
88
91
|
/**
|
|
89
92
|
* Derive the child public key as a noble curve point (internal shape).
|
|
90
93
|
*
|
|
91
|
-
* @param
|
|
94
|
+
* @param mpcSecp256k1PublicKey - The MPC root public key, any spelling
|
|
95
|
+
* `parseSecp256k1PublicKey` accepts.
|
|
92
96
|
* @param requester - The normalised requester address.
|
|
93
97
|
* @param path - The derivation path component.
|
|
94
|
-
* @param chainId - The chain id component.
|
|
95
98
|
* @returns The derived child point on secp256k1.
|
|
96
99
|
*/
|
|
97
|
-
function deriveChildPoint(
|
|
98
|
-
const epsilon = deriveEpsilon(requester, path
|
|
99
|
-
const
|
|
100
|
-
const rootPoint = secp256k1.Point.fromHex(rootPubKeyHex.slice(2));
|
|
100
|
+
function deriveChildPoint(mpcSecp256k1PublicKey, requester, path) {
|
|
101
|
+
const epsilon = deriveEpsilon(requester, path);
|
|
102
|
+
const rootPoint = parseSecp256k1PublicKeyToNoblePoint(mpcSecp256k1PublicKey);
|
|
101
103
|
return epsilon === 0n ? rootPoint : rootPoint.add(secp256k1.Point.BASE.multiply(epsilon));
|
|
102
104
|
}
|
|
103
105
|
/**
|
|
@@ -106,14 +108,15 @@ function deriveChildPoint(mpcSecp256k1PubkeyHex, requester, path, chainId) {
|
|
|
106
108
|
* and what response verification checks against. See
|
|
107
109
|
* {@link MIDNIGHT_RESPOND_BIDIRECTIONAL_PATH} for the scheme.
|
|
108
110
|
*
|
|
109
|
-
* @param
|
|
110
|
-
* (
|
|
111
|
+
* @param mpcSecp256k1PublicKey - The MPC root secp256k1 public key, in any
|
|
112
|
+
* spelling `parseSecp256k1PublicKey` accepts (SEC1 hex or NEAR
|
|
113
|
+
* `secp256k1:<base58>`).
|
|
111
114
|
* @param clientContractAddress - The client contract's Midnight address
|
|
112
115
|
* (`0x` prefix optional, case-insensitive).
|
|
113
116
|
* @returns The response public key as a Compact-runtime `Secp256k1Point`.
|
|
114
117
|
*/
|
|
115
|
-
export function deriveMidnightResponseKey(
|
|
116
|
-
const point = deriveChildPoint(
|
|
118
|
+
export function deriveMidnightResponseKey(mpcSecp256k1PublicKey, clientContractAddress) {
|
|
119
|
+
const point = deriveChildPoint(mpcSecp256k1PublicKey, normaliseRequesterAddress(clientContractAddress), MIDNIGHT_RESPOND_BIDIRECTIONAL_PATH);
|
|
117
120
|
return { x: point.x, y: point.y, identity: false };
|
|
118
121
|
}
|
|
119
122
|
/**
|
|
@@ -133,7 +136,7 @@ export function deriveMidnightResponseSecretKey(mpcRootSecretKey, clientContract
|
|
|
133
136
|
throw new Error(`MPC root secret key must be 32 bytes, got ${String(mpcRootSecretKey.length)}`);
|
|
134
137
|
}
|
|
135
138
|
const root = bytesToBigintBE(mpcRootSecretKey);
|
|
136
|
-
const epsilon = deriveEpsilon(normaliseRequesterAddress(clientContractAddress), MIDNIGHT_RESPOND_BIDIRECTIONAL_PATH
|
|
139
|
+
const epsilon = deriveEpsilon(normaliseRequesterAddress(clientContractAddress), MIDNIGHT_RESPOND_BIDIRECTIONAL_PATH);
|
|
137
140
|
const child = (root + epsilon) % SECP256K1_ORDER;
|
|
138
141
|
if (child === 0n) {
|
|
139
142
|
throw new Error("derived response secret key is zero (invalid scalar)");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"epsilon-derivation.js","sourceRoot":"","sources":["../src/epsilon-derivation.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,EAAE;AACF,4EAA4E;AAC5E,EAAE;AACF,2EAA2E;AAC3E,wDAAwD;AAExD,OAAO,EAAE,SAAS,E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