@imtbl/toolkit 2.0.0-alpha.5 → 2.0.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.js +71 -4
- package/dist/node/index.cjs +91 -24
- package/dist/node/index.js +71 -4
- package/package.json +2 -2
package/dist/browser/index.js
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
import * as encUtils from 'enc-utils';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// src/crypto.ts
|
|
5
|
+
function serializeEthSignature(sig) {
|
|
6
|
+
return encUtils.addHexPrefix(
|
|
7
|
+
encUtils.padLeft(sig.r.toString(16), 64) + encUtils.padLeft(sig.s.toString(16), 64) + encUtils.padLeft(sig.recoveryParam?.toString(16) || "", 2)
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
function importRecoveryParam(v) {
|
|
11
|
+
const isValidBigNumber = new BN(v, 16).cmp(new BN(27)) !== -1 ? new BN(v, 16).sub(new BN(27)).toNumber() : new BN(v, 16).toNumber();
|
|
12
|
+
return v.trim() ? isValidBigNumber : void 0;
|
|
13
|
+
}
|
|
14
|
+
function deserializeSignature(sig, size = 64) {
|
|
15
|
+
const removedHexPrefixSig = encUtils.removeHexPrefix(sig);
|
|
16
|
+
return {
|
|
17
|
+
r: new BN(removedHexPrefixSig.substring(0, size), "hex"),
|
|
18
|
+
s: new BN(removedHexPrefixSig.substring(size, size * 2), "hex"),
|
|
19
|
+
recoveryParam: importRecoveryParam(removedHexPrefixSig.substring(size * 2, size * 2 + 2))
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
async function signRaw(payload, signer) {
|
|
23
|
+
const signature = deserializeSignature(await signer.signMessage(payload));
|
|
24
|
+
return serializeEthSignature(signature);
|
|
25
|
+
}
|
|
26
|
+
async function generateIMXAuthorisationHeaders(ethSigner) {
|
|
27
|
+
const timestamp = Math.floor(Date.now() / 1e3).toString();
|
|
28
|
+
const signature = await signRaw(timestamp, ethSigner);
|
|
29
|
+
return {
|
|
30
|
+
timestamp,
|
|
31
|
+
signature
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async function signMessage(message, signer) {
|
|
35
|
+
const ethAddress = await signer.getAddress();
|
|
36
|
+
const ethSignature = await signRaw(message, signer);
|
|
37
|
+
return {
|
|
38
|
+
message,
|
|
39
|
+
ethAddress,
|
|
40
|
+
ethSignature
|
|
41
|
+
};
|
|
42
|
+
}
|
|
5
43
|
|
|
6
|
-
|
|
44
|
+
// src/convertToSignableToken.ts
|
|
45
|
+
function convertToSignableToken(token) {
|
|
46
|
+
switch (token.type) {
|
|
47
|
+
case "ERC721":
|
|
48
|
+
return {
|
|
49
|
+
type: "ERC721",
|
|
50
|
+
data: {
|
|
51
|
+
token_id: token.tokenId,
|
|
52
|
+
token_address: token.tokenAddress
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
case "ERC20":
|
|
56
|
+
return {
|
|
57
|
+
type: "ERC20",
|
|
58
|
+
data: {
|
|
59
|
+
token_address: token.tokenAddress
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
case "ETH":
|
|
63
|
+
default:
|
|
64
|
+
return {
|
|
65
|
+
type: "ETH",
|
|
66
|
+
data: {
|
|
67
|
+
decimals: 18
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { convertToSignableToken, generateIMXAuthorisationHeaders, signMessage, signRaw };
|
package/dist/node/index.cjs
CHANGED
|
@@ -1,34 +1,101 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var BN = require('bn.js');
|
|
4
|
+
var encUtils = require('enc-utils');
|
|
5
5
|
|
|
6
6
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
7
|
|
|
8
8
|
function _interopNamespace(e) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
9
|
+
if (e && e.__esModule) return e;
|
|
10
|
+
var n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return e[k]; }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
n.default = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
var
|
|
27
|
-
var
|
|
26
|
+
var BN__default = /*#__PURE__*/_interopDefault(BN);
|
|
27
|
+
var encUtils__namespace = /*#__PURE__*/_interopNamespace(encUtils);
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
// src/crypto.ts
|
|
30
|
+
function serializeEthSignature(sig) {
|
|
31
|
+
return encUtils__namespace.addHexPrefix(
|
|
32
|
+
encUtils__namespace.padLeft(sig.r.toString(16), 64) + encUtils__namespace.padLeft(sig.s.toString(16), 64) + encUtils__namespace.padLeft(sig.recoveryParam?.toString(16) || "", 2)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
function importRecoveryParam(v) {
|
|
36
|
+
const isValidBigNumber = new BN__default.default(v, 16).cmp(new BN__default.default(27)) !== -1 ? new BN__default.default(v, 16).sub(new BN__default.default(27)).toNumber() : new BN__default.default(v, 16).toNumber();
|
|
37
|
+
return v.trim() ? isValidBigNumber : void 0;
|
|
38
|
+
}
|
|
39
|
+
function deserializeSignature(sig, size = 64) {
|
|
40
|
+
const removedHexPrefixSig = encUtils__namespace.removeHexPrefix(sig);
|
|
41
|
+
return {
|
|
42
|
+
r: new BN__default.default(removedHexPrefixSig.substring(0, size), "hex"),
|
|
43
|
+
s: new BN__default.default(removedHexPrefixSig.substring(size, size * 2), "hex"),
|
|
44
|
+
recoveryParam: importRecoveryParam(removedHexPrefixSig.substring(size * 2, size * 2 + 2))
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function signRaw(payload, signer) {
|
|
48
|
+
const signature = deserializeSignature(await signer.signMessage(payload));
|
|
49
|
+
return serializeEthSignature(signature);
|
|
50
|
+
}
|
|
51
|
+
async function generateIMXAuthorisationHeaders(ethSigner) {
|
|
52
|
+
const timestamp = Math.floor(Date.now() / 1e3).toString();
|
|
53
|
+
const signature = await signRaw(timestamp, ethSigner);
|
|
54
|
+
return {
|
|
55
|
+
timestamp,
|
|
56
|
+
signature
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async function signMessage(message, signer) {
|
|
60
|
+
const ethAddress = await signer.getAddress();
|
|
61
|
+
const ethSignature = await signRaw(message, signer);
|
|
62
|
+
return {
|
|
63
|
+
message,
|
|
64
|
+
ethAddress,
|
|
65
|
+
ethSignature
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/convertToSignableToken.ts
|
|
70
|
+
function convertToSignableToken(token) {
|
|
71
|
+
switch (token.type) {
|
|
72
|
+
case "ERC721":
|
|
73
|
+
return {
|
|
74
|
+
type: "ERC721",
|
|
75
|
+
data: {
|
|
76
|
+
token_id: token.tokenId,
|
|
77
|
+
token_address: token.tokenAddress
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
case "ERC20":
|
|
81
|
+
return {
|
|
82
|
+
type: "ERC20",
|
|
83
|
+
data: {
|
|
84
|
+
token_address: token.tokenAddress
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
case "ETH":
|
|
88
|
+
default:
|
|
89
|
+
return {
|
|
90
|
+
type: "ETH",
|
|
91
|
+
data: {
|
|
92
|
+
decimals: 18
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
30
97
|
|
|
31
|
-
exports.convertToSignableToken =
|
|
32
|
-
exports.generateIMXAuthorisationHeaders =
|
|
33
|
-
exports.signMessage =
|
|
34
|
-
exports.signRaw =
|
|
98
|
+
exports.convertToSignableToken = convertToSignableToken;
|
|
99
|
+
exports.generateIMXAuthorisationHeaders = generateIMXAuthorisationHeaders;
|
|
100
|
+
exports.signMessage = signMessage;
|
|
101
|
+
exports.signRaw = signRaw;
|
package/dist/node/index.js
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
import * as encUtils from 'enc-utils';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// src/crypto.ts
|
|
5
|
+
function serializeEthSignature(sig) {
|
|
6
|
+
return encUtils.addHexPrefix(
|
|
7
|
+
encUtils.padLeft(sig.r.toString(16), 64) + encUtils.padLeft(sig.s.toString(16), 64) + encUtils.padLeft(sig.recoveryParam?.toString(16) || "", 2)
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
function importRecoveryParam(v) {
|
|
11
|
+
const isValidBigNumber = new BN(v, 16).cmp(new BN(27)) !== -1 ? new BN(v, 16).sub(new BN(27)).toNumber() : new BN(v, 16).toNumber();
|
|
12
|
+
return v.trim() ? isValidBigNumber : void 0;
|
|
13
|
+
}
|
|
14
|
+
function deserializeSignature(sig, size = 64) {
|
|
15
|
+
const removedHexPrefixSig = encUtils.removeHexPrefix(sig);
|
|
16
|
+
return {
|
|
17
|
+
r: new BN(removedHexPrefixSig.substring(0, size), "hex"),
|
|
18
|
+
s: new BN(removedHexPrefixSig.substring(size, size * 2), "hex"),
|
|
19
|
+
recoveryParam: importRecoveryParam(removedHexPrefixSig.substring(size * 2, size * 2 + 2))
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
async function signRaw(payload, signer) {
|
|
23
|
+
const signature = deserializeSignature(await signer.signMessage(payload));
|
|
24
|
+
return serializeEthSignature(signature);
|
|
25
|
+
}
|
|
26
|
+
async function generateIMXAuthorisationHeaders(ethSigner) {
|
|
27
|
+
const timestamp = Math.floor(Date.now() / 1e3).toString();
|
|
28
|
+
const signature = await signRaw(timestamp, ethSigner);
|
|
29
|
+
return {
|
|
30
|
+
timestamp,
|
|
31
|
+
signature
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async function signMessage(message, signer) {
|
|
35
|
+
const ethAddress = await signer.getAddress();
|
|
36
|
+
const ethSignature = await signRaw(message, signer);
|
|
37
|
+
return {
|
|
38
|
+
message,
|
|
39
|
+
ethAddress,
|
|
40
|
+
ethSignature
|
|
41
|
+
};
|
|
42
|
+
}
|
|
5
43
|
|
|
6
|
-
|
|
44
|
+
// src/convertToSignableToken.ts
|
|
45
|
+
function convertToSignableToken(token) {
|
|
46
|
+
switch (token.type) {
|
|
47
|
+
case "ERC721":
|
|
48
|
+
return {
|
|
49
|
+
type: "ERC721",
|
|
50
|
+
data: {
|
|
51
|
+
token_id: token.tokenId,
|
|
52
|
+
token_address: token.tokenAddress
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
case "ERC20":
|
|
56
|
+
return {
|
|
57
|
+
type: "ERC20",
|
|
58
|
+
data: {
|
|
59
|
+
token_address: token.tokenAddress
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
case "ETH":
|
|
63
|
+
default:
|
|
64
|
+
return {
|
|
65
|
+
type: "ETH",
|
|
66
|
+
data: {
|
|
67
|
+
decimals: 18
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { convertToSignableToken, generateIMXAuthorisationHeaders, signMessage, signRaw };
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imtbl/toolkit",
|
|
3
3
|
"description": "Provider package for Immutable SDK",
|
|
4
|
-
"version": "2.0.0-alpha.
|
|
4
|
+
"version": "2.0.0-alpha.7",
|
|
5
5
|
"author": "Immutable",
|
|
6
6
|
"bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@imtbl/x-client": "2.0.0-alpha.
|
|
8
|
+
"@imtbl/x-client": "2.0.0-alpha.7",
|
|
9
9
|
"@magic-ext/oidc": "4.3.1",
|
|
10
10
|
"@metamask/detect-provider": "^2.0.0",
|
|
11
11
|
"axios": "^1.6.5",
|