@ring-protocol/permit2-sdk 0.1.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/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/cjs/abis/Permit2.json +830 -0
- package/dist/cjs/src/allowanceTransfer.d.ts +36 -0
- package/dist/cjs/src/allowanceTransfer.js +72 -0
- package/dist/cjs/src/allowanceTransfer.js.map +1 -0
- package/dist/cjs/src/allowanceTransfer.test.d.ts +1 -0
- package/dist/cjs/src/allowanceTransfer.test.js +95 -0
- package/dist/cjs/src/allowanceTransfer.test.js.map +1 -0
- package/dist/cjs/src/constants.d.ts +13 -0
- package/dist/cjs/src/constants.js +29 -0
- package/dist/cjs/src/constants.js.map +1 -0
- package/dist/cjs/src/constants.test.d.ts +1 -0
- package/dist/cjs/src/constants.test.js +19 -0
- package/dist/cjs/src/constants.test.js.map +1 -0
- package/dist/cjs/src/domain.d.ts +7 -0
- package/dist/cjs/src/domain.js +13 -0
- package/dist/cjs/src/domain.js.map +1 -0
- package/dist/cjs/src/index.d.ts +4 -0
- package/dist/cjs/src/index.js +8 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/src/providers/AllowanceProvider.d.ts +17 -0
- package/dist/cjs/src/providers/AllowanceProvider.js +35 -0
- package/dist/cjs/src/providers/AllowanceProvider.js.map +1 -0
- package/dist/cjs/src/providers/SignatureProvider.d.ts +74 -0
- package/dist/cjs/src/providers/SignatureProvider.js +146 -0
- package/dist/cjs/src/providers/SignatureProvider.js.map +1 -0
- package/dist/cjs/src/providers/SignatureProvider.test.d.ts +1 -0
- package/dist/cjs/src/providers/SignatureProvider.test.js +247 -0
- package/dist/cjs/src/providers/SignatureProvider.test.js.map +1 -0
- package/dist/cjs/src/providers/index.d.ts +2 -0
- package/dist/cjs/src/providers/index.js +6 -0
- package/dist/cjs/src/providers/index.js.map +1 -0
- package/dist/cjs/src/signatureTransfer.d.ts +41 -0
- package/dist/cjs/src/signatureTransfer.js +93 -0
- package/dist/cjs/src/signatureTransfer.js.map +1 -0
- package/dist/cjs/src/signatureTransfer.test.d.ts +1 -0
- package/dist/cjs/src/signatureTransfer.test.js +109 -0
- package/dist/cjs/src/signatureTransfer.test.js.map +1 -0
- package/dist/esm/abis/Permit2.json +830 -0
- package/dist/esm/src/allowanceTransfer.d.ts +36 -0
- package/dist/esm/src/allowanceTransfer.js +67 -0
- package/dist/esm/src/allowanceTransfer.js.map +1 -0
- package/dist/esm/src/allowanceTransfer.test.d.ts +1 -0
- package/dist/esm/src/allowanceTransfer.test.js +93 -0
- package/dist/esm/src/allowanceTransfer.test.js.map +1 -0
- package/dist/esm/src/constants.d.ts +13 -0
- package/dist/esm/src/constants.js +25 -0
- package/dist/esm/src/constants.js.map +1 -0
- package/dist/esm/src/constants.test.d.ts +1 -0
- package/dist/esm/src/constants.test.js +17 -0
- package/dist/esm/src/constants.test.js.map +1 -0
- package/dist/esm/src/domain.d.ts +7 -0
- package/dist/esm/src/domain.js +9 -0
- package/dist/esm/src/domain.js.map +1 -0
- package/dist/esm/src/index.d.ts +4 -0
- package/dist/esm/src/index.js +5 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/providers/AllowanceProvider.d.ts +17 -0
- package/dist/esm/src/providers/AllowanceProvider.js +31 -0
- package/dist/esm/src/providers/AllowanceProvider.js.map +1 -0
- package/dist/esm/src/providers/SignatureProvider.d.ts +74 -0
- package/dist/esm/src/providers/SignatureProvider.js +142 -0
- package/dist/esm/src/providers/SignatureProvider.js.map +1 -0
- package/dist/esm/src/providers/SignatureProvider.test.d.ts +1 -0
- package/dist/esm/src/providers/SignatureProvider.test.js +245 -0
- package/dist/esm/src/providers/SignatureProvider.test.js.map +1 -0
- package/dist/esm/src/providers/index.d.ts +2 -0
- package/dist/esm/src/providers/index.js +3 -0
- package/dist/esm/src/providers/index.js.map +1 -0
- package/dist/esm/src/signatureTransfer.d.ts +41 -0
- package/dist/esm/src/signatureTransfer.js +88 -0
- package/dist/esm/src/signatureTransfer.js.map +1 -0
- package/dist/esm/src/signatureTransfer.test.d.ts +1 -0
- package/dist/esm/src/signatureTransfer.test.js +107 -0
- package/dist/esm/src/signatureTransfer.test.js.map +1 -0
- package/dist/types/src/allowanceTransfer.d.ts +36 -0
- package/dist/types/src/allowanceTransfer.test.d.ts +1 -0
- package/dist/types/src/constants.d.ts +13 -0
- package/dist/types/src/constants.test.d.ts +1 -0
- package/dist/types/src/domain.d.ts +7 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/providers/AllowanceProvider.d.ts +17 -0
- package/dist/types/src/providers/SignatureProvider.d.ts +74 -0
- package/dist/types/src/providers/SignatureProvider.test.d.ts +1 -0
- package/dist/types/src/providers/index.d.ts +2 -0
- package/dist/types/src/signatureTransfer.d.ts +41 -0
- package/dist/types/src/signatureTransfer.test.d.ts +1 -0
- package/package.json +92 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { TypedDataDomain, TypedDataField } from '@ethersproject/abstract-signer';
|
|
2
|
+
import { BigNumberish } from '@ethersproject/bignumber';
|
|
3
|
+
export interface PermitDetails {
|
|
4
|
+
token: string;
|
|
5
|
+
amount: BigNumberish;
|
|
6
|
+
expiration: BigNumberish;
|
|
7
|
+
nonce: BigNumberish;
|
|
8
|
+
}
|
|
9
|
+
export interface PermitSingle {
|
|
10
|
+
details: PermitDetails;
|
|
11
|
+
spender: string;
|
|
12
|
+
sigDeadline: BigNumberish;
|
|
13
|
+
}
|
|
14
|
+
export interface PermitBatch {
|
|
15
|
+
details: PermitDetails[];
|
|
16
|
+
spender: string;
|
|
17
|
+
sigDeadline: BigNumberish;
|
|
18
|
+
}
|
|
19
|
+
export type PermitSingleData = {
|
|
20
|
+
domain: TypedDataDomain;
|
|
21
|
+
types: Record<string, TypedDataField[]>;
|
|
22
|
+
values: PermitSingle;
|
|
23
|
+
};
|
|
24
|
+
export type PermitBatchData = {
|
|
25
|
+
domain: TypedDataDomain;
|
|
26
|
+
types: Record<string, TypedDataField[]>;
|
|
27
|
+
values: PermitBatch;
|
|
28
|
+
};
|
|
29
|
+
export declare abstract class AllowanceTransfer {
|
|
30
|
+
/**
|
|
31
|
+
* Cannot be constructed.
|
|
32
|
+
*/
|
|
33
|
+
private constructor();
|
|
34
|
+
static getPermitData(permit: PermitSingle | PermitBatch, permit2Address: string, chainId: number): PermitSingleData | PermitBatchData;
|
|
35
|
+
static hash(permit: PermitSingle | PermitBatch, permit2Address: string, chainId: number): string;
|
|
36
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AllowanceTransfer = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
|
|
6
|
+
const hash_1 = require("@ethersproject/hash");
|
|
7
|
+
const constants_1 = require("./constants");
|
|
8
|
+
const domain_1 = require("./domain");
|
|
9
|
+
const PERMIT_DETAILS = [
|
|
10
|
+
{ name: 'token', type: 'address' },
|
|
11
|
+
{ name: 'amount', type: 'uint160' },
|
|
12
|
+
{ name: 'expiration', type: 'uint48' },
|
|
13
|
+
{ name: 'nonce', type: 'uint48' },
|
|
14
|
+
];
|
|
15
|
+
const PERMIT_TYPES = {
|
|
16
|
+
PermitSingle: [
|
|
17
|
+
{ name: 'details', type: 'PermitDetails' },
|
|
18
|
+
{ name: 'spender', type: 'address' },
|
|
19
|
+
{ name: 'sigDeadline', type: 'uint256' },
|
|
20
|
+
],
|
|
21
|
+
PermitDetails: PERMIT_DETAILS,
|
|
22
|
+
};
|
|
23
|
+
const PERMIT_BATCH_TYPES = {
|
|
24
|
+
PermitBatch: [
|
|
25
|
+
{ name: 'details', type: 'PermitDetails[]' },
|
|
26
|
+
{ name: 'spender', type: 'address' },
|
|
27
|
+
{ name: 'sigDeadline', type: 'uint256' },
|
|
28
|
+
],
|
|
29
|
+
PermitDetails: PERMIT_DETAILS,
|
|
30
|
+
};
|
|
31
|
+
function isPermit(permit) {
|
|
32
|
+
return !Array.isArray(permit.details);
|
|
33
|
+
}
|
|
34
|
+
class AllowanceTransfer {
|
|
35
|
+
/**
|
|
36
|
+
* Cannot be constructed.
|
|
37
|
+
*/
|
|
38
|
+
constructor() { }
|
|
39
|
+
// return the data to be sent in a eth_signTypedData RPC call
|
|
40
|
+
// for signing the given permit data
|
|
41
|
+
static getPermitData(permit, permit2Address, chainId) {
|
|
42
|
+
(0, tiny_invariant_1.default)(constants_1.MaxSigDeadline.gte(permit.sigDeadline), 'SIG_DEADLINE_OUT_OF_RANGE');
|
|
43
|
+
const domain = (0, domain_1.permit2Domain)(permit2Address, chainId);
|
|
44
|
+
if (isPermit(permit)) {
|
|
45
|
+
validatePermitDetails(permit.details);
|
|
46
|
+
return {
|
|
47
|
+
domain,
|
|
48
|
+
types: PERMIT_TYPES,
|
|
49
|
+
values: permit,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
permit.details.forEach(validatePermitDetails);
|
|
54
|
+
return {
|
|
55
|
+
domain,
|
|
56
|
+
types: PERMIT_BATCH_TYPES,
|
|
57
|
+
values: permit,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
static hash(permit, permit2Address, chainId) {
|
|
62
|
+
const { domain, types, values } = AllowanceTransfer.getPermitData(permit, permit2Address, chainId);
|
|
63
|
+
return hash_1._TypedDataEncoder.hash(domain, types, values);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.AllowanceTransfer = AllowanceTransfer;
|
|
67
|
+
function validatePermitDetails(details) {
|
|
68
|
+
(0, tiny_invariant_1.default)(constants_1.MaxOrderedNonce.gte(details.nonce), 'NONCE_OUT_OF_RANGE');
|
|
69
|
+
(0, tiny_invariant_1.default)(constants_1.MaxAllowanceTransferAmount.gte(details.amount), 'AMOUNT_OUT_OF_RANGE');
|
|
70
|
+
(0, tiny_invariant_1.default)(constants_1.MaxAllowanceExpiration.gte(details.expiration), 'EXPIRATION_OUT_OF_RANGE');
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=allowanceTransfer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allowanceTransfer.js","sourceRoot":"","sources":["../../../src/allowanceTransfer.ts"],"names":[],"mappings":";;;;AAAA,4EAAsC;AAGtC,8CAAuD;AACvD,2CAAiH;AACjH,qCAAwC;AAiCxC,MAAM,cAAc,GAAG;IACrB,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;IAClC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE;IACnC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE;IACtC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE;CAClC,CAAA;AAED,MAAM,YAAY,GAAG;IACnB,YAAY,EAAE;QACZ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,eAAe,EAAE;QAC1C,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;QACpC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE;KACzC;IACD,aAAa,EAAE,cAAc;CAC9B,CAAA;AAED,MAAM,kBAAkB,GAAG;IACzB,WAAW,EAAE;QACX,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,iBAAiB,EAAE;QAC5C,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;QACpC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE;KACzC;IACD,aAAa,EAAE,cAAc;CAC9B,CAAA;AAED,SAAS,QAAQ,CAAC,MAAkC;IAClD,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;AACvC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AA,wBAAS,EAAC,kCAAsB,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,yBAAyB,CAAC,CAAA;AACtF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const allowanceTransfer_1 = require("./allowanceTransfer");
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
describe('AllowanceTransfer', () => {
|
|
6
|
+
describe('Max values', () => {
|
|
7
|
+
it('max values pass', () => {
|
|
8
|
+
expect(() => allowanceTransfer_1.AllowanceTransfer.hash({
|
|
9
|
+
details: {
|
|
10
|
+
token: '0x0000000000000000000000000000000000000000',
|
|
11
|
+
amount: constants_1.MaxAllowanceTransferAmount.toString(),
|
|
12
|
+
expiration: constants_1.MaxAllowanceExpiration.toString(),
|
|
13
|
+
nonce: constants_1.MaxOrderedNonce.toString(),
|
|
14
|
+
},
|
|
15
|
+
spender: '0x0000000000000000000000000000000000000000',
|
|
16
|
+
sigDeadline: constants_1.MaxSigDeadline.toString(),
|
|
17
|
+
}, '0x0000000000000000000000000000000000000000', 1)).not.toThrow();
|
|
18
|
+
});
|
|
19
|
+
it('nonce out of range', () => {
|
|
20
|
+
expect(() => allowanceTransfer_1.AllowanceTransfer.hash({
|
|
21
|
+
details: {
|
|
22
|
+
token: '0x0000000000000000000000000000000000000000',
|
|
23
|
+
amount: '0',
|
|
24
|
+
expiration: '0',
|
|
25
|
+
nonce: constants_1.MaxOrderedNonce.add(1).toString(),
|
|
26
|
+
},
|
|
27
|
+
spender: '0x0000000000000000000000000000000000000000',
|
|
28
|
+
sigDeadline: '0',
|
|
29
|
+
}, '0x0000000000000000000000000000000000000000', 1)).toThrow('NONCE_OUT_OF_RANGE');
|
|
30
|
+
});
|
|
31
|
+
it('amount out of range', () => {
|
|
32
|
+
expect(() => allowanceTransfer_1.AllowanceTransfer.hash({
|
|
33
|
+
details: {
|
|
34
|
+
token: '0x0000000000000000000000000000000000000000',
|
|
35
|
+
amount: constants_1.MaxAllowanceTransferAmount.add(1).toString(),
|
|
36
|
+
expiration: '0',
|
|
37
|
+
nonce: 0,
|
|
38
|
+
},
|
|
39
|
+
spender: '0x0000000000000000000000000000000000000000',
|
|
40
|
+
sigDeadline: '0',
|
|
41
|
+
}, '0x0000000000000000000000000000000000000000', 1)).toThrow('AMOUNT_OUT_OF_RANGE');
|
|
42
|
+
});
|
|
43
|
+
it('expiration out of range', () => {
|
|
44
|
+
expect(() => allowanceTransfer_1.AllowanceTransfer.hash({
|
|
45
|
+
details: {
|
|
46
|
+
token: '0x0000000000000000000000000000000000000000',
|
|
47
|
+
amount: '0',
|
|
48
|
+
expiration: constants_1.MaxAllowanceExpiration.add(1).toString(),
|
|
49
|
+
nonce: 0,
|
|
50
|
+
},
|
|
51
|
+
spender: '0x0000000000000000000000000000000000000000',
|
|
52
|
+
sigDeadline: '0',
|
|
53
|
+
}, '0x0000000000000000000000000000000000000000', 1)).toThrow('EXPIRATION_OUT_OF_RANGE');
|
|
54
|
+
});
|
|
55
|
+
it('sigDeadline out of range', () => {
|
|
56
|
+
expect(() => allowanceTransfer_1.AllowanceTransfer.hash({
|
|
57
|
+
details: {
|
|
58
|
+
token: '0x0000000000000000000000000000000000000000',
|
|
59
|
+
amount: '0',
|
|
60
|
+
expiration: '0',
|
|
61
|
+
nonce: 0,
|
|
62
|
+
},
|
|
63
|
+
spender: '0x0000000000000000000000000000000000000000',
|
|
64
|
+
sigDeadline: constants_1.MaxSigDeadline.add(1).toString(),
|
|
65
|
+
}, '0x0000000000000000000000000000000000000000', 1)).toThrow('SIG_DEADLINE_OUT_OF_RANGE');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
it('non-batch', () => {
|
|
69
|
+
expect(allowanceTransfer_1.AllowanceTransfer.hash({
|
|
70
|
+
details: {
|
|
71
|
+
token: '0x0000000000000000000000000000000000000000',
|
|
72
|
+
amount: '0',
|
|
73
|
+
expiration: '0',
|
|
74
|
+
nonce: 0,
|
|
75
|
+
},
|
|
76
|
+
spender: '0x0000000000000000000000000000000000000000',
|
|
77
|
+
sigDeadline: '0',
|
|
78
|
+
}, '0x0000000000000000000000000000000000000000', 1)).toBe('0xd47437bffdbc4d123a2165feb6ca646b8700c038622ce304f84e9048bc744f36');
|
|
79
|
+
});
|
|
80
|
+
it('batch', () => {
|
|
81
|
+
expect(allowanceTransfer_1.AllowanceTransfer.hash({
|
|
82
|
+
details: [
|
|
83
|
+
{
|
|
84
|
+
token: '0x0000000000000000000000000000000000000000',
|
|
85
|
+
amount: '0',
|
|
86
|
+
expiration: '0',
|
|
87
|
+
nonce: 0,
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
spender: '0x0000000000000000000000000000000000000000',
|
|
91
|
+
sigDeadline: '0',
|
|
92
|
+
}, '0x0000000000000000000000000000000000000000', 1)).toBe('0x49642ada5f77eb9458f8265eb01fed2684c2f25d50534fea3efdf2cf395deb2f');
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
//# sourceMappingURL=allowanceTransfer.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allowanceTransfer.test.js","sourceRoot":"","sources":["../../../src/allowanceTransfer.test.ts"],"names":[],"mappings":";;AAAA,2DAAuD;AACvD,2CAAiH;AAEjH,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;QAC1B,EAAE,CAAC,iBAAiB,EAAE,GAAG,EAAE;YACzB,MAAM,CAAC,GAAG,EAAE,CACV,qCAAiB,CAAC,IAAI,CACpB;gBACE,OAAO,EAAE;oBACP,KAAK,EAAE,4CAA4C;oBACnD,MAAM,EAAE,sCAA0B,CAAC,QAAQ,EAAE;oBAC7C,UAAU,EAAE,kCAAsB,CAAC,QAAQ,EAAE;oBAC7C,KAAK,EAAE,2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,KAAK,EAAE,4CAA4C;oBACnD,MAAM,EAAE,GAAG;oBACX,UAAU,EAAE,GAAG;oBACf,KAAK,EAAE,CAAC;iBACT;aACF;YACD,OAAO,EAAE,4CAA4C;YACrD,WAAW,EAAE,GAAG;SACjB,EACD,4CAA4C,EAC5C,CAAC,CACF,CACF,CAAC,IAAI,CAAC,oEAAoE,CAAC,CAAA;IAC9E,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
2
|
+
export declare const PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
|
3
|
+
export declare function permit2Address(chainId?: number): string;
|
|
4
|
+
export declare const MaxUint48: BigNumber;
|
|
5
|
+
export declare const MaxUint160: BigNumber;
|
|
6
|
+
export declare const MaxUint256: BigNumber;
|
|
7
|
+
export declare const MaxAllowanceTransferAmount: BigNumber;
|
|
8
|
+
export declare const MaxAllowanceExpiration: BigNumber;
|
|
9
|
+
export declare const MaxOrderedNonce: BigNumber;
|
|
10
|
+
export declare const MaxSignatureTransferAmount: BigNumber;
|
|
11
|
+
export declare const MaxUnorderedNonce: BigNumber;
|
|
12
|
+
export declare const MaxSigDeadline: BigNumber;
|
|
13
|
+
export declare const InstantExpiration: BigNumber;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InstantExpiration = exports.MaxSigDeadline = exports.MaxUnorderedNonce = exports.MaxSignatureTransferAmount = exports.MaxOrderedNonce = exports.MaxAllowanceExpiration = exports.MaxAllowanceTransferAmount = exports.MaxUint256 = exports.MaxUint160 = exports.MaxUint48 = exports.permit2Address = exports.PERMIT2_ADDRESS = void 0;
|
|
4
|
+
const bignumber_1 = require("@ethersproject/bignumber");
|
|
5
|
+
// @deprecated please use permit2Address(chainId: number)
|
|
6
|
+
exports.PERMIT2_ADDRESS = '0x000000000022D473030F116dDEE9F6B43aC78BA3';
|
|
7
|
+
function permit2Address(chainId) {
|
|
8
|
+
switch (chainId) {
|
|
9
|
+
case 324:
|
|
10
|
+
return '0x0000000000225e31D15943971F47aD3022F714Fa';
|
|
11
|
+
default:
|
|
12
|
+
return exports.PERMIT2_ADDRESS;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.permit2Address = permit2Address;
|
|
16
|
+
exports.MaxUint48 = bignumber_1.BigNumber.from('0xffffffffffff');
|
|
17
|
+
exports.MaxUint160 = bignumber_1.BigNumber.from('0xffffffffffffffffffffffffffffffffffffffff');
|
|
18
|
+
exports.MaxUint256 = bignumber_1.BigNumber.from('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
|
|
19
|
+
// alias max types for their usages
|
|
20
|
+
// allowance transfer types
|
|
21
|
+
exports.MaxAllowanceTransferAmount = exports.MaxUint160;
|
|
22
|
+
exports.MaxAllowanceExpiration = exports.MaxUint48;
|
|
23
|
+
exports.MaxOrderedNonce = exports.MaxUint48;
|
|
24
|
+
// signature transfer types
|
|
25
|
+
exports.MaxSignatureTransferAmount = exports.MaxUint256;
|
|
26
|
+
exports.MaxUnorderedNonce = exports.MaxUint256;
|
|
27
|
+
exports.MaxSigDeadline = exports.MaxUint256;
|
|
28
|
+
exports.InstantExpiration = bignumber_1.BigNumber.from(0);
|
|
29
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":";;;AAAA,wDAAoD;AAEpD,yDAAyD;AAC5C,QAAA,eAAe,GAAG,4CAA4C,CAAA;AAE3E,SAAgB,cAAc,CAAC,OAAgB;IAC7C,QAAQ,OAAO,EAAE;QACf,KAAK,GAAG;YACN,OAAO,4CAA4C,CAAA;QACrD;YACE,OAAO,uBAAe,CAAA;KACzB;AACH,CAAC;AAPD,wCAOC;AAEY,QAAA,SAAS,GAAG,qBAAS,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAA;AAC5C,QAAA,UAAU,GAAG,qBAAS,CAAC,IAAI,CAAC,4CAA4C,CAAC,CAAA;AACzE,QAAA,UAAU,GAAG,qBAAS,CAAC,IAAI,CAAC,oEAAoE,CAAC,CAAA;AAE9G,mCAAmC;AACnC,2BAA2B;AACd,QAAA,0BAA0B,GAAG,kBAAU,CAAA;AACvC,QAAA,sBAAsB,GAAG,iBAAS,CAAA;AAClC,QAAA,eAAe,GAAG,iBAAS,CAAA;AAExC,2BAA2B;AACd,QAAA,0BAA0B,GAAG,kBAAU,CAAA;AACvC,QAAA,iBAAiB,GAAG,kBAAU,CAAA;AAC9B,QAAA,cAAc,GAAG,kBAAU,CAAA;AAE3B,QAAA,iBAAiB,GAAc,qBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bignumber_1 = require("@ethersproject/bignumber");
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
describe('Constants', () => {
|
|
6
|
+
it('MaxUint256', () => {
|
|
7
|
+
expect(constants_1.MaxUint256).toEqual(bignumber_1.BigNumber.from(2).pow(256).sub(1));
|
|
8
|
+
});
|
|
9
|
+
it('MaxUint160', () => {
|
|
10
|
+
expect(constants_1.MaxUint160).toEqual(bignumber_1.BigNumber.from(2).pow(160).sub(1));
|
|
11
|
+
});
|
|
12
|
+
it('MaxUint48', () => {
|
|
13
|
+
expect(constants_1.MaxUint48).toEqual(bignumber_1.BigNumber.from(2).pow(48).sub(1));
|
|
14
|
+
});
|
|
15
|
+
it('InstantExpiration', () => {
|
|
16
|
+
expect(constants_1.InstantExpiration).toEqual(bignumber_1.BigNumber.from(0));
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=constants.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.test.js","sourceRoot":"","sources":["../../../src/constants.test.ts"],"names":[],"mappings":";;AAAA,wDAAoD;AACpD,2CAAkF;AAElF,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,EAAE,CAAC,YAAY,EAAE,GAAG,EAAE;QACpB,MAAM,CAAC,sBAAU,CAAC,CAAC,OAAO,CAAC,qBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC/D,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,YAAY,EAAE,GAAG,EAAE;QACpB,MAAM,CAAC,sBAAU,CAAC,CAAC,OAAO,CAAC,qBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC/D,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;QACnB,MAAM,CAAC,qBAAS,CAAC,CAAC,OAAO,CAAC,qBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7D,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,mBAAmB,EAAE,GAAG,EAAE;QAC3B,MAAM,CAAC,6BAAiB,CAAC,CAAC,OAAO,CAAC,qBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;IACtD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypedDataDomain, TypedDataField } from '@ethersproject/abstract-signer';
|
|
2
|
+
export declare function permit2Domain(permit2Address: string, chainId: number): TypedDataDomain;
|
|
3
|
+
export type PermitData = {
|
|
4
|
+
domain: TypedDataDomain;
|
|
5
|
+
types: Record<string, TypedDataField[]>;
|
|
6
|
+
values: any;
|
|
7
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.permit2Domain = void 0;
|
|
4
|
+
const PERMIT2_DOMAIN_NAME = 'Permit2';
|
|
5
|
+
function permit2Domain(permit2Address, chainId) {
|
|
6
|
+
return {
|
|
7
|
+
name: PERMIT2_DOMAIN_NAME,
|
|
8
|
+
chainId,
|
|
9
|
+
verifyingContract: permit2Address,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
exports.permit2Domain = permit2Domain;
|
|
13
|
+
//# sourceMappingURL=domain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"domain.js","sourceRoot":"","sources":["../../../src/domain.ts"],"names":[],"mappings":";;;AAEA,MAAM,mBAAmB,GAAG,SAAS,CAAA;AAErC,SAAgB,aAAa,CAAC,cAAsB,EAAE,OAAe;IACnE,OAAO;QACL,IAAI,EAAE,mBAAmB;QACzB,OAAO;QACP,iBAAiB,EAAE,cAAc;KAClC,CAAA;AACH,CAAC;AAND,sCAMC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./allowanceTransfer"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./signatureTransfer"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./providers"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./constants"), exports);
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";;;AAAA,8DAAmC;AACnC,8DAAmC;AACnC,sDAA2B;AAC3B,sDAA2B"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
2
|
+
import { Provider } from '@ethersproject/providers';
|
|
3
|
+
export interface AllowanceData {
|
|
4
|
+
amount: BigNumber;
|
|
5
|
+
nonce: number;
|
|
6
|
+
expiration: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class AllowanceProvider {
|
|
9
|
+
private provider;
|
|
10
|
+
private permit2Address;
|
|
11
|
+
private permit2;
|
|
12
|
+
constructor(provider: Provider, permit2Address: string);
|
|
13
|
+
getAllowanceData(token: string, owner: string, spender: string): Promise<AllowanceData>;
|
|
14
|
+
getAllowance(token: string, owner: string, spender: string): Promise<BigNumber>;
|
|
15
|
+
getNonce(token: string, owner: string, spender: string): Promise<number>;
|
|
16
|
+
getExpiration(token: string, owner: string, spender: string): Promise<number>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AllowanceProvider = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const Permit2_json_1 = tslib_1.__importDefault(require("../../abis/Permit2.json"));
|
|
6
|
+
const contracts_1 = require("@ethersproject/contracts");
|
|
7
|
+
class AllowanceProvider {
|
|
8
|
+
constructor(provider, permit2Address) {
|
|
9
|
+
this.provider = provider;
|
|
10
|
+
this.permit2Address = permit2Address;
|
|
11
|
+
this.permit2 = new contracts_1.Contract(this.permit2Address, Permit2_json_1.default, this.provider);
|
|
12
|
+
}
|
|
13
|
+
getAllowanceData(token, owner, spender) {
|
|
14
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
return yield this.permit2.allowance(owner, token, spender);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
getAllowance(token, owner, spender) {
|
|
19
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
return (yield this.getAllowanceData(token, owner, spender)).amount;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
getNonce(token, owner, spender) {
|
|
24
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return (yield this.getAllowanceData(token, owner, spender)).nonce;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
getExpiration(token, owner, spender) {
|
|
29
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
return (yield this.getAllowanceData(token, owner, spender)).expiration;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.AllowanceProvider = AllowanceProvider;
|
|
35
|
+
//# sourceMappingURL=AllowanceProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AllowanceProvider.js","sourceRoot":"","sources":["../../../../src/providers/AllowanceProvider.ts"],"names":[],"mappings":";;;;AAEA,mFAAgD;AAChD,wDAAmD;AAQnD,MAAa,iBAAiB;IAG5B,YAAoB,QAAkB,EAAU,cAAsB;QAAlD,aAAQ,GAAR,QAAQ,CAAU;QAAU,mBAAc,GAAd,cAAc,CAAQ;QACpE,IAAI,CAAC,OAAO,GAAG,IAAI,oBAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,sBAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC7E,CAAC;IAEK,gBAAgB,CAAC,KAAa,EAAE,KAAa,EAAE,OAAe;;YAClE,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;QAC5D,CAAC;KAAA;IAEK,YAAY,CAAC,KAAa,EAAE,KAAa,EAAE,OAAe;;YAC9D,OAAO,CAAC,MAAM,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAA;QACpE,CAAC;KAAA;IAEK,QAAQ,CAAC,KAAa,EAAE,KAAa,EAAE,OAAe;;YAC1D,OAAO,CAAC,MAAM,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,CAAA;QACnE,CAAC;KAAA;IAEK,aAAa,CAAC,KAAa,EAAE,KAAa,EAAE,OAAe;;YAC/D,OAAO,CAAC,MAAM,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,UAAU,CAAA;QACxE,CAAC;KAAA;CACF;AAtBD,8CAsBC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
|
|
2
|
+
import { Provider } from '@ethersproject/providers';
|
|
3
|
+
import { PermitTransferFrom, PermitBatchTransferFrom } from '../signatureTransfer';
|
|
4
|
+
export interface NonceValidationResult {
|
|
5
|
+
isUsed: boolean;
|
|
6
|
+
isExpired: boolean;
|
|
7
|
+
isValid: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare class SignatureProvider {
|
|
10
|
+
private provider;
|
|
11
|
+
private permit2Address;
|
|
12
|
+
private permit2;
|
|
13
|
+
constructor(provider: Provider, permit2Address: string);
|
|
14
|
+
/**
|
|
15
|
+
* Check if a nonce has been used for signature transfers
|
|
16
|
+
* @param owner The owner address
|
|
17
|
+
* @param nonce The nonce to check
|
|
18
|
+
* @returns true if the nonce has been used, false otherwise
|
|
19
|
+
*/
|
|
20
|
+
isNonceUsed(owner: string, nonce: BigNumberish): Promise<boolean>;
|
|
21
|
+
/**
|
|
22
|
+
* Check if a permit has expired based on its deadline
|
|
23
|
+
* @param deadline The deadline timestamp
|
|
24
|
+
* @returns true if the permit has expired, false otherwise
|
|
25
|
+
*/
|
|
26
|
+
isExpired(deadline: BigNumberish): Promise<boolean>;
|
|
27
|
+
/**
|
|
28
|
+
* Check if a permit is valid (not expired and nonce not used)
|
|
29
|
+
* @param permit The permit data to validate
|
|
30
|
+
* @returns true if the permit is valid, false otherwise
|
|
31
|
+
*/
|
|
32
|
+
isPermitValid(permit: PermitTransferFrom | PermitBatchTransferFrom): Promise<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Get detailed validation results for a permit
|
|
35
|
+
* @param permit The permit data to validate
|
|
36
|
+
* @returns Object containing validation results
|
|
37
|
+
*/
|
|
38
|
+
validatePermit(permit: PermitTransferFrom | PermitBatchTransferFrom): Promise<NonceValidationResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Get the current nonce bitmap for an owner at a specific word position
|
|
41
|
+
* @param owner The owner address
|
|
42
|
+
* @param wordPos The word position in the bitmap
|
|
43
|
+
* @returns The bitmap as a BigNumber
|
|
44
|
+
*/
|
|
45
|
+
getNonceBitmap(owner: string, wordPos: BigNumberish): Promise<BigNumber>;
|
|
46
|
+
/**
|
|
47
|
+
* Check if a specific bit is set in the nonce bitmap
|
|
48
|
+
* @param bitmap The bitmap to check
|
|
49
|
+
* @param bitPos The bit position (0-255)
|
|
50
|
+
* @returns true if the bit is set, false otherwise
|
|
51
|
+
*/
|
|
52
|
+
static isBitSet(bitmap: BigNumber, bitPos: number): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Get the word position and bit position for a given nonce
|
|
55
|
+
* @param nonce The nonce to analyze
|
|
56
|
+
* @returns Object containing wordPos and bitPos
|
|
57
|
+
*/
|
|
58
|
+
static getNoncePositions(nonce: BigNumberish): {
|
|
59
|
+
wordPos: BigNumber;
|
|
60
|
+
bitPos: number;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Batch check multiple nonces for the same owner
|
|
64
|
+
* @param owner The owner address
|
|
65
|
+
* @param nonces Array of nonces to check
|
|
66
|
+
* @returns Array of boolean results indicating if each nonce is used
|
|
67
|
+
*/
|
|
68
|
+
batchCheckNonces(owner: string, nonces: BigNumberish[]): Promise<boolean[]>;
|
|
69
|
+
/**
|
|
70
|
+
* Get the current block timestamp
|
|
71
|
+
* @returns Current block timestamp
|
|
72
|
+
*/
|
|
73
|
+
getCurrentTimestamp(): Promise<number>;
|
|
74
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SignatureProvider = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const bignumber_1 = require("@ethersproject/bignumber");
|
|
6
|
+
const contracts_1 = require("@ethersproject/contracts");
|
|
7
|
+
const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
|
|
8
|
+
const Permit2_json_1 = tslib_1.__importDefault(require("../../abis/Permit2.json"));
|
|
9
|
+
class SignatureProvider {
|
|
10
|
+
constructor(provider, permit2Address) {
|
|
11
|
+
this.provider = provider;
|
|
12
|
+
this.permit2Address = permit2Address;
|
|
13
|
+
this.permit2 = new contracts_1.Contract(this.permit2Address, Permit2_json_1.default, this.provider);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Check if a nonce has been used for signature transfers
|
|
17
|
+
* @param owner The owner address
|
|
18
|
+
* @param nonce The nonce to check
|
|
19
|
+
* @returns true if the nonce has been used, false otherwise
|
|
20
|
+
*/
|
|
21
|
+
isNonceUsed(owner, nonce) {
|
|
22
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const { wordPos, bitPos } = SignatureProvider.getNoncePositions(nonce);
|
|
24
|
+
const bitmap = yield this.permit2.nonceBitmap(owner, wordPos);
|
|
25
|
+
return SignatureProvider.isBitSet(bitmap, bitPos);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Check if a permit has expired based on its deadline
|
|
30
|
+
* @param deadline The deadline timestamp
|
|
31
|
+
* @returns true if the permit has expired, false otherwise
|
|
32
|
+
*/
|
|
33
|
+
isExpired(deadline) {
|
|
34
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
const currentTimestamp = yield this.getCurrentTimestamp();
|
|
36
|
+
return bignumber_1.BigNumber.from(deadline).lt(currentTimestamp);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Check if a permit is valid (not expired and nonce not used)
|
|
41
|
+
* @param permit The permit data to validate
|
|
42
|
+
* @returns true if the permit is valid, false otherwise
|
|
43
|
+
*/
|
|
44
|
+
isPermitValid(permit) {
|
|
45
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
return (yield this.validatePermit(permit)).isValid;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get detailed validation results for a permit
|
|
51
|
+
* @param permit The permit data to validate
|
|
52
|
+
* @returns Object containing validation results
|
|
53
|
+
*/
|
|
54
|
+
validatePermit(permit) {
|
|
55
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const [isExpiredResult, isNonceUsedResult] = yield Promise.all([
|
|
57
|
+
this.isExpired(permit.deadline),
|
|
58
|
+
this.isNonceUsed(permit.spender, permit.nonce),
|
|
59
|
+
]);
|
|
60
|
+
return {
|
|
61
|
+
isUsed: isNonceUsedResult,
|
|
62
|
+
isExpired: isExpiredResult,
|
|
63
|
+
isValid: !isExpiredResult && !isNonceUsedResult,
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get the current nonce bitmap for an owner at a specific word position
|
|
69
|
+
* @param owner The owner address
|
|
70
|
+
* @param wordPos The word position in the bitmap
|
|
71
|
+
* @returns The bitmap as a BigNumber
|
|
72
|
+
*/
|
|
73
|
+
getNonceBitmap(owner, wordPos) {
|
|
74
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
return yield this.permit2.nonceBitmap(owner, wordPos);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if a specific bit is set in the nonce bitmap
|
|
80
|
+
* @param bitmap The bitmap to check
|
|
81
|
+
* @param bitPos The bit position (0-255)
|
|
82
|
+
* @returns true if the bit is set, false otherwise
|
|
83
|
+
*/
|
|
84
|
+
static isBitSet(bitmap, bitPos) {
|
|
85
|
+
(0, tiny_invariant_1.default)(bitPos >= 0 && bitPos <= 255, 'BIT_POSITION_OUT_OF_RANGE');
|
|
86
|
+
const mask = bignumber_1.BigNumber.from(1).shl(bitPos);
|
|
87
|
+
return bitmap.and(mask).gt(0);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get the word position and bit position for a given nonce
|
|
91
|
+
* @param nonce The nonce to analyze
|
|
92
|
+
* @returns Object containing wordPos and bitPos
|
|
93
|
+
*/
|
|
94
|
+
static getNoncePositions(nonce) {
|
|
95
|
+
const nonceBN = bignumber_1.BigNumber.from(nonce);
|
|
96
|
+
return {
|
|
97
|
+
wordPos: nonceBN.shr(8),
|
|
98
|
+
bitPos: nonceBN.and(255).toNumber(),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Batch check multiple nonces for the same owner
|
|
103
|
+
* @param owner The owner address
|
|
104
|
+
* @param nonces Array of nonces to check
|
|
105
|
+
* @returns Array of boolean results indicating if each nonce is used
|
|
106
|
+
*/
|
|
107
|
+
batchCheckNonces(owner, nonces) {
|
|
108
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
// Get unique word positions to minimize contract calls
|
|
110
|
+
const wordPositions = new Set();
|
|
111
|
+
nonces.forEach((nonce) => {
|
|
112
|
+
const { wordPos } = SignatureProvider.getNoncePositions(nonce);
|
|
113
|
+
wordPositions.add(wordPos.toString());
|
|
114
|
+
});
|
|
115
|
+
// Fetch all required bitmaps
|
|
116
|
+
const bitmapPromises = Array.from(wordPositions).map((wordPosKey) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const wordPos = bignumber_1.BigNumber.from(wordPosKey);
|
|
118
|
+
const bitmap = yield this.getNonceBitmap(owner, wordPos);
|
|
119
|
+
return { wordPos, bitmap };
|
|
120
|
+
}));
|
|
121
|
+
const bitmaps = yield Promise.all(bitmapPromises);
|
|
122
|
+
const bitmapMap = new Map(bitmaps.map(({ wordPos, bitmap }) => [wordPos.toString(), bitmap]));
|
|
123
|
+
// Check each nonce
|
|
124
|
+
return nonces.map((nonce) => {
|
|
125
|
+
const { wordPos, bitPos } = SignatureProvider.getNoncePositions(nonce);
|
|
126
|
+
const bitmap = bitmapMap.get(wordPos.toString());
|
|
127
|
+
if (!bitmap) {
|
|
128
|
+
throw new Error(`Bitmap not found for word position ${wordPos.toString()}`);
|
|
129
|
+
}
|
|
130
|
+
return SignatureProvider.isBitSet(bitmap, bitPos);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get the current block timestamp
|
|
136
|
+
* @returns Current block timestamp
|
|
137
|
+
*/
|
|
138
|
+
getCurrentTimestamp() {
|
|
139
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
const currentBlock = yield this.provider.getBlock('latest');
|
|
141
|
+
return currentBlock.timestamp;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.SignatureProvider = SignatureProvider;
|
|
146
|
+
//# sourceMappingURL=SignatureProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SignatureProvider.js","sourceRoot":"","sources":["../../../../src/providers/SignatureProvider.ts"],"names":[],"mappings":";;;;AAAA,wDAAkE;AAElE,wDAAmD;AACnD,4EAAsC;AACtC,mFAAgD;AAShD,MAAa,iBAAiB;IAG5B,YAAoB,QAAkB,EAAU,cAAsB;QAAlD,aAAQ,GAAR,QAAQ,CAAU;QAAU,mBAAc,GAAd,cAAc,CAAQ;QACpE,IAAI,CAAC,OAAO,GAAG,IAAI,oBAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,sBAAU,EAAE,IAAI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