@thesingularitynetwork/darkswap-sdk 0.1.14 → 0.1.16
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/index.esm.js +20 -16
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +129 -80
- package/dist/index.js.map +1 -1
- package/dist/types/src/proof/noteService.d.ts +2 -2
- package/package.json +2 -1
- package/src/proof/noteService.ts +86 -80
- package/dist/index.umd.js +0 -7487
- package/dist/index.umd.js.map +0 -1
- package/dist/types/src/services/pro/swapMessage.d.ts +0 -2
package/src/proof/noteService.ts
CHANGED
|
@@ -1,109 +1,115 @@
|
|
|
1
|
-
import { Fr } from
|
|
2
|
-
import { hexlify } from
|
|
3
|
-
import { DarkSwapNote, DarkSwapNoteExt, DarkSwapOrderNote } from
|
|
4
|
-
import { P } from
|
|
5
|
-
import { encodeAddress } from
|
|
6
|
-
import { mimc_bn254 } from
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields'
|
|
2
|
+
import { hexlify } from 'ethers'
|
|
3
|
+
import { DarkSwapNote, DarkSwapNoteExt, DarkSwapOrderNote } from '../types.js'
|
|
4
|
+
import { P } from '../utils/constants.js'
|
|
5
|
+
import { encodeAddress } from '../utils/encoders.js'
|
|
6
|
+
import { mimc_bn254 } from '../utils/mimc.js'
|
|
7
|
+
import cryptoJs from 'crypto-js'
|
|
7
8
|
|
|
8
|
-
let getRandomValues: (buf: Uint8Array) => Uint8Array
|
|
9
|
+
let getRandomValues: (buf: Uint8Array) => Uint8Array
|
|
9
10
|
|
|
10
|
-
if (
|
|
11
|
-
|
|
11
|
+
if (
|
|
12
|
+
typeof window !== 'undefined' &&
|
|
13
|
+
window.crypto &&
|
|
14
|
+
window.crypto.getRandomValues
|
|
15
|
+
) {
|
|
16
|
+
getRandomValues = (buf) => window.crypto.getRandomValues(buf)
|
|
12
17
|
} else {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
18
|
+
getRandomValues = (buf) => {
|
|
19
|
+
const randomBytes = cryptoJs.randomBytes(buf.length)
|
|
20
|
+
buf.set(randomBytes)
|
|
21
|
+
return buf
|
|
22
|
+
}
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
export const DOMAIN_NOTE = 2n
|
|
22
|
-
export const DOMAIN_ORDER_NOTE = 3n
|
|
25
|
+
export const DOMAIN_NOTE = 2n
|
|
26
|
+
export const DOMAIN_ORDER_NOTE = 3n
|
|
23
27
|
|
|
24
28
|
export const EMPTY_NOTE: DarkSwapNote = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
rho: 0n,
|
|
30
|
+
note: 0n,
|
|
31
|
+
amount: 0n,
|
|
32
|
+
asset: '0x0000000000000000000000000000000000000000',
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
export function createNote(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
address: string,
|
|
37
|
+
asset: string,
|
|
38
|
+
amount: bigint,
|
|
39
|
+
fuzkPubKey: [Fr, Fr]
|
|
36
40
|
): DarkSwapNoteExt {
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
const rho = generateRho()
|
|
42
|
+
const footer = getNoteFooter(rho, fuzkPubKey)
|
|
39
43
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
rho,
|
|
51
|
-
note,
|
|
52
|
-
asset,
|
|
53
|
-
amount,
|
|
54
|
-
footer
|
|
55
|
-
};
|
|
44
|
+
const addressMod = encodeAddress(address)
|
|
45
|
+
const assetMod = encodeAddress(asset)
|
|
46
|
+
const note = mimc_bn254([DOMAIN_NOTE, addressMod, assetMod, amount, footer])
|
|
47
|
+
return {
|
|
48
|
+
rho,
|
|
49
|
+
note,
|
|
50
|
+
asset,
|
|
51
|
+
amount,
|
|
52
|
+
footer,
|
|
53
|
+
}
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
export function getNoteFooter(rho: bigint, publicKey: [Fr, Fr]): bigint {
|
|
59
|
-
|
|
57
|
+
return mimc_bn254([
|
|
58
|
+
mimc_bn254([BigInt(rho)]),
|
|
59
|
+
BigInt(publicKey[0].toString()),
|
|
60
|
+
BigInt(publicKey[1].toString()),
|
|
61
|
+
])
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
function generateRho(): bigint {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
const securityLevel = 128
|
|
66
|
+
const primeByteLength = Math.ceil(P.toString(2).length / 8)
|
|
67
|
+
const totalBytes = primeByteLength + Math.ceil(securityLevel / 8)
|
|
66
68
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
let rho = BigInt(0)
|
|
70
|
+
do {
|
|
71
|
+
let ab = new ArrayBuffer(totalBytes)
|
|
72
|
+
let buf = new Uint8Array(ab)
|
|
73
|
+
rho = BigInt(hexlify(getRandomValues(buf))) % P
|
|
74
|
+
} while (rho === BigInt(0))
|
|
73
75
|
|
|
74
|
-
|
|
76
|
+
return rho
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
export function calcNullifier(rho: bigint, fuzkPubKey: [Fr, Fr]): bigint {
|
|
78
|
-
|
|
80
|
+
return mimc_bn254([
|
|
81
|
+
rho,
|
|
82
|
+
BigInt(fuzkPubKey[0].toString()),
|
|
83
|
+
BigInt(fuzkPubKey[1].toString()),
|
|
84
|
+
])
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
export function createOrderNoteExt(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
address: string,
|
|
89
|
+
asset: string,
|
|
90
|
+
amount: bigint,
|
|
91
|
+
feeRatio: bigint,
|
|
92
|
+
fuzkPubKey: [Fr, Fr]
|
|
87
93
|
): DarkSwapOrderNote {
|
|
88
|
-
|
|
89
|
-
|
|
94
|
+
const rho = generateRho()
|
|
95
|
+
const footer = getNoteFooter(rho, fuzkPubKey)
|
|
90
96
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
97
|
+
const assetMod = encodeAddress(asset)
|
|
98
|
+
const addressMod = encodeAddress(address)
|
|
99
|
+
const noteCommitment = mimc_bn254([
|
|
100
|
+
DOMAIN_ORDER_NOTE,
|
|
101
|
+
addressMod,
|
|
102
|
+
assetMod,
|
|
103
|
+
amount,
|
|
104
|
+
feeRatio,
|
|
105
|
+
footer,
|
|
106
|
+
])
|
|
101
107
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
108
|
+
return {
|
|
109
|
+
rho,
|
|
110
|
+
note: noteCommitment,
|
|
111
|
+
asset,
|
|
112
|
+
amount,
|
|
113
|
+
feeRatio,
|
|
114
|
+
}
|
|
115
|
+
}
|