@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.
@@ -1,109 +1,115 @@
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";
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 (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
11
- getRandomValues = (buf) => window.crypto.getRandomValues(buf);
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
- const nodeCrypto = require('crypto');
14
- getRandomValues = (buf) => {
15
- const randomBytes = nodeCrypto.randomBytes(buf.length);
16
- buf.set(randomBytes);
17
- return buf;
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
- rho: 0n,
26
- note: 0n,
27
- amount: 0n,
28
- asset: '0x0000000000000000000000000000000000000000'
29
+ rho: 0n,
30
+ note: 0n,
31
+ amount: 0n,
32
+ asset: '0x0000000000000000000000000000000000000000',
29
33
  }
30
34
 
31
35
  export function createNote(
32
- address: string,
33
- asset: string,
34
- amount: bigint,
35
- fuzkPubKey: [Fr, Fr]
36
+ address: string,
37
+ asset: string,
38
+ amount: bigint,
39
+ fuzkPubKey: [Fr, Fr]
36
40
  ): DarkSwapNoteExt {
37
- const rho = generateRho();
38
- const footer = getNoteFooter(rho, fuzkPubKey)
41
+ const rho = generateRho()
42
+ const footer = getNoteFooter(rho, fuzkPubKey)
39
43
 
40
- const addressMod = encodeAddress(address);
41
- const assetMod = encodeAddress(asset);
42
- const note = mimc_bn254([
43
- DOMAIN_NOTE,
44
- addressMod,
45
- assetMod,
46
- amount,
47
- footer
48
- ]);
49
- return {
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
- return mimc_bn254([mimc_bn254([BigInt(rho)]), BigInt(publicKey[0].toString()), BigInt(publicKey[1].toString())]);
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
- const securityLevel = 128;
64
- const primeByteLength = Math.ceil(P.toString(2).length / 8);
65
- const totalBytes = primeByteLength + Math.ceil(securityLevel / 8);
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
- let rho = BigInt(0);
68
- do {
69
- let ab = new ArrayBuffer(totalBytes);
70
- let buf = new Uint8Array(ab);
71
- rho = BigInt(hexlify(getRandomValues(buf))) % P;
72
- } while (rho === BigInt(0));
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
- return rho;
76
+ return rho
75
77
  }
76
78
 
77
79
  export function calcNullifier(rho: bigint, fuzkPubKey: [Fr, Fr]): bigint {
78
- return mimc_bn254([rho, BigInt(fuzkPubKey[0].toString()), BigInt(fuzkPubKey[1].toString())]);
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
- address: string,
83
- asset: string,
84
- amount: bigint,
85
- feeRatio: bigint,
86
- fuzkPubKey: [Fr, Fr]
88
+ address: string,
89
+ asset: string,
90
+ amount: bigint,
91
+ feeRatio: bigint,
92
+ fuzkPubKey: [Fr, Fr]
87
93
  ): DarkSwapOrderNote {
88
- const rho = generateRho();
89
- const footer = getNoteFooter(rho, fuzkPubKey)
94
+ const rho = generateRho()
95
+ const footer = getNoteFooter(rho, fuzkPubKey)
90
96
 
91
- const assetMod = encodeAddress(asset);
92
- const addressMod = encodeAddress(address);
93
- const noteCommitment = mimc_bn254([
94
- DOMAIN_ORDER_NOTE,
95
- addressMod,
96
- assetMod,
97
- amount,
98
- feeRatio,
99
- footer
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
- return {
103
- rho,
104
- note: noteCommitment,
105
- asset,
106
- amount,
107
- feeRatio
108
- };
109
- }
108
+ return {
109
+ rho,
110
+ note: noteCommitment,
111
+ asset,
112
+ amount,
113
+ feeRatio,
114
+ }
115
+ }