@ocap/mcrypto 1.31.0 → 1.31.2
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/esm/bridge-governance.d.mts +88 -0
- package/esm/bridge-governance.mjs +131 -0
- package/esm/envelope.d.mts +42 -2
- package/esm/envelope.mjs +105 -9
- package/esm/eth-address.d.mts +27 -0
- package/esm/eth-address.mjs +51 -0
- package/esm/hasher/keccak.mjs +1 -1
- package/esm/hasher/sha2.mjs +1 -1
- package/esm/hasher/sha3.mjs +1 -1
- package/esm/index.d.mts +6 -3
- package/esm/index.mjs +5 -2
- package/esm/signer/ed25519.mjs +1 -1
- package/esm/signer/ethereum.mjs +1 -1
- package/esm/signer/secp256k1.mjs +1 -1
- package/lib/bridge-governance.cjs +144 -0
- package/lib/bridge-governance.d.cts +88 -0
- package/lib/envelope.cjs +105 -7
- package/lib/envelope.d.cts +42 -2
- package/lib/eth-address.cjs +52 -0
- package/lib/eth-address.d.cts +27 -0
- package/lib/hasher/keccak.cjs +1 -1
- package/lib/hasher/sha2.cjs +1 -1
- package/lib/hasher/sha3.cjs +1 -1
- package/lib/index.cjs +20 -0
- package/lib/index.d.cts +6 -3
- package/lib/signer/ed25519.cjs +1 -1
- package/lib/signer/secp256k1.cjs +1 -1
- package/package.json +2 -2
package/esm/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { BRIDGE_GOV_DOMAIN_NAME, BRIDGE_GOV_DOMAIN_VERSION, BRIDGE_MEMBERSHIP_ACTION, BRIDGE_MEMBERSHIP_TYPE_STRING, BRIDGE_OWNER_ATTESTATION_TYPE_STRING, bridgeEvidenceKey, computeBridgeEndpointsHash, computeBridgeMembershipDigest, computeBridgeOwnerAttestationDigest, ethAddressOfPublicKey, recoverBridgeSigner, signBridgeDigest, toHexDigest } from "./bridge-governance.mjs";
|
|
1
2
|
import { encode } from "./encode.mjs";
|
|
2
|
-
import {
|
|
3
|
+
import { isStrictEthAddress, parseStrictEthAddress } from "./eth-address.mjs";
|
|
4
|
+
import { EIP712_BRIDGE_WITHDRAW_TYPE_STRING, assertEnvelopeMatchesTxContext, buildBridgeWithdrawEnvelopeExtra, buildEd25519EnvelopePayload, buildSolanaOffchainEnvelopeBytes, computeEip191EnvelopeDigest, computeEip712EnvelopeDigest, envelope_exports, formatWalletEnvelopeText, isCanonicalWalletEnvelopeExtra, normalizeChallengeHex, parseWalletEnvelopeExtra, signEthereumEnvelope, verifyEthereumEnvelope } from "./envelope.mjs";
|
|
3
5
|
import keccak_default from "./hasher/keccak.mjs";
|
|
4
6
|
import sha2_default, { hmacSha256 } from "./hasher/sha2.mjs";
|
|
5
7
|
import sha3_default from "./hasher/sha3.mjs";
|
|
@@ -110,6 +112,7 @@ const types = {
|
|
|
110
112
|
ROLE_PASSKEY: 22,
|
|
111
113
|
ROLE_TOKEN_FACTORY: 23,
|
|
112
114
|
ROLE_PROVIDER: 24,
|
|
115
|
+
ROLE_BRIDGE_RESERVE: 25,
|
|
113
116
|
ROLE_ANY: 63
|
|
114
117
|
},
|
|
115
118
|
EncodingType: {
|
|
@@ -183,4 +186,4 @@ function getRandomBytes(length = 32, encoding = "hex") {
|
|
|
183
186
|
const toTxHash = (buf) => Hasher.SHA2.hash256(buf, 1, "hex").replace(/^0x/, "").toUpperCase();
|
|
184
187
|
|
|
185
188
|
//#endregion
|
|
186
|
-
export { Hasher, Signer, assertEnvelopeMatchesTxContext, buildEd25519EnvelopePayload, buildSolanaOffchainEnvelopeBytes, computeEip191EnvelopeDigest, computeEip712EnvelopeDigest, envelope_exports as envelope, formatWalletEnvelopeText, getHasher, getRandomBytes, getSigner, hmacSha256, isCanonicalWalletEnvelopeExtra, normalizeChallengeHex, parseWalletEnvelopeExtra, signEthereumEnvelope, toTxHash, types, verifyEthereumEnvelope, webauthn_exports as webauthn };
|
|
189
|
+
export { BRIDGE_GOV_DOMAIN_NAME, BRIDGE_GOV_DOMAIN_VERSION, BRIDGE_MEMBERSHIP_ACTION, BRIDGE_MEMBERSHIP_TYPE_STRING, BRIDGE_OWNER_ATTESTATION_TYPE_STRING, EIP712_BRIDGE_WITHDRAW_TYPE_STRING, Hasher, Signer, assertEnvelopeMatchesTxContext, bridgeEvidenceKey, buildBridgeWithdrawEnvelopeExtra, buildEd25519EnvelopePayload, buildSolanaOffchainEnvelopeBytes, computeBridgeEndpointsHash, computeBridgeMembershipDigest, computeBridgeOwnerAttestationDigest, computeEip191EnvelopeDigest, computeEip712EnvelopeDigest, envelope_exports as envelope, ethAddressOfPublicKey, formatWalletEnvelopeText, getHasher, getRandomBytes, getSigner, hmacSha256, isCanonicalWalletEnvelopeExtra, isStrictEthAddress, normalizeChallengeHex, parseStrictEthAddress, parseWalletEnvelopeExtra, recoverBridgeSigner, signBridgeDigest, signEthereumEnvelope, toHexDigest, toTxHash, types, verifyEthereumEnvelope, webauthn_exports as webauthn };
|
package/esm/signer/ed25519.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { encode } from "../encode.mjs";
|
|
2
2
|
import { buildEd25519EnvelopePayload } from "../envelope.mjs";
|
|
3
3
|
import signer_default from "../protocols/signer.mjs";
|
|
4
|
-
import { toUint8Array } from "@ocap/util";
|
|
5
4
|
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
5
|
+
import { toUint8Array } from "@ocap/util";
|
|
6
6
|
import randomBytes from "randombytes";
|
|
7
7
|
import { sha512 } from "@noble/hashes/sha2.js";
|
|
8
8
|
import * as ed from "@noble/ed25519";
|
package/esm/signer/ethereum.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Secp256k1Signer } from "./secp256k1.mjs";
|
|
2
|
-
import { hexToBytes, isHexStrict, utf8ToHex } from "@ocap/util";
|
|
3
2
|
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
4
3
|
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
5
4
|
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
5
|
+
import { hexToBytes, isHexStrict, utf8ToHex } from "@ocap/util";
|
|
6
6
|
|
|
7
7
|
//#region src/signer/ethereum.ts
|
|
8
8
|
function toChecksumAddress(address) {
|
package/esm/signer/secp256k1.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { encode } from "../encode.mjs";
|
|
2
2
|
import { verifyEthereumEnvelope } from "../envelope.mjs";
|
|
3
3
|
import signer_default from "../protocols/signer.mjs";
|
|
4
|
-
import { toHex, toUint8Array } from "@ocap/util";
|
|
5
4
|
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
6
5
|
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
6
|
+
import { toHex, toUint8Array } from "@ocap/util";
|
|
7
7
|
import randomBytes from "randombytes";
|
|
8
8
|
|
|
9
9
|
//#region src/signer/secp256k1.ts
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_utils = require('./_vendor/@noble/hashes/utils.cjs');
|
|
3
|
+
const require_secp256k1 = require('./_vendor/@noble/curves/secp256k1.cjs');
|
|
4
|
+
const require_sha3 = require('./_vendor/@noble/hashes/sha3.cjs');
|
|
5
|
+
let _ocap_util = require("@ocap/util");
|
|
6
|
+
|
|
7
|
+
//#region src/bridge-governance.ts
|
|
8
|
+
/**
|
|
9
|
+
* EIP-712 structs the on-chain bridge governance (bridge/docs/ocap-protocols.md §v2.2) verifies on the OCAP side:
|
|
10
|
+
* `BridgeMembership` (operator consent / join / leave) and `BridgeOwnerAttestation` (Ethereum owner proof).
|
|
11
|
+
*
|
|
12
|
+
* This is a port of `membership.ts` in `@arcblock/bridge-types` (bridge/types/src/membership.ts). Core packages cannot
|
|
13
|
+
* depend on the private bridge workspace, so the logic is duplicated on purpose and a parity test
|
|
14
|
+
* (core/mcrypto/tests/bridge-governance.spec.ts) runs this port against the vector file
|
|
15
|
+
* bridge/types/vectors/membership.json, which is generated from viem (what wallets run). Change both together.
|
|
16
|
+
*
|
|
17
|
+
* Domain: `{ name: "ArcBridgeOcap", version: "1" }` with NO chainId / verifyingContract; the OCAP chain and the bridge
|
|
18
|
+
* are carried inside the struct (`ocapChainId`, `bridge`), so an approval cannot be replayed across chains or bridges.
|
|
19
|
+
*/
|
|
20
|
+
const BRIDGE_GOV_DOMAIN_NAME = "ArcBridgeOcap";
|
|
21
|
+
const BRIDGE_GOV_DOMAIN_VERSION = "1";
|
|
22
|
+
const BRIDGE_MEMBERSHIP_TYPE_STRING = "BridgeMembership(string ocapChainId,string bridge,uint64 epoch,address[] validators,uint32 threshold,uint64 activeFrom,uint64 voucherValidity,bytes32 endpointsHash,uint8 action)";
|
|
23
|
+
const BRIDGE_OWNER_ATTESTATION_TYPE_STRING = "BridgeOwnerAttestation(string ocapChainId,bytes32 salt,string ocapToken,uint256 ethChainId,address gateway,address ethToken)";
|
|
24
|
+
/** `action` of a BridgeMembership: CREATE = epoch 1 consent, JOIN / LEAVE = epoch change. */
|
|
25
|
+
const BRIDGE_MEMBERSHIP_ACTION = {
|
|
26
|
+
CREATE: 0,
|
|
27
|
+
JOIN: 1,
|
|
28
|
+
LEAVE: 2
|
|
29
|
+
};
|
|
30
|
+
const utf8 = (s) => new Uint8Array(Buffer.from(s, "utf8"));
|
|
31
|
+
const keccakUtf8 = (s) => require_sha3.keccak_256(utf8(s));
|
|
32
|
+
const concat = (...parts) => new Uint8Array(Buffer.concat(parts));
|
|
33
|
+
const DOMAIN_TYPEHASH = keccakUtf8("EIP712Domain(string name,string version)");
|
|
34
|
+
const MEMBERSHIP_TYPEHASH = keccakUtf8(BRIDGE_MEMBERSHIP_TYPE_STRING);
|
|
35
|
+
const ATTESTATION_TYPEHASH = keccakUtf8(BRIDGE_OWNER_ATTESTATION_TYPE_STRING);
|
|
36
|
+
const DOMAIN_SEPARATOR = require_sha3.keccak_256(concat(DOMAIN_TYPEHASH, keccakUtf8(BRIDGE_GOV_DOMAIN_NAME), keccakUtf8(BRIDGE_GOV_DOMAIN_VERSION)));
|
|
37
|
+
function uintWord(value, bits, name) {
|
|
38
|
+
let n;
|
|
39
|
+
try {
|
|
40
|
+
n = typeof value === "bigint" ? value : BigInt(value);
|
|
41
|
+
} catch {
|
|
42
|
+
throw new Error(`${name} is not an integer`);
|
|
43
|
+
}
|
|
44
|
+
if (n < 0n || n >= 1n << BigInt(bits)) throw new Error(`${name} does not fit uint${bits}`);
|
|
45
|
+
return new Uint8Array(Buffer.from(n.toString(16).padStart(64, "0"), "hex"));
|
|
46
|
+
}
|
|
47
|
+
function addressWord(value, name) {
|
|
48
|
+
if (typeof value !== "string" || !/^0x[0-9a-fA-F]{40}$/.test(value)) throw new Error(`${name} is not an address`);
|
|
49
|
+
return new Uint8Array(Buffer.from(value.slice(2).toLowerCase().padStart(64, "0"), "hex"));
|
|
50
|
+
}
|
|
51
|
+
function bytes32Word(value, name) {
|
|
52
|
+
if (typeof value !== "string" || !/^0x[0-9a-fA-F]{64}$/.test(value)) throw new Error(`${name} is not a bytes32`);
|
|
53
|
+
return new Uint8Array(Buffer.from(value.slice(2), "hex"));
|
|
54
|
+
}
|
|
55
|
+
function digestOf(structHash) {
|
|
56
|
+
return require_sha3.keccak_256(concat(new Uint8Array([25, 1]), DOMAIN_SEPARATOR, structHash));
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* `keccak256(keccak256(endpoint_0) || keccak256(endpoint_1) || ...)` over the endpoints of the validators in the order
|
|
60
|
+
* of the signed `validators` array. It binds every listed operator to the URL it is reachable at, so a creator cannot
|
|
61
|
+
* pair a victim operator with another URL.
|
|
62
|
+
*/
|
|
63
|
+
function computeBridgeEndpointsHash(endpoints) {
|
|
64
|
+
if (!Array.isArray(endpoints)) throw new Error("endpoints is not an array");
|
|
65
|
+
return `0x${require_utils.bytesToHex(require_sha3.keccak_256(concat(...endpoints.map((e) => keccakUtf8(String(e))))))}`;
|
|
66
|
+
}
|
|
67
|
+
function computeBridgeMembershipDigest(m) {
|
|
68
|
+
if (!Array.isArray(m.validators)) throw new Error("validators is not an array");
|
|
69
|
+
const validators = require_sha3.keccak_256(concat(...m.validators.map((a, i) => addressWord(a, `validators[${i}]`))));
|
|
70
|
+
return digestOf(require_sha3.keccak_256(concat(MEMBERSHIP_TYPEHASH, keccakUtf8(m.ocapChainId), keccakUtf8(m.bridge), uintWord(m.epoch, 64, "epoch"), validators, uintWord(m.threshold, 32, "threshold"), uintWord(m.activeFrom, 64, "activeFrom"), uintWord(m.voucherValidity, 64, "voucherValidity"), bytes32Word(m.endpointsHash, "endpointsHash"), uintWord(m.action, 8, "action"))));
|
|
71
|
+
}
|
|
72
|
+
function computeBridgeOwnerAttestationDigest(m) {
|
|
73
|
+
return digestOf(require_sha3.keccak_256(concat(ATTESTATION_TYPEHASH, keccakUtf8(m.ocapChainId), bytes32Word(m.salt, "salt"), keccakUtf8(m.ocapToken), uintWord(m.ethChainId, 256, "ethChainId"), addressWord(m.gateway, "gateway"), addressWord(m.ethToken, "ethToken"))));
|
|
74
|
+
}
|
|
75
|
+
/** Ethereum address (lowercase 0x) of an uncompressed (64/65 bytes) or compressed secp256k1 public key. */
|
|
76
|
+
function ethAddressOfPublicKey(pk) {
|
|
77
|
+
const bytes = (0, _ocap_util.toUint8Array)(pk);
|
|
78
|
+
return `0x${require_utils.bytesToHex(require_sha3.keccak_256(bytes.byteLength === 64 ? bytes : bytes.byteLength === 65 ? bytes.slice(1) : require_secp256k1.secp256k1.Point.fromBytes(bytes).toBytes(false).slice(1)).slice(12))}`;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Recover the signer of a canonical 65-byte `r || s || v` signature (`v` in {27, 28}, low-S). Returns null on anything
|
|
82
|
+
* else, so a malleable or malformed signature never recovers to an address.
|
|
83
|
+
*/
|
|
84
|
+
function recoverBridgeSigner(digest, signature) {
|
|
85
|
+
if (typeof signature !== "string" || !/^0x[0-9a-fA-F]{130}$/.test(signature)) return null;
|
|
86
|
+
const hex = signature.slice(2);
|
|
87
|
+
const v = Number.parseInt(hex.slice(128, 130), 16);
|
|
88
|
+
if (v !== 27 && v !== 28) return null;
|
|
89
|
+
try {
|
|
90
|
+
const sig = require_secp256k1.secp256k1.Signature.fromHex(hex.slice(0, 128), "compact").addRecoveryBit(v - 27);
|
|
91
|
+
if (sig.hasHighS()) return null;
|
|
92
|
+
const publicKey = sig.recoverPublicKey(digest).toBytes(false);
|
|
93
|
+
return {
|
|
94
|
+
address: ethAddressOfPublicKey(publicKey),
|
|
95
|
+
publicKey
|
|
96
|
+
};
|
|
97
|
+
} catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** Sign a governance digest with a secp256k1 secret key; returns the 65-byte `0x{r}{s}{v}` signature. */
|
|
102
|
+
function signBridgeDigest(digest, sk) {
|
|
103
|
+
const recovered = require_secp256k1.secp256k1.sign(digest, (0, _ocap_util.toUint8Array)(sk), {
|
|
104
|
+
prehash: false,
|
|
105
|
+
format: "recovered"
|
|
106
|
+
});
|
|
107
|
+
const sig = require_secp256k1.secp256k1.Signature.fromBytes(recovered, "recovered");
|
|
108
|
+
const v = (27 + sig.recovery).toString(16).padStart(2, "0");
|
|
109
|
+
return `0x${sig.toHex("compact")}${v}`;
|
|
110
|
+
}
|
|
111
|
+
const toHexDigest = (digest) => `0x${require_utils.bytesToHex(digest)}`;
|
|
112
|
+
/**
|
|
113
|
+
* keccak256 of `utf8(prefix) || words`: the key preimage helper for the `arc-bridge:*` evidence keys. Every dynamic part
|
|
114
|
+
* is a fixed-width word (uint256 / address / bytes32) or the trailing DID string, so two different tuples never share a
|
|
115
|
+
* preimage. Returns a `0x` + 64 hex string (the evidence table key shape).
|
|
116
|
+
*/
|
|
117
|
+
function bridgeEvidenceKey(prefix, parts) {
|
|
118
|
+
const chunks = [utf8(prefix)];
|
|
119
|
+
parts.forEach((p, i) => {
|
|
120
|
+
if ("uint" in p) chunks.push(uintWord(p.uint, 256, `part[${i}]`));
|
|
121
|
+
else if ("address" in p) chunks.push(addressWord(p.address, `part[${i}]`));
|
|
122
|
+
else if ("bytes32" in p) chunks.push(bytes32Word(p.bytes32, `part[${i}]`));
|
|
123
|
+
else {
|
|
124
|
+
if (i !== parts.length - 1) throw new Error("text part must be last");
|
|
125
|
+
chunks.push(utf8(p.text));
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return `0x${require_utils.bytesToHex(require_sha3.keccak_256(concat(...chunks)))}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
exports.BRIDGE_GOV_DOMAIN_NAME = BRIDGE_GOV_DOMAIN_NAME;
|
|
133
|
+
exports.BRIDGE_GOV_DOMAIN_VERSION = BRIDGE_GOV_DOMAIN_VERSION;
|
|
134
|
+
exports.BRIDGE_MEMBERSHIP_ACTION = BRIDGE_MEMBERSHIP_ACTION;
|
|
135
|
+
exports.BRIDGE_MEMBERSHIP_TYPE_STRING = BRIDGE_MEMBERSHIP_TYPE_STRING;
|
|
136
|
+
exports.BRIDGE_OWNER_ATTESTATION_TYPE_STRING = BRIDGE_OWNER_ATTESTATION_TYPE_STRING;
|
|
137
|
+
exports.bridgeEvidenceKey = bridgeEvidenceKey;
|
|
138
|
+
exports.computeBridgeEndpointsHash = computeBridgeEndpointsHash;
|
|
139
|
+
exports.computeBridgeMembershipDigest = computeBridgeMembershipDigest;
|
|
140
|
+
exports.computeBridgeOwnerAttestationDigest = computeBridgeOwnerAttestationDigest;
|
|
141
|
+
exports.ethAddressOfPublicKey = ethAddressOfPublicKey;
|
|
142
|
+
exports.recoverBridgeSigner = recoverBridgeSigner;
|
|
143
|
+
exports.signBridgeDigest = signBridgeDigest;
|
|
144
|
+
exports.toHexDigest = toHexDigest;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { BytesType } from "@ocap/util";
|
|
2
|
+
|
|
3
|
+
//#region src/bridge-governance.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* EIP-712 structs the on-chain bridge governance (bridge/docs/ocap-protocols.md §v2.2) verifies on the OCAP side:
|
|
7
|
+
* `BridgeMembership` (operator consent / join / leave) and `BridgeOwnerAttestation` (Ethereum owner proof).
|
|
8
|
+
*
|
|
9
|
+
* This is a port of `membership.ts` in `@arcblock/bridge-types` (bridge/types/src/membership.ts). Core packages cannot
|
|
10
|
+
* depend on the private bridge workspace, so the logic is duplicated on purpose and a parity test
|
|
11
|
+
* (core/mcrypto/tests/bridge-governance.spec.ts) runs this port against the vector file
|
|
12
|
+
* bridge/types/vectors/membership.json, which is generated from viem (what wallets run). Change both together.
|
|
13
|
+
*
|
|
14
|
+
* Domain: `{ name: "ArcBridgeOcap", version: "1" }` with NO chainId / verifyingContract; the OCAP chain and the bridge
|
|
15
|
+
* are carried inside the struct (`ocapChainId`, `bridge`), so an approval cannot be replayed across chains or bridges.
|
|
16
|
+
*/
|
|
17
|
+
declare const BRIDGE_GOV_DOMAIN_NAME = "ArcBridgeOcap";
|
|
18
|
+
declare const BRIDGE_GOV_DOMAIN_VERSION = "1";
|
|
19
|
+
declare const BRIDGE_MEMBERSHIP_TYPE_STRING = "BridgeMembership(string ocapChainId,string bridge,uint64 epoch,address[] validators,uint32 threshold,uint64 activeFrom,uint64 voucherValidity,bytes32 endpointsHash,uint8 action)";
|
|
20
|
+
declare const BRIDGE_OWNER_ATTESTATION_TYPE_STRING = "BridgeOwnerAttestation(string ocapChainId,bytes32 salt,string ocapToken,uint256 ethChainId,address gateway,address ethToken)";
|
|
21
|
+
/** `action` of a BridgeMembership: CREATE = epoch 1 consent, JOIN / LEAVE = epoch change. */
|
|
22
|
+
declare const BRIDGE_MEMBERSHIP_ACTION: {
|
|
23
|
+
readonly CREATE: 0;
|
|
24
|
+
readonly JOIN: 1;
|
|
25
|
+
readonly LEAVE: 2;
|
|
26
|
+
};
|
|
27
|
+
interface BridgeMembershipMessage {
|
|
28
|
+
ocapChainId: string;
|
|
29
|
+
bridge: string;
|
|
30
|
+
epoch: bigint | number | string;
|
|
31
|
+
/** Strictly ascending (numeric) and unique: the signer signs exactly this order. */
|
|
32
|
+
validators: string[];
|
|
33
|
+
threshold: number;
|
|
34
|
+
activeFrom: bigint | number | string;
|
|
35
|
+
voucherValidity: bigint | number | string;
|
|
36
|
+
/** `computeBridgeEndpointsHash` of the endpoints of `validators`, in the same order */
|
|
37
|
+
endpointsHash: string;
|
|
38
|
+
action: number;
|
|
39
|
+
}
|
|
40
|
+
interface BridgeOwnerAttestationMessage {
|
|
41
|
+
ocapChainId: string;
|
|
42
|
+
salt: string;
|
|
43
|
+
ocapToken: string;
|
|
44
|
+
ethChainId: bigint | number | string;
|
|
45
|
+
gateway: string;
|
|
46
|
+
ethToken: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* `keccak256(keccak256(endpoint_0) || keccak256(endpoint_1) || ...)` over the endpoints of the validators in the order
|
|
50
|
+
* of the signed `validators` array. It binds every listed operator to the URL it is reachable at, so a creator cannot
|
|
51
|
+
* pair a victim operator with another URL.
|
|
52
|
+
*/
|
|
53
|
+
declare function computeBridgeEndpointsHash(endpoints: string[]): string;
|
|
54
|
+
declare function computeBridgeMembershipDigest(m: BridgeMembershipMessage): Uint8Array;
|
|
55
|
+
declare function computeBridgeOwnerAttestationDigest(m: BridgeOwnerAttestationMessage): Uint8Array;
|
|
56
|
+
/** Ethereum address (lowercase 0x) of an uncompressed (64/65 bytes) or compressed secp256k1 public key. */
|
|
57
|
+
declare function ethAddressOfPublicKey(pk: BytesType): string;
|
|
58
|
+
interface RecoveredBridgeSigner {
|
|
59
|
+
/** lowercase 0x address */
|
|
60
|
+
address: string;
|
|
61
|
+
/** 65-byte uncompressed public key, 0x04 prefixed */
|
|
62
|
+
publicKey: Uint8Array;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Recover the signer of a canonical 65-byte `r || s || v` signature (`v` in {27, 28}, low-S). Returns null on anything
|
|
66
|
+
* else, so a malleable or malformed signature never recovers to an address.
|
|
67
|
+
*/
|
|
68
|
+
declare function recoverBridgeSigner(digest: Uint8Array, signature: string): RecoveredBridgeSigner | null;
|
|
69
|
+
/** Sign a governance digest with a secp256k1 secret key; returns the 65-byte `0x{r}{s}{v}` signature. */
|
|
70
|
+
declare function signBridgeDigest(digest: Uint8Array, sk: BytesType): string;
|
|
71
|
+
declare const toHexDigest: (digest: Uint8Array) => string;
|
|
72
|
+
type BridgeKeyPart = {
|
|
73
|
+
uint: bigint | number | string;
|
|
74
|
+
} | {
|
|
75
|
+
address: string;
|
|
76
|
+
} | {
|
|
77
|
+
bytes32: string;
|
|
78
|
+
} | {
|
|
79
|
+
text: string;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* keccak256 of `utf8(prefix) || words`: the key preimage helper for the `arc-bridge:*` evidence keys. Every dynamic part
|
|
83
|
+
* is a fixed-width word (uint256 / address / bytes32) or the trailing DID string, so two different tuples never share a
|
|
84
|
+
* preimage. Returns a `0x` + 64 hex string (the evidence table key shape).
|
|
85
|
+
*/
|
|
86
|
+
declare function bridgeEvidenceKey(prefix: string, parts: BridgeKeyPart[]): string;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { BRIDGE_GOV_DOMAIN_NAME, BRIDGE_GOV_DOMAIN_VERSION, BRIDGE_MEMBERSHIP_ACTION, BRIDGE_MEMBERSHIP_TYPE_STRING, BRIDGE_OWNER_ATTESTATION_TYPE_STRING, BridgeKeyPart, BridgeMembershipMessage, BridgeOwnerAttestationMessage, RecoveredBridgeSigner, bridgeEvidenceKey, computeBridgeEndpointsHash, computeBridgeMembershipDigest, computeBridgeOwnerAttestationDigest, ethAddressOfPublicKey, recoverBridgeSigner, signBridgeDigest, toHexDigest };
|
package/lib/envelope.cjs
CHANGED
|
@@ -2,11 +2,14 @@ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
|
2
2
|
const require_utils = require('./_vendor/@noble/hashes/utils.cjs');
|
|
3
3
|
const require_secp256k1 = require('./_vendor/@noble/curves/secp256k1.cjs');
|
|
4
4
|
const require_sha3 = require('./_vendor/@noble/hashes/sha3.cjs');
|
|
5
|
+
const require_eth_address = require('./eth-address.cjs');
|
|
5
6
|
let _ocap_util = require("@ocap/util");
|
|
6
7
|
|
|
7
8
|
//#region src/envelope.ts
|
|
8
9
|
var envelope_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({
|
|
10
|
+
EIP712_BRIDGE_WITHDRAW_TYPE_STRING: () => EIP712_BRIDGE_WITHDRAW_TYPE_STRING,
|
|
9
11
|
assertEnvelopeMatchesTxContext: () => assertEnvelopeMatchesTxContext,
|
|
12
|
+
buildBridgeWithdrawEnvelopeExtra: () => buildBridgeWithdrawEnvelopeExtra,
|
|
10
13
|
buildEd25519EnvelopePayload: () => buildEd25519EnvelopePayload,
|
|
11
14
|
buildSolanaOffchainEnvelopeBytes: () => buildSolanaOffchainEnvelopeBytes,
|
|
12
15
|
computeEip191EnvelopeDigest: () => computeEip191EnvelopeDigest,
|
|
@@ -39,7 +42,11 @@ const ALLOWED_ENVELOPE_KEYS = new Set([
|
|
|
39
42
|
"domainVersion",
|
|
40
43
|
"applicationDomain",
|
|
41
44
|
"messageFormat",
|
|
42
|
-
"challenge"
|
|
45
|
+
"challenge",
|
|
46
|
+
"bridge",
|
|
47
|
+
"amount",
|
|
48
|
+
"token",
|
|
49
|
+
"ethRecipient"
|
|
43
50
|
]);
|
|
44
51
|
const SOLANA_OFFCHAIN_SIGNING_DOMAIN = new Uint8Array([
|
|
45
52
|
255,
|
|
@@ -103,12 +110,51 @@ function isCanonicalWalletEnvelopeExtra(extra, keyType) {
|
|
|
103
110
|
"applicationDomain",
|
|
104
111
|
"challenge"
|
|
105
112
|
]) if (parsed[field] !== void 0 && !isSafeSingleLine(parsed[field])) return false;
|
|
113
|
+
if (hasAnyWithdrawField(parsed)) {
|
|
114
|
+
if (!ETHEREUM_ENVELOPES.has(envelope) || !isWithdrawFieldSetValid(parsed)) return false;
|
|
115
|
+
}
|
|
106
116
|
if (parsed.messageFormat !== void 0 && parsed.messageFormat !== 0 && parsed.messageFormat !== 1 && parsed.messageFormat !== 2) return false;
|
|
107
117
|
return true;
|
|
108
118
|
} catch {
|
|
109
119
|
return false;
|
|
110
120
|
}
|
|
111
121
|
}
|
|
122
|
+
const WITHDRAW_FIELDS = [
|
|
123
|
+
"bridge",
|
|
124
|
+
"amount",
|
|
125
|
+
"token",
|
|
126
|
+
"ethRecipient"
|
|
127
|
+
];
|
|
128
|
+
const DECIMAL_AMOUNT = /^(0|[1-9][0-9]{0,77})$/;
|
|
129
|
+
function hasAnyWithdrawField(meta) {
|
|
130
|
+
return WITHDRAW_FIELDS.some((f) => meta[f] !== void 0);
|
|
131
|
+
}
|
|
132
|
+
function isWithdrawFieldSetValid(meta) {
|
|
133
|
+
return isSafeSingleLine(meta.bridge) && meta.bridge.length > 0 && typeof meta.amount === "string" && DECIMAL_AMOUNT.test(meta.amount) && isSafeSingleLine(meta.token) && meta.token.length > 0 && require_eth_address.parseStrictEthAddress(meta.ethRecipient).ok;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Build the `extra` object a wallet passes to `signEnvelope` for a bridge withdraw (#1465).
|
|
137
|
+
* `bridge` is the OCAP bridge (rollup) address whose reserve is `itx.to`, `amount` the decimal base-unit string, `token` the bridge token address, `ethRecipient` the Ethereum recipient
|
|
138
|
+
* exactly as written in `itx.data`.
|
|
139
|
+
*/
|
|
140
|
+
function buildBridgeWithdrawEnvelopeExtra(input) {
|
|
141
|
+
const extra = {
|
|
142
|
+
envelope: input.envelope || "eip712",
|
|
143
|
+
chainId: input.chainId,
|
|
144
|
+
action: input.action || "transfer_v2",
|
|
145
|
+
from: input.from,
|
|
146
|
+
nonce: String(input.nonce),
|
|
147
|
+
bridge: input.bridge,
|
|
148
|
+
amount: String(input.amount),
|
|
149
|
+
token: input.token,
|
|
150
|
+
ethRecipient: input.ethRecipient
|
|
151
|
+
};
|
|
152
|
+
if (input.domainName) extra.domainName = input.domainName;
|
|
153
|
+
if (input.domainVersion) extra.domainVersion = input.domainVersion;
|
|
154
|
+
if (input.statement) extra.statement = input.statement;
|
|
155
|
+
if (!isWithdrawFieldSetValid(extra)) throw new Error("Invalid bridge withdraw envelope fields");
|
|
156
|
+
return extra;
|
|
157
|
+
}
|
|
112
158
|
function parseWalletEnvelopeExtra(extra, expectedKeyType) {
|
|
113
159
|
if (!isCanonicalWalletEnvelopeExtra(extra, expectedKeyType)) throw new Error("Invalid or non-canonical wallet signature envelope extra");
|
|
114
160
|
return JSON.parse(extra);
|
|
@@ -120,6 +166,21 @@ function parseWalletEnvelopeExtra(extra, expectedKeyType) {
|
|
|
120
166
|
*/
|
|
121
167
|
function assertEnvelopeMatchesTxContext(extra, expected) {
|
|
122
168
|
const parsed = typeof extra === "string" ? parseWalletEnvelopeExtra(extra) : extra;
|
|
169
|
+
const withdraw = expected.withdraw;
|
|
170
|
+
if (withdraw) {
|
|
171
|
+
if (!hasAnyWithdrawField(parsed) || !isWithdrawFieldSetValid(parsed)) throw new Error("Bridge withdraw requires an envelope carrying bridge, amount, token and ethRecipient");
|
|
172
|
+
if (!ETHEREUM_ENVELOPES.has(parsed.envelope)) throw new Error("Bridge withdraw requires an Ethereum envelope");
|
|
173
|
+
for (const field of [
|
|
174
|
+
"chainId",
|
|
175
|
+
"from",
|
|
176
|
+
"nonce"
|
|
177
|
+
]) if (parsed[field] === void 0 || parsed[field] === "") throw new Error(`Bridge withdraw envelope is missing "${field}"`);
|
|
178
|
+
if (parsed.action !== "transfer_v2" && parsed.action !== "fg:t:transfer_v2") throw new Error(`Bridge withdraw envelope action must be transfer_v2, got "${parsed.action ?? ""}"`);
|
|
179
|
+
if (parsed.bridge !== withdraw.bridge) throw new Error(`Envelope bridge "${parsed.bridge}" does not match transaction bridge "${withdraw.bridge}"`);
|
|
180
|
+
if (parsed.amount !== withdraw.amount) throw new Error(`Envelope amount "${parsed.amount}" does not match transaction amount "${withdraw.amount}"`);
|
|
181
|
+
if (parsed.token !== withdraw.token) throw new Error(`Envelope token "${parsed.token}" does not match transaction token "${withdraw.token}"`);
|
|
182
|
+
if (String(parsed.ethRecipient).toLowerCase() !== String(withdraw.ethRecipient).toLowerCase()) throw new Error(`Envelope ethRecipient "${parsed.ethRecipient}" does not match transaction recipient "${withdraw.ethRecipient}"`);
|
|
183
|
+
} else if (hasAnyWithdrawField(parsed)) throw new Error("Envelope carries bridge withdraw fields but the transaction is not a bridge withdraw");
|
|
123
184
|
if (parsed.chainId !== void 0 && expected.chainId !== void 0 && expected.chainId !== "") {
|
|
124
185
|
if (parsed.chainId !== expected.chainId) throw new Error(`Envelope chainId "${parsed.chainId}" does not match transaction chainId "${expected.chainId}"`);
|
|
125
186
|
}
|
|
@@ -142,19 +203,28 @@ function assertEnvelopeMatchesTxContext(extra, expected) {
|
|
|
142
203
|
function formatWalletEnvelopeText(challenge, meta = {}) {
|
|
143
204
|
const challengeHex = normalizeChallengeHex(challenge);
|
|
144
205
|
if (meta.challenge !== void 0 && normalizeChallengeHex(meta.challenge) !== challengeHex) throw new Error("challenge mismatch in wallet signature envelope");
|
|
145
|
-
|
|
206
|
+
const optionalStringFields = [
|
|
146
207
|
"statement",
|
|
147
208
|
"chainId",
|
|
148
209
|
"action",
|
|
149
210
|
"from",
|
|
150
|
-
"nonce"
|
|
151
|
-
|
|
211
|
+
"nonce",
|
|
212
|
+
...WITHDRAW_FIELDS
|
|
213
|
+
];
|
|
214
|
+
for (const field of optionalStringFields) if (meta[field] !== void 0 && !isSafeSingleLine(meta[field])) throw new Error(`Invalid character in wallet envelope field "${field}"`);
|
|
215
|
+
if (hasAnyWithdrawField(meta) && !isWithdrawFieldSetValid(meta)) throw new Error("Invalid bridge withdraw envelope fields");
|
|
152
216
|
const lines = ["ArcBlock Transaction v1"];
|
|
153
217
|
if (meta.statement) lines.push(`Statement: ${meta.statement}`);
|
|
154
218
|
if (meta.chainId) lines.push(`Chain: ${meta.chainId}`);
|
|
155
219
|
if (meta.action) lines.push(`Action: ${meta.action}`);
|
|
156
220
|
if (meta.from) lines.push(`Signer: ${meta.from}`);
|
|
157
221
|
if (meta.nonce) lines.push(`Nonce: ${meta.nonce}`);
|
|
222
|
+
if (hasAnyWithdrawField(meta)) {
|
|
223
|
+
lines.push(`Bridge: ${meta.bridge}`);
|
|
224
|
+
lines.push(`Amount: ${meta.amount}`);
|
|
225
|
+
lines.push(`Token: ${meta.token}`);
|
|
226
|
+
lines.push(`Recipient: ${meta.ethRecipient}`);
|
|
227
|
+
}
|
|
158
228
|
lines.push(`Challenge: ${challengeHex}`);
|
|
159
229
|
return lines.join("\n");
|
|
160
230
|
}
|
|
@@ -169,6 +239,9 @@ function computeEip191EnvelopeDigest(challenge, meta = {}) {
|
|
|
169
239
|
}
|
|
170
240
|
const EIP712_DOMAIN_TYPEHASH = require_sha3.keccak_256(new Uint8Array(Buffer.from("EIP712Domain(string name,string version)", "utf8")));
|
|
171
241
|
const EIP712_TX_TYPEHASH = require_sha3.keccak_256(new Uint8Array(Buffer.from("ArcBlockTransaction(string chainId,string action,string from,string nonce,string challenge)", "utf8")));
|
|
242
|
+
/** Primary type for the bridge withdraw clear-signing envelope (bridge/docs/ocap-protocols.md §2). */
|
|
243
|
+
const EIP712_BRIDGE_WITHDRAW_TYPE_STRING = "ArcBridgeWithdraw(string chainId,string from,string nonce,string bridge,string amount,string token,address ethRecipient,string challenge)";
|
|
244
|
+
const EIP712_BRIDGE_WITHDRAW_TYPEHASH = require_sha3.keccak_256(new Uint8Array(Buffer.from(EIP712_BRIDGE_WITHDRAW_TYPE_STRING, "utf8")));
|
|
172
245
|
function keccakUtf8(value) {
|
|
173
246
|
return require_sha3.keccak_256(new Uint8Array(Buffer.from(value, "utf8")));
|
|
174
247
|
}
|
|
@@ -178,14 +251,16 @@ function keccakUtf8(value) {
|
|
|
178
251
|
function computeEip712EnvelopeDigest(challenge, meta = {}) {
|
|
179
252
|
const challengeHex = normalizeChallengeHex(challenge);
|
|
180
253
|
if (meta.challenge !== void 0 && normalizeChallengeHex(meta.challenge) !== challengeHex) throw new Error("challenge mismatch in wallet signature envelope");
|
|
181
|
-
|
|
254
|
+
const fields = [
|
|
182
255
|
"chainId",
|
|
183
256
|
"action",
|
|
184
257
|
"from",
|
|
185
258
|
"nonce",
|
|
186
259
|
"domainName",
|
|
187
|
-
"domainVersion"
|
|
188
|
-
|
|
260
|
+
"domainVersion",
|
|
261
|
+
...WITHDRAW_FIELDS
|
|
262
|
+
];
|
|
263
|
+
for (const field of fields) if (meta[field] !== void 0 && !isSafeSingleLine(meta[field])) throw new Error(`Invalid character in wallet envelope field "${field}"`);
|
|
189
264
|
const domainName = meta.domainName || "ArcBlock";
|
|
190
265
|
const domainVersion = meta.domainVersion || "1";
|
|
191
266
|
const chainId = meta.chainId || "arcblock";
|
|
@@ -197,6 +272,27 @@ function computeEip712EnvelopeDigest(challenge, meta = {}) {
|
|
|
197
272
|
keccakUtf8(domainName),
|
|
198
273
|
keccakUtf8(domainVersion)
|
|
199
274
|
])));
|
|
275
|
+
if (hasAnyWithdrawField(meta)) {
|
|
276
|
+
if (!isWithdrawFieldSetValid(meta)) throw new Error("Invalid bridge withdraw envelope fields");
|
|
277
|
+
const recipient = require_eth_address.parseStrictEthAddress(meta.ethRecipient);
|
|
278
|
+
if (!recipient.ok) throw new Error("Invalid bridge withdraw envelope fields");
|
|
279
|
+
const withdrawHash = require_sha3.keccak_256(new Uint8Array(Buffer.concat([
|
|
280
|
+
EIP712_BRIDGE_WITHDRAW_TYPEHASH,
|
|
281
|
+
keccakUtf8(chainId),
|
|
282
|
+
keccakUtf8(from),
|
|
283
|
+
keccakUtf8(nonce),
|
|
284
|
+
keccakUtf8(meta.bridge),
|
|
285
|
+
keccakUtf8(meta.amount),
|
|
286
|
+
keccakUtf8(meta.token),
|
|
287
|
+
Buffer.concat([Buffer.alloc(12), Buffer.from(recipient.lowercase.slice(2), "hex")]),
|
|
288
|
+
keccakUtf8(challengeHex)
|
|
289
|
+
])));
|
|
290
|
+
return require_sha3.keccak_256(new Uint8Array(Buffer.concat([
|
|
291
|
+
Buffer.from([25, 1]),
|
|
292
|
+
domainSeparator,
|
|
293
|
+
withdrawHash
|
|
294
|
+
])));
|
|
295
|
+
}
|
|
200
296
|
const structHash = require_sha3.keccak_256(new Uint8Array(Buffer.concat([
|
|
201
297
|
EIP712_TX_TYPEHASH,
|
|
202
298
|
keccakUtf8(chainId),
|
|
@@ -303,7 +399,9 @@ function buildEd25519EnvelopePayload(challenge, pk, extra) {
|
|
|
303
399
|
}
|
|
304
400
|
|
|
305
401
|
//#endregion
|
|
402
|
+
exports.EIP712_BRIDGE_WITHDRAW_TYPE_STRING = EIP712_BRIDGE_WITHDRAW_TYPE_STRING;
|
|
306
403
|
exports.assertEnvelopeMatchesTxContext = assertEnvelopeMatchesTxContext;
|
|
404
|
+
exports.buildBridgeWithdrawEnvelopeExtra = buildBridgeWithdrawEnvelopeExtra;
|
|
307
405
|
exports.buildEd25519EnvelopePayload = buildEd25519EnvelopePayload;
|
|
308
406
|
exports.buildSolanaOffchainEnvelopeBytes = buildSolanaOffchainEnvelopeBytes;
|
|
309
407
|
exports.computeEip191EnvelopeDigest = computeEip191EnvelopeDigest;
|
package/lib/envelope.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import { BytesType } from "@ocap/util";
|
|
|
2
2
|
|
|
3
3
|
//#region src/envelope.d.ts
|
|
4
4
|
declare namespace envelope_d_exports {
|
|
5
|
-
export { ExpectedEnvelopeTxContext, WalletEnvelopeExtra, WalletEnvelopeType, assertEnvelopeMatchesTxContext, buildEd25519EnvelopePayload, buildSolanaOffchainEnvelopeBytes, computeEip191EnvelopeDigest, computeEip712EnvelopeDigest, formatWalletEnvelopeText, isCanonicalWalletEnvelopeExtra, normalizeChallengeHex, parseWalletEnvelopeExtra, signEthereumEnvelope, verifyEthereumEnvelope };
|
|
5
|
+
export { EIP712_BRIDGE_WITHDRAW_TYPE_STRING, ExpectedEnvelopeTxContext, WalletEnvelopeExtra, WalletEnvelopeType, assertEnvelopeMatchesTxContext, buildBridgeWithdrawEnvelopeExtra, buildEd25519EnvelopePayload, buildSolanaOffchainEnvelopeBytes, computeEip191EnvelopeDigest, computeEip712EnvelopeDigest, formatWalletEnvelopeText, isCanonicalWalletEnvelopeExtra, normalizeChallengeHex, parseWalletEnvelopeExtra, signEthereumEnvelope, verifyEthereumEnvelope };
|
|
6
6
|
}
|
|
7
7
|
type WalletEnvelopeType = 'eip191' | 'eip712' | 'solana-message' | 'solana-offchain';
|
|
8
8
|
interface WalletEnvelopeExtra {
|
|
@@ -17,6 +17,14 @@ interface WalletEnvelopeExtra {
|
|
|
17
17
|
applicationDomain?: string;
|
|
18
18
|
messageFormat?: number;
|
|
19
19
|
challenge?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Bridge withdraw clear-signing fields (#1465). All four present selects the `ArcBridgeWithdraw` EIP-712 primary type
|
|
22
|
+
* (and adds Bridge/Amount/Token/Recipient lines to the EIP-191 text); partially present is invalid.
|
|
23
|
+
*/
|
|
24
|
+
bridge?: string;
|
|
25
|
+
amount?: string;
|
|
26
|
+
token?: string;
|
|
27
|
+
ethRecipient?: string;
|
|
20
28
|
}
|
|
21
29
|
/**
|
|
22
30
|
* Normalize a challenge input (hex string or bytes) into a canonical lowercase `0x`-prefixed hex string.
|
|
@@ -27,6 +35,25 @@ declare function normalizeChallengeHex(challenge: BytesType): string;
|
|
|
27
35
|
* When `keyType` is provided (0 = ED25519, 1 = SECP256K1, 2 = ETHEREUM), also enforces curve compatibility.
|
|
28
36
|
*/
|
|
29
37
|
declare function isCanonicalWalletEnvelopeExtra(extra: unknown, keyType?: number): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Build the `extra` object a wallet passes to `signEnvelope` for a bridge withdraw (#1465).
|
|
40
|
+
* `bridge` is the OCAP bridge (rollup) address whose reserve is `itx.to`, `amount` the decimal base-unit string, `token` the bridge token address, `ethRecipient` the Ethereum recipient
|
|
41
|
+
* exactly as written in `itx.data`.
|
|
42
|
+
*/
|
|
43
|
+
declare function buildBridgeWithdrawEnvelopeExtra(input: {
|
|
44
|
+
envelope?: 'eip191' | 'eip712';
|
|
45
|
+
chainId: string;
|
|
46
|
+
from: string;
|
|
47
|
+
nonce: string | number | bigint;
|
|
48
|
+
bridge: string;
|
|
49
|
+
amount: string | number | bigint;
|
|
50
|
+
token: string;
|
|
51
|
+
ethRecipient: string;
|
|
52
|
+
action?: string;
|
|
53
|
+
domainName?: string;
|
|
54
|
+
domainVersion?: string;
|
|
55
|
+
statement?: string;
|
|
56
|
+
}): WalletEnvelopeExtra;
|
|
30
57
|
declare function parseWalletEnvelopeExtra(extra: string, expectedKeyType?: number): WalletEnvelopeExtra;
|
|
31
58
|
interface ExpectedEnvelopeTxContext {
|
|
32
59
|
chainId?: string;
|
|
@@ -34,6 +61,17 @@ interface ExpectedEnvelopeTxContext {
|
|
|
34
61
|
from?: string;
|
|
35
62
|
nonce?: string | number | bigint;
|
|
36
63
|
action?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Decoded bridge withdraw (transfer_v2 into the reserve), or null/undefined for any other tx.
|
|
66
|
+
* When set, the envelope MUST carry matching `amount`, `token` and `ethRecipient` (absent = rejected).
|
|
67
|
+
* When not set, an envelope carrying any of those fields is rejected.
|
|
68
|
+
*/
|
|
69
|
+
withdraw?: {
|
|
70
|
+
bridge: string;
|
|
71
|
+
amount: string;
|
|
72
|
+
token: string;
|
|
73
|
+
ethRecipient: string;
|
|
74
|
+
} | null;
|
|
37
75
|
}
|
|
38
76
|
/**
|
|
39
77
|
* Verifies that any human-readable metadata field present in a signed wallet envelope
|
|
@@ -50,6 +88,8 @@ declare function formatWalletEnvelopeText(challenge: BytesType, meta?: Partial<W
|
|
|
50
88
|
* Compute the EIP-191 (`personal_sign`) 32-byte keccak256 digest over the canonical envelope text.
|
|
51
89
|
*/
|
|
52
90
|
declare function computeEip191EnvelopeDigest(challenge: BytesType, meta?: Partial<WalletEnvelopeExtra>): Uint8Array;
|
|
91
|
+
/** Primary type for the bridge withdraw clear-signing envelope (bridge/docs/ocap-protocols.md §2). */
|
|
92
|
+
declare const EIP712_BRIDGE_WITHDRAW_TYPE_STRING = "ArcBridgeWithdraw(string chainId,string from,string nonce,string bridge,string amount,string token,address ethRecipient,string challenge)";
|
|
53
93
|
/**
|
|
54
94
|
* Compute the EIP-712 (`eth_signTypedData_v4`) 32-byte digest for an ArcBlockTransaction envelope.
|
|
55
95
|
*/
|
|
@@ -73,4 +113,4 @@ declare function verifyEthereumEnvelope(challenge: BytesType, signature: BytesTy
|
|
|
73
113
|
*/
|
|
74
114
|
declare function buildEd25519EnvelopePayload(challenge: BytesType, pk: BytesType, extra: string): Uint8Array;
|
|
75
115
|
//#endregion
|
|
76
|
-
export { ExpectedEnvelopeTxContext, WalletEnvelopeExtra, WalletEnvelopeType, assertEnvelopeMatchesTxContext, buildEd25519EnvelopePayload, buildSolanaOffchainEnvelopeBytes, computeEip191EnvelopeDigest, computeEip712EnvelopeDigest, envelope_d_exports, formatWalletEnvelopeText, isCanonicalWalletEnvelopeExtra, normalizeChallengeHex, parseWalletEnvelopeExtra, signEthereumEnvelope, verifyEthereumEnvelope };
|
|
116
|
+
export { EIP712_BRIDGE_WITHDRAW_TYPE_STRING, ExpectedEnvelopeTxContext, WalletEnvelopeExtra, WalletEnvelopeType, assertEnvelopeMatchesTxContext, buildBridgeWithdrawEnvelopeExtra, buildEd25519EnvelopePayload, buildSolanaOffchainEnvelopeBytes, computeEip191EnvelopeDigest, computeEip712EnvelopeDigest, envelope_d_exports, formatWalletEnvelopeText, isCanonicalWalletEnvelopeExtra, normalizeChallengeHex, parseWalletEnvelopeExtra, signEthereumEnvelope, verifyEthereumEnvelope };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const require_utils = require('./_vendor/@noble/hashes/utils.cjs');
|
|
2
|
+
const require_sha3 = require('./_vendor/@noble/hashes/sha3.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/eth-address.ts
|
|
5
|
+
const FORMAT = /^0x[0-9a-fA-F]{40}$/;
|
|
6
|
+
function eip55(bodyLower) {
|
|
7
|
+
const hash = require_utils.bytesToHex(require_sha3.keccak_256(new Uint8Array(Buffer.from(bodyLower, "ascii"))));
|
|
8
|
+
let out = "";
|
|
9
|
+
for (let i = 0; i < 40; i++) {
|
|
10
|
+
const ch = bodyLower[i];
|
|
11
|
+
out += Number.parseInt(hash[i], 16) >= 8 ? ch.toUpperCase() : ch;
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
function parseStrictEthAddress(value) {
|
|
16
|
+
if (typeof value !== "string") return {
|
|
17
|
+
ok: false,
|
|
18
|
+
reason: "not-a-string"
|
|
19
|
+
};
|
|
20
|
+
if (!FORMAT.test(value)) return {
|
|
21
|
+
ok: false,
|
|
22
|
+
reason: "format"
|
|
23
|
+
};
|
|
24
|
+
const body = value.slice(2);
|
|
25
|
+
const lower = body.toLowerCase();
|
|
26
|
+
const hasLower = /[a-f]/.test(body);
|
|
27
|
+
const hasUpper = /[A-F]/.test(body);
|
|
28
|
+
if (hasUpper && !hasLower) return {
|
|
29
|
+
ok: false,
|
|
30
|
+
reason: "uppercase"
|
|
31
|
+
};
|
|
32
|
+
if (hasUpper && hasLower && eip55(lower) !== body) return {
|
|
33
|
+
ok: false,
|
|
34
|
+
reason: "checksum"
|
|
35
|
+
};
|
|
36
|
+
if (/^0+$/.test(lower)) return {
|
|
37
|
+
ok: false,
|
|
38
|
+
reason: "zero-address"
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
ok: true,
|
|
42
|
+
address: value,
|
|
43
|
+
lowercase: `0x${lower}`
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function isStrictEthAddress(value) {
|
|
47
|
+
return parseStrictEthAddress(value).ok;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
exports.isStrictEthAddress = isStrictEthAddress;
|
|
52
|
+
exports.parseStrictEthAddress = parseStrictEthAddress;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/eth-address.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Strict Ethereum address parser for the OCAP bridge (bridge/docs/ocap-protocols.md §6).
|
|
4
|
+
*
|
|
5
|
+
* This is a port of `parseStrictEthAddress` in `@arcblock/bridge-types` (bridge/types/src/address.ts). Core packages
|
|
6
|
+
* cannot depend on the private bridge workspace, so the logic is duplicated on purpose and a parity test in
|
|
7
|
+
* bridge/types runs both implementations against the same vector file (bridge/types/vectors/eth-address.json).
|
|
8
|
+
* Change both together.
|
|
9
|
+
*
|
|
10
|
+
* Rules:
|
|
11
|
+
* - exactly `0x` + 40 hex digits, no whitespace, no `0X` prefix;
|
|
12
|
+
* - letters all lowercase: accepted; letters all uppercase: rejected; mixed case: must satisfy EIP-55;
|
|
13
|
+
* - the zero address is rejected.
|
|
14
|
+
*/
|
|
15
|
+
type StrictEthAddressFailure = 'not-a-string' | 'format' | 'uppercase' | 'checksum' | 'zero-address';
|
|
16
|
+
type StrictEthAddressResult = {
|
|
17
|
+
ok: true;
|
|
18
|
+
address: string;
|
|
19
|
+
lowercase: string;
|
|
20
|
+
} | {
|
|
21
|
+
ok: false;
|
|
22
|
+
reason: StrictEthAddressFailure;
|
|
23
|
+
};
|
|
24
|
+
declare function parseStrictEthAddress(value: unknown): StrictEthAddressResult;
|
|
25
|
+
declare function isStrictEthAddress(value: unknown): value is string;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { StrictEthAddressFailure, StrictEthAddressResult, isStrictEthAddress, parseStrictEthAddress };
|