您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

121 行
3.1 KiB

  1. const { proto } = require("@whiskeysockets/baileys/WAProto");
  2. const {
  3. Curve,
  4. signedKeyPair,
  5. } = require("@whiskeysockets/baileys/lib/Utils/crypto");
  6. const {
  7. generateRegistrationId,
  8. } = require("@whiskeysockets/baileys/lib/Utils/generics");
  9. const { randomBytes } = require("crypto");
  10. const initAuthCreds = () => {
  11. const identityKey = Curve.generateKeyPair();
  12. return {
  13. noiseKey: Curve.generateKeyPair(),
  14. signedIdentityKey: identityKey,
  15. signedPreKey: signedKeyPair(identityKey, 1),
  16. registrationId: generateRegistrationId(),
  17. advSecretKey: randomBytes(32).toString("base64"),
  18. processedHistoryMessages: [],
  19. nextPreKeyId: 1,
  20. firstUnuploadedPreKeyId: 1,
  21. accountSettings: {
  22. unarchiveChats: false,
  23. },
  24. };
  25. };
  26. const BufferJSON = {
  27. replacer: (k, value) => {
  28. if (
  29. Buffer.isBuffer(value) ||
  30. value instanceof Uint8Array ||
  31. value?.type === "Buffer"
  32. ) {
  33. return {
  34. type: "Buffer",
  35. data: Buffer.from(value?.data || value).toString("base64"),
  36. };
  37. }
  38. return value;
  39. },
  40. reviver: (_, value) => {
  41. if (
  42. typeof value === "object" &&
  43. !!value &&
  44. (value.buffer === true || value.type === "Buffer")
  45. ) {
  46. const val = value.data || value.value;
  47. return typeof val === "string"
  48. ? Buffer.from(val, "base64")
  49. : Buffer.from(val || []);
  50. }
  51. return value;
  52. },
  53. };
  54. module.exports = useMongoDBAuthState = async (collection) => {
  55. const writeData = (data, id) => {
  56. const informationToStore = JSON.parse(
  57. JSON.stringify(data, BufferJSON.replacer)
  58. );
  59. const update = {
  60. $set: {
  61. ...informationToStore,
  62. },
  63. };
  64. return collection.updateOne({ _id: id }, update, { upsert: true });
  65. };
  66. const readData = async (id) => {
  67. try {
  68. const data = JSON.stringify(await collection.findOne({ _id: id }));
  69. return JSON.parse(data, BufferJSON.reviver);
  70. } catch (error) {
  71. return null;
  72. }
  73. };
  74. const removeData = async (id) => {
  75. try {
  76. await collection.deleteOne({ _id: id });
  77. } catch (_a) {}
  78. };
  79. const creds = (await readData("creds")) || (0, initAuthCreds)();
  80. return {
  81. state: {
  82. creds,
  83. keys: {
  84. get: async (type, ids) => {
  85. const data = {};
  86. await Promise.all(
  87. ids.map(async (id) => {
  88. let value = await readData(`${type}-${id}`);
  89. if (type === "app-state-sync-key") {
  90. value = proto.Message.AppStateSyncKeyData.fromObject(data);
  91. }
  92. data[id] = value;
  93. })
  94. );
  95. return data;
  96. },
  97. set: async (data) => {
  98. const tasks = [];
  99. for (const category of Object.keys(data)) {
  100. for (const id of Object.keys(data[category])) {
  101. const value = data[category][id];
  102. const key = `${category}-${id}`;
  103. tasks.push(value ? writeData(value, key) : removeData(key));
  104. }
  105. }
  106. await Promise.all(tasks);
  107. },
  108. },
  109. },
  110. saveCreds: () => {
  111. return writeData(creds, "creds");
  112. },
  113. };
  114. };