@getpara/rest-sdk 2.32.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 +175 -0
- package/dist/cjs/ethers/index.js +193 -0
- package/dist/cjs/index.js +339 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/solana/index.js +246 -0
- package/dist/cjs/viem/index.js +178 -0
- package/dist/esm/ethers/index.js +168 -0
- package/dist/esm/index.js +312 -0
- package/dist/esm/package.json +4 -0
- package/dist/esm/solana/index.js +225 -0
- package/dist/esm/viem/index.js +157 -0
- package/dist/types/ethers/index.d.ts +23 -0
- package/dist/types/index.d.ts +292 -0
- package/dist/types/solana/index.d.ts +12 -0
- package/dist/types/viem/index.d.ts +11 -0
- package/package.json +114 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __export = (target, all) => {
|
|
20
|
+
for (var name in all)
|
|
21
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
22
|
+
};
|
|
23
|
+
var __copyProps = (to, from, except, desc) => {
|
|
24
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
25
|
+
for (let key of __getOwnPropNames(from))
|
|
26
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
27
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
28
|
+
}
|
|
29
|
+
return to;
|
|
30
|
+
};
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
var __async = (__this, __arguments, generator) => {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
var fulfilled = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.next(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var rejected = (value) => {
|
|
42
|
+
try {
|
|
43
|
+
step(generator.throw(value));
|
|
44
|
+
} catch (e) {
|
|
45
|
+
reject(e);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
49
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
var viem_exports = {};
|
|
53
|
+
__export(viem_exports, {
|
|
54
|
+
createParaRestViemAccount: () => createParaRestViemAccount
|
|
55
|
+
});
|
|
56
|
+
module.exports = __toCommonJS(viem_exports);
|
|
57
|
+
var import_viem = require("viem");
|
|
58
|
+
var import__ = require("../index.js");
|
|
59
|
+
function createParaRestViemAccount(options) {
|
|
60
|
+
const { client, walletId, address, publicKey = "0x" } = options;
|
|
61
|
+
return {
|
|
62
|
+
address,
|
|
63
|
+
publicKey,
|
|
64
|
+
source: "custom",
|
|
65
|
+
type: "local",
|
|
66
|
+
sign: (_0) => __async(this, [_0], function* ({ hash }) {
|
|
67
|
+
const res = yield client.signRaw(walletId, {
|
|
68
|
+
data: hash,
|
|
69
|
+
encoding: "hex",
|
|
70
|
+
walletType: "EVM"
|
|
71
|
+
});
|
|
72
|
+
return normalizeSignature(res.signature);
|
|
73
|
+
}),
|
|
74
|
+
signMessage: (_0) => __async(this, [_0], function* ({ message }) {
|
|
75
|
+
if (typeof message !== "string") {
|
|
76
|
+
const res2 = yield client.signRaw(walletId, {
|
|
77
|
+
data: (0, import_viem.hashMessage)(message),
|
|
78
|
+
encoding: "hex",
|
|
79
|
+
walletType: "EVM"
|
|
80
|
+
});
|
|
81
|
+
return normalizeSignature(res2.signature);
|
|
82
|
+
}
|
|
83
|
+
const res = yield client.signMessage(walletId, { message });
|
|
84
|
+
return normalizeSignature(res.signature);
|
|
85
|
+
}),
|
|
86
|
+
signTypedData: (typedDataDefinition) => __async(this, null, function* () {
|
|
87
|
+
const res = yield client.signTypedData(walletId, {
|
|
88
|
+
typedData: toRestTypedData(typedDataDefinition)
|
|
89
|
+
});
|
|
90
|
+
return normalizeSignature(res.signature);
|
|
91
|
+
}),
|
|
92
|
+
signTransaction: (transaction, args) => __async(this, null, function* () {
|
|
93
|
+
if (args == null ? void 0 : args.serializer) {
|
|
94
|
+
throw new import__.ParaRestValidationError("custom transaction serializers are not supported by the REST signer");
|
|
95
|
+
}
|
|
96
|
+
const res = yield client.signTransaction(walletId, {
|
|
97
|
+
transaction: toRestTransaction(transaction)
|
|
98
|
+
});
|
|
99
|
+
return res.signedTransaction;
|
|
100
|
+
}),
|
|
101
|
+
signAuthorization: (authorization) => __async(this, null, function* () {
|
|
102
|
+
const res = yield client.signAuthorization(walletId, {
|
|
103
|
+
authorization
|
|
104
|
+
});
|
|
105
|
+
const parsed = (0, import_viem.parseSignature)(toHexSignature(res.signature));
|
|
106
|
+
return {
|
|
107
|
+
address: res.address,
|
|
108
|
+
chainId: res.chainId,
|
|
109
|
+
nonce: res.nonce,
|
|
110
|
+
r: parsed.r,
|
|
111
|
+
s: parsed.s,
|
|
112
|
+
yParity: parsed.yParity
|
|
113
|
+
};
|
|
114
|
+
})
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function normalizeSignature(signature) {
|
|
118
|
+
const parsed = (0, import_viem.parseSignature)(toHexSignature(signature));
|
|
119
|
+
return (0, import_viem.serializeSignature)({ r: parsed.r, s: parsed.s, yParity: parsed.yParity });
|
|
120
|
+
}
|
|
121
|
+
function toHexSignature(signature) {
|
|
122
|
+
return signature.startsWith("0x") ? signature : `0x${signature}`;
|
|
123
|
+
}
|
|
124
|
+
function toRestTransaction(transaction) {
|
|
125
|
+
if (!transaction.to) {
|
|
126
|
+
throw new import__.ParaRestValidationError("transaction.to is required");
|
|
127
|
+
}
|
|
128
|
+
if (transaction.chainId == null) {
|
|
129
|
+
throw new import__.ParaRestValidationError("transaction.chainId is required");
|
|
130
|
+
}
|
|
131
|
+
if (transaction.accessList != null) {
|
|
132
|
+
throw new import__.ParaRestValidationError("transaction.accessList is not supported by the REST signer");
|
|
133
|
+
}
|
|
134
|
+
const type = restTransactionType(transaction);
|
|
135
|
+
return omitUndefined({
|
|
136
|
+
to: transaction.to,
|
|
137
|
+
value: quantity(transaction.value),
|
|
138
|
+
chainId: quantity(transaction.chainId),
|
|
139
|
+
data: transaction.data,
|
|
140
|
+
nonce: transaction.nonce,
|
|
141
|
+
gasLimit: quantity(transaction.gas),
|
|
142
|
+
maxFeePerGas: quantity(transaction.maxFeePerGas),
|
|
143
|
+
maxPriorityFeePerGas: quantity(transaction.maxPriorityFeePerGas),
|
|
144
|
+
gasPrice: quantity(transaction.gasPrice),
|
|
145
|
+
type
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function toRestTypedData(typedData) {
|
|
149
|
+
var _a;
|
|
150
|
+
const types = __spreadValues({}, typedData.types);
|
|
151
|
+
delete types.EIP712Domain;
|
|
152
|
+
return {
|
|
153
|
+
domain: (_a = typedData.domain) != null ? _a : {},
|
|
154
|
+
types,
|
|
155
|
+
primaryType: typedData.primaryType,
|
|
156
|
+
message: typedData.message
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function restTransactionType(transaction) {
|
|
160
|
+
const { type } = transaction;
|
|
161
|
+
if (type === "legacy") return 0;
|
|
162
|
+
if (type === "eip1559") return 2;
|
|
163
|
+
if (type !== void 0) {
|
|
164
|
+
throw new import__.ParaRestValidationError(`transaction.type ${String(type)} is not supported by the REST signer`);
|
|
165
|
+
}
|
|
166
|
+
if (transaction.maxFeePerGas != null || transaction.maxPriorityFeePerGas != null) return 2;
|
|
167
|
+
return transaction.gasPrice == null ? 2 : 0;
|
|
168
|
+
}
|
|
169
|
+
function quantity(value) {
|
|
170
|
+
return value == null ? void 0 : value.toString();
|
|
171
|
+
}
|
|
172
|
+
function omitUndefined(value) {
|
|
173
|
+
return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== void 0));
|
|
174
|
+
}
|
|
175
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
176
|
+
0 && (module.exports = {
|
|
177
|
+
createParaRestViemAccount
|
|
178
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { ethers } from "ethers";
|
|
22
|
+
import { ParaRestValidationError } from "../index.js";
|
|
23
|
+
class ParaRestEthersSigner extends ethers.AbstractSigner {
|
|
24
|
+
constructor({ client, walletId, address, provider }) {
|
|
25
|
+
super(provider);
|
|
26
|
+
if (!client) {
|
|
27
|
+
throw new ParaRestValidationError("client is required");
|
|
28
|
+
}
|
|
29
|
+
if (!walletId) {
|
|
30
|
+
throw new ParaRestValidationError("walletId is required");
|
|
31
|
+
}
|
|
32
|
+
this.client = client;
|
|
33
|
+
this.walletId = walletId;
|
|
34
|
+
this.initialAddress = address;
|
|
35
|
+
}
|
|
36
|
+
connect(provider) {
|
|
37
|
+
return new ParaRestEthersSigner({
|
|
38
|
+
client: this.client,
|
|
39
|
+
walletId: this.walletId,
|
|
40
|
+
address: this.initialAddress,
|
|
41
|
+
provider
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
getAddress() {
|
|
45
|
+
return __async(this, null, function* () {
|
|
46
|
+
if (this.initialAddress) {
|
|
47
|
+
return ethers.getAddress(this.initialAddress);
|
|
48
|
+
}
|
|
49
|
+
if (!this.cachedWallet) {
|
|
50
|
+
this.cachedWallet = yield this.client.getWallet(this.walletId);
|
|
51
|
+
}
|
|
52
|
+
if (!this.cachedWallet.address) {
|
|
53
|
+
throw new ParaRestValidationError("wallet address is not available");
|
|
54
|
+
}
|
|
55
|
+
return ethers.getAddress(this.cachedWallet.address);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
signMessage(message) {
|
|
59
|
+
return __async(this, null, function* () {
|
|
60
|
+
if (typeof message === "string") {
|
|
61
|
+
const res2 = yield this.client.signMessage(this.walletId, { message });
|
|
62
|
+
return normalizeEvmSignature(res2.signature);
|
|
63
|
+
}
|
|
64
|
+
const digest = ethers.hashMessage(message);
|
|
65
|
+
const res = yield this.client.signRaw(this.walletId, { data: digest, walletType: "EVM" });
|
|
66
|
+
return normalizeEvmSignature(res.signature);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
signTransaction(tx) {
|
|
70
|
+
return __async(this, null, function* () {
|
|
71
|
+
const transaction = yield this.toRestTransaction(tx);
|
|
72
|
+
const res = yield this.client.signTransaction(this.walletId, { transaction });
|
|
73
|
+
return res.signedTransaction;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
signTypedData(domain, types, value) {
|
|
77
|
+
return __async(this, null, function* () {
|
|
78
|
+
const populated = yield ethers.TypedDataEncoder.resolveNames(domain, types, value, (name) => __async(this, null, function* () {
|
|
79
|
+
ethers.assert(this.provider != null, "cannot resolve ENS names without a provider", "UNSUPPORTED_OPERATION", {
|
|
80
|
+
operation: "resolveName",
|
|
81
|
+
info: { name }
|
|
82
|
+
});
|
|
83
|
+
const address = yield this.provider.resolveName(name);
|
|
84
|
+
ethers.assert(address != null, "unconfigured ENS name", "UNCONFIGURED_NAME", {
|
|
85
|
+
value: name
|
|
86
|
+
});
|
|
87
|
+
return address;
|
|
88
|
+
}));
|
|
89
|
+
const typesWithoutDomain = stripEip712Domain(types);
|
|
90
|
+
const typedData = {
|
|
91
|
+
domain: populated.domain,
|
|
92
|
+
message: populated.value,
|
|
93
|
+
primaryType: ethers.TypedDataEncoder.from(typesWithoutDomain).primaryType,
|
|
94
|
+
types: typesWithoutDomain
|
|
95
|
+
};
|
|
96
|
+
const res = yield this.client.signTypedData(this.walletId, { typedData });
|
|
97
|
+
return normalizeEvmSignature(res.signature);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
toRestTransaction(tx) {
|
|
101
|
+
return __async(this, null, function* () {
|
|
102
|
+
const resolved = yield ethers.resolveProperties(tx);
|
|
103
|
+
const to = resolved.to ? yield ethers.resolveAddress(resolved.to, this.provider) : void 0;
|
|
104
|
+
const from = resolved.from ? yield ethers.resolveAddress(resolved.from, this.provider) : void 0;
|
|
105
|
+
if (!to) {
|
|
106
|
+
throw new ParaRestValidationError("transaction.to is required");
|
|
107
|
+
}
|
|
108
|
+
if (from && ethers.getAddress(from) !== ethers.getAddress(yield this.getAddress())) {
|
|
109
|
+
throw new ParaRestValidationError("transaction from address mismatch");
|
|
110
|
+
}
|
|
111
|
+
if (resolved.chainId == null) {
|
|
112
|
+
throw new ParaRestValidationError("transaction.chainId is required");
|
|
113
|
+
}
|
|
114
|
+
if (resolved.accessList != null) {
|
|
115
|
+
throw new ParaRestValidationError("transaction.accessList is not supported by the REST signer");
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
to,
|
|
119
|
+
value: quantityToDecimalString(resolved.value),
|
|
120
|
+
chainId: quantityToDecimalString(resolved.chainId),
|
|
121
|
+
data: resolved.data ? ethers.hexlify(resolved.data) : void 0,
|
|
122
|
+
nonce: resolved.nonce == null ? void 0 : Number(resolved.nonce),
|
|
123
|
+
gasLimit: quantityToDecimalString(resolved.gasLimit),
|
|
124
|
+
maxFeePerGas: quantityToDecimalString(resolved.maxFeePerGas),
|
|
125
|
+
maxPriorityFeePerGas: quantityToDecimalString(resolved.maxPriorityFeePerGas),
|
|
126
|
+
gasPrice: quantityToDecimalString(resolved.gasPrice),
|
|
127
|
+
type: normalizeTransactionType(resolved.type, resolved.gasPrice)
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function createParaRestEthersSigner(options) {
|
|
133
|
+
return new ParaRestEthersSigner(options);
|
|
134
|
+
}
|
|
135
|
+
function normalizeEvmSignature(signature) {
|
|
136
|
+
const prefixed = signature.startsWith("0x") ? signature : `0x${signature}`;
|
|
137
|
+
const parsed = ethers.Signature.from(prefixed);
|
|
138
|
+
return parsed.serialized;
|
|
139
|
+
}
|
|
140
|
+
function quantityToDecimalString(value) {
|
|
141
|
+
if (value == null) {
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
return ethers.toBigInt(value).toString(10);
|
|
145
|
+
}
|
|
146
|
+
function normalizeTransactionType(type, gasPrice) {
|
|
147
|
+
if (type == null) {
|
|
148
|
+
return gasPrice == null ? 2 : 0;
|
|
149
|
+
}
|
|
150
|
+
const numericType = Number(type);
|
|
151
|
+
if (numericType !== 0 && numericType !== 2) {
|
|
152
|
+
throw new ParaRestValidationError("transaction.type must be 0 or 2");
|
|
153
|
+
}
|
|
154
|
+
return numericType;
|
|
155
|
+
}
|
|
156
|
+
function stripEip712Domain(types) {
|
|
157
|
+
const restTypes = {};
|
|
158
|
+
for (const [typeName, fields] of Object.entries(types)) {
|
|
159
|
+
if (typeName === "EIP712Domain") continue;
|
|
160
|
+
restTypes[typeName] = fields.map((field) => ({ name: field.name, type: field.type }));
|
|
161
|
+
}
|
|
162
|
+
return restTypes;
|
|
163
|
+
}
|
|
164
|
+
export {
|
|
165
|
+
ParaRestEthersSigner,
|
|
166
|
+
createParaRestEthersSigner,
|
|
167
|
+
normalizeEvmSignature
|
|
168
|
+
};
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
class ParaRestError extends Error {
|
|
22
|
+
constructor({
|
|
23
|
+
status,
|
|
24
|
+
code,
|
|
25
|
+
requestId,
|
|
26
|
+
body,
|
|
27
|
+
message
|
|
28
|
+
}) {
|
|
29
|
+
var _a;
|
|
30
|
+
super((_a = message != null ? message : code) != null ? _a : `Para REST request failed with status ${status}`);
|
|
31
|
+
this.name = "ParaRestError";
|
|
32
|
+
this.status = status;
|
|
33
|
+
this.code = code;
|
|
34
|
+
this.requestId = requestId;
|
|
35
|
+
this.body = body;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
class ParaRestSerializationError extends Error {
|
|
39
|
+
constructor(message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = "ParaRestSerializationError";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class ParaRestValidationError extends Error {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "ParaRestValidationError";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const BASE_URLS = {
|
|
51
|
+
PROD: "https://api.getpara.com",
|
|
52
|
+
BETA: "https://api.beta.getpara.com",
|
|
53
|
+
SANDBOX: "https://api.sandbox.getpara.com"
|
|
54
|
+
};
|
|
55
|
+
function toRestJson(body) {
|
|
56
|
+
let json;
|
|
57
|
+
try {
|
|
58
|
+
json = JSON.stringify(body, (_key, value) => {
|
|
59
|
+
if (typeof value === "bigint") return value.toString(10);
|
|
60
|
+
if (typeof value === "function") {
|
|
61
|
+
throw new ParaRestSerializationError("REST request body cannot contain functions");
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === "symbol") {
|
|
64
|
+
throw new ParaRestSerializationError("REST request body cannot contain symbols");
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
});
|
|
68
|
+
} catch (err) {
|
|
69
|
+
if (err instanceof ParaRestSerializationError) throw err;
|
|
70
|
+
if (err instanceof TypeError) {
|
|
71
|
+
throw new ParaRestSerializationError("REST request body cannot contain circular references");
|
|
72
|
+
}
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
if (typeof json !== "string") {
|
|
76
|
+
throw new ParaRestSerializationError("REST request body must be JSON serializable");
|
|
77
|
+
}
|
|
78
|
+
return json;
|
|
79
|
+
}
|
|
80
|
+
class ParaRestClient {
|
|
81
|
+
constructor(options) {
|
|
82
|
+
var _a, _b, _c;
|
|
83
|
+
if (!options.apiKey) {
|
|
84
|
+
throw new ParaRestValidationError("apiKey is required");
|
|
85
|
+
}
|
|
86
|
+
this.apiKey = options.apiKey;
|
|
87
|
+
this.baseUrl = resolveBaseUrl((_a = options.env) != null ? _a : "PROD");
|
|
88
|
+
this.fetchImpl = (_b = options.fetch) != null ? _b : getGlobalFetch();
|
|
89
|
+
this.requestIdGenerator = (_c = options.requestIdGenerator) != null ? _c : defaultRequestId;
|
|
90
|
+
}
|
|
91
|
+
createWallet(body, options) {
|
|
92
|
+
requireBody(body, "body");
|
|
93
|
+
requireNonEmpty(body.type, "type");
|
|
94
|
+
requireNonEmpty(body.userIdentifier, "userIdentifier");
|
|
95
|
+
requireNonEmpty(body.userIdentifierType, "userIdentifierType");
|
|
96
|
+
return this.request("POST", "/v1/wallets", { body, options, expectedStatus: 201 });
|
|
97
|
+
}
|
|
98
|
+
listWallets(query, options) {
|
|
99
|
+
return this.request("GET", `/v1/wallets${queryString(query)}`, { options });
|
|
100
|
+
}
|
|
101
|
+
getWallet(walletId, options) {
|
|
102
|
+
return this.request("GET", `/v1/wallets/${walletPath(walletId)}`, { options });
|
|
103
|
+
}
|
|
104
|
+
updateWalletIdentifier(walletId, body, options) {
|
|
105
|
+
requireNonEmpty(walletId, "walletId");
|
|
106
|
+
requireBody(body, "body");
|
|
107
|
+
requireNonEmpty(body.userIdentifier, "userIdentifier");
|
|
108
|
+
requireNonEmpty(body.userIdentifierType, "userIdentifierType");
|
|
109
|
+
return this.request("PATCH", `/v1/wallets/${walletPath(walletId)}`, { body, options });
|
|
110
|
+
}
|
|
111
|
+
migrateShare(walletId, body, options) {
|
|
112
|
+
requireNonEmpty(walletId, "walletId");
|
|
113
|
+
requireBody(body, "body");
|
|
114
|
+
requireNonEmpty(body.encryptedPayload, "encryptedPayload");
|
|
115
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/migrate-share`, { body, options });
|
|
116
|
+
}
|
|
117
|
+
getEnclavePublicKey(options) {
|
|
118
|
+
return this.request("GET", "/v1/enclave/public-key", { options });
|
|
119
|
+
}
|
|
120
|
+
getWalletBalance(walletId, query, options) {
|
|
121
|
+
return this.request("GET", `/v1/wallets/${walletPath(walletId)}/balance${queryString(query)}`, { options });
|
|
122
|
+
}
|
|
123
|
+
signRaw(walletId, body, options) {
|
|
124
|
+
requireNonEmpty(walletId, "walletId");
|
|
125
|
+
requireBody(body, "body");
|
|
126
|
+
requireNonEmpty(body.data, "data");
|
|
127
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/sign-raw`, { body, options });
|
|
128
|
+
}
|
|
129
|
+
signMessage(walletId, body, options) {
|
|
130
|
+
requireNonEmpty(walletId, "walletId");
|
|
131
|
+
requireBody(body, "body");
|
|
132
|
+
requireNonEmpty(body.message, "message");
|
|
133
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/sign-message`, { body, options });
|
|
134
|
+
}
|
|
135
|
+
signTransaction(walletId, body, options) {
|
|
136
|
+
requireNonEmpty(walletId, "walletId");
|
|
137
|
+
requireBody(body, "body");
|
|
138
|
+
if (body.transaction == null || body.transaction === "") {
|
|
139
|
+
throw new ParaRestValidationError("transaction is required");
|
|
140
|
+
}
|
|
141
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/sign-transaction`, { body, options });
|
|
142
|
+
}
|
|
143
|
+
signTypedData(walletId, body, options) {
|
|
144
|
+
requireNonEmpty(walletId, "walletId");
|
|
145
|
+
requireBody(body, "body");
|
|
146
|
+
requireBody(body.typedData, "typedData");
|
|
147
|
+
requireBody(body.typedData.domain, "typedData.domain");
|
|
148
|
+
requireBody(body.typedData.types, "typedData.types");
|
|
149
|
+
requireNonEmpty(body.typedData.primaryType, "typedData.primaryType");
|
|
150
|
+
requireBody(body.typedData.message, "typedData.message");
|
|
151
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/sign-typed-data`, { body, options });
|
|
152
|
+
}
|
|
153
|
+
signAuthorization(walletId, body, options) {
|
|
154
|
+
requireNonEmpty(walletId, "walletId");
|
|
155
|
+
requireBody(body, "body");
|
|
156
|
+
requireBody(body.authorization, "authorization");
|
|
157
|
+
if (!body.authorization.address && !body.authorization.contractAddress) {
|
|
158
|
+
throw new ParaRestValidationError("authorization.address is required");
|
|
159
|
+
}
|
|
160
|
+
if (body.authorization.chainId == null) {
|
|
161
|
+
throw new ParaRestValidationError("authorization.chainId is required");
|
|
162
|
+
}
|
|
163
|
+
if (body.authorization.nonce == null) {
|
|
164
|
+
throw new ParaRestValidationError("authorization.nonce is required");
|
|
165
|
+
}
|
|
166
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/sign-authorization`, { body, options });
|
|
167
|
+
}
|
|
168
|
+
transfer(walletId, body, options) {
|
|
169
|
+
requireNonEmpty(walletId, "walletId");
|
|
170
|
+
requireBody(body, "body");
|
|
171
|
+
requireNonEmpty(body.to, "to");
|
|
172
|
+
requireNonEmpty(body.value, "value");
|
|
173
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/transfer`, { body, options });
|
|
174
|
+
}
|
|
175
|
+
estimateFee(walletId, body, options) {
|
|
176
|
+
requireNonEmpty(walletId, "walletId");
|
|
177
|
+
requireBody(body, "body");
|
|
178
|
+
requireNonEmpty(body.to, "to");
|
|
179
|
+
requireNonEmpty(body.value, "value");
|
|
180
|
+
return this.request("POST", `/v1/wallets/${walletPath(walletId)}/estimate-fee`, { body, options });
|
|
181
|
+
}
|
|
182
|
+
requestFaucet(body, options) {
|
|
183
|
+
requireBody(body, "body");
|
|
184
|
+
requireNonEmpty(body.walletId, "walletId");
|
|
185
|
+
return this.request("POST", "/faucet", { body, options });
|
|
186
|
+
}
|
|
187
|
+
listRestTransactions(walletId, query, options) {
|
|
188
|
+
return this.request("GET", `/v1/wallets/${walletPath(walletId)}/transactions${queryString(query)}`, { options });
|
|
189
|
+
}
|
|
190
|
+
getRestTransaction(walletId, transactionId, options) {
|
|
191
|
+
return this.request(
|
|
192
|
+
"GET",
|
|
193
|
+
`/v1/wallets/${walletPath(walletId)}/transactions/${pathSegment("transactionId", transactionId)}`,
|
|
194
|
+
{
|
|
195
|
+
options
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
request(_0, _1) {
|
|
200
|
+
return __async(this, arguments, function* (method, path, {
|
|
201
|
+
body,
|
|
202
|
+
options,
|
|
203
|
+
expectedStatus
|
|
204
|
+
} = {}) {
|
|
205
|
+
var _a;
|
|
206
|
+
const requestId = this.requestIdGenerator();
|
|
207
|
+
const headers = {
|
|
208
|
+
"Accept": "application/json",
|
|
209
|
+
"X-API-Key": this.apiKey,
|
|
210
|
+
"X-Request-Id": requestId
|
|
211
|
+
};
|
|
212
|
+
let requestBody;
|
|
213
|
+
if (body !== void 0) {
|
|
214
|
+
requestBody = toRestJson(body);
|
|
215
|
+
headers["Content-Type"] = "application/json";
|
|
216
|
+
}
|
|
217
|
+
if (options == null ? void 0 : options.idempotencyKey) {
|
|
218
|
+
headers["Idempotency-Key"] = options.idempotencyKey;
|
|
219
|
+
}
|
|
220
|
+
const response = yield this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
221
|
+
body: requestBody,
|
|
222
|
+
headers,
|
|
223
|
+
method,
|
|
224
|
+
signal: options == null ? void 0 : options.signal
|
|
225
|
+
});
|
|
226
|
+
const parsed = yield parseBody(response);
|
|
227
|
+
if (!response.ok || expectedStatus != null && response.status !== expectedStatus) {
|
|
228
|
+
const responseBody = isRecord(parsed) ? parsed : {};
|
|
229
|
+
throw new ParaRestError({
|
|
230
|
+
status: response.status,
|
|
231
|
+
code: stringField(responseBody.code),
|
|
232
|
+
requestId: (_a = response.headers.get("x-request-id")) != null ? _a : requestId,
|
|
233
|
+
body: parsed,
|
|
234
|
+
message: stringField(responseBody.message)
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return parsed;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function resolveBaseUrl(env) {
|
|
242
|
+
const baseUrl = typeof env === "string" ? BASE_URLS[env] : env.baseUrl;
|
|
243
|
+
if (!baseUrl) {
|
|
244
|
+
throw new ParaRestValidationError("baseUrl is required");
|
|
245
|
+
}
|
|
246
|
+
return baseUrl.replace(/\/+$/, "");
|
|
247
|
+
}
|
|
248
|
+
function getGlobalFetch() {
|
|
249
|
+
const fetchImpl = globalThis.fetch;
|
|
250
|
+
if (!fetchImpl) {
|
|
251
|
+
throw new ParaRestValidationError("fetch is required in this runtime; pass options.fetch");
|
|
252
|
+
}
|
|
253
|
+
return fetchImpl;
|
|
254
|
+
}
|
|
255
|
+
function defaultRequestId() {
|
|
256
|
+
const crypto = globalThis.crypto;
|
|
257
|
+
if (crypto == null ? void 0 : crypto.randomUUID) {
|
|
258
|
+
return crypto.randomUUID();
|
|
259
|
+
}
|
|
260
|
+
return `req_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;
|
|
261
|
+
}
|
|
262
|
+
function walletPath(walletId) {
|
|
263
|
+
return pathSegment("walletId", walletId);
|
|
264
|
+
}
|
|
265
|
+
function pathSegment(name, value) {
|
|
266
|
+
requireNonEmpty(value, name);
|
|
267
|
+
return encodeURIComponent(value);
|
|
268
|
+
}
|
|
269
|
+
function requireBody(value, name) {
|
|
270
|
+
if (!isRecord(value)) {
|
|
271
|
+
throw new ParaRestValidationError(`${name} is required`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function requireNonEmpty(value, name) {
|
|
275
|
+
if (value == null || value === "") {
|
|
276
|
+
throw new ParaRestValidationError(`${name} is required`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
function queryString(query) {
|
|
280
|
+
if (!query) return "";
|
|
281
|
+
const params = new URLSearchParams();
|
|
282
|
+
for (const [key, value] of Object.entries(query)) {
|
|
283
|
+
if (value == null) continue;
|
|
284
|
+
params.set(key, String(value));
|
|
285
|
+
}
|
|
286
|
+
const serialized = params.toString();
|
|
287
|
+
return serialized ? `?${serialized}` : "";
|
|
288
|
+
}
|
|
289
|
+
function parseBody(response) {
|
|
290
|
+
return __async(this, null, function* () {
|
|
291
|
+
const text = yield response.text();
|
|
292
|
+
if (!text) return void 0;
|
|
293
|
+
try {
|
|
294
|
+
return JSON.parse(text);
|
|
295
|
+
} catch (e) {
|
|
296
|
+
return text;
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
function isRecord(value) {
|
|
301
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
302
|
+
}
|
|
303
|
+
function stringField(value) {
|
|
304
|
+
return typeof value === "string" ? value : void 0;
|
|
305
|
+
}
|
|
306
|
+
export {
|
|
307
|
+
ParaRestClient,
|
|
308
|
+
ParaRestError,
|
|
309
|
+
ParaRestSerializationError,
|
|
310
|
+
ParaRestValidationError,
|
|
311
|
+
toRestJson
|
|
312
|
+
};
|