@itznomo/baileys 0.0.0-stage → 0.0.1
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/BUTTONS.md +153 -0
- package/Example/call-bot.mjs +38 -0
- package/Example/example.mjs +36 -0
- package/Example/full-bot-commands.mjs +319 -0
- package/Example/full-bot.mjs +77 -0
- package/LICENSE +21 -0
- package/README.md +543 -2
- package/WAProto/@dnuzi +1 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +113300 -0
- package/assets/banner.svg +17 -0
- package/assets/devider.svg +1 -0
- package/assets/features.svg +21 -0
- package/docs/CALLS.md +106 -0
- package/docs/FEATURES.md +1152 -0
- package/engine-requirements.js +19 -0
- package/index.d.ts +815 -0
- package/package.json +51 -4
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +627 -0
- package/src/constants.js +147 -0
- package/src/defaults.js +106 -0
- package/src/foundation/ai-rich.js +654 -0
- package/src/foundation/audio-duration.js +470 -0
- package/src/foundation/audio-samples.js +250 -0
- package/src/foundation/boom.js +39 -0
- package/src/foundation/builders.js +851 -0
- package/src/foundation/cache.js +134 -0
- package/src/foundation/concurrency.js +69 -0
- package/src/foundation/curve25519.js +189 -0
- package/src/foundation/image-thumb.js +65 -0
- package/src/foundation/link-preview.js +278 -0
- package/src/foundation/logger.js +78 -0
- package/src/foundation/long.js +130 -0
- package/src/foundation/performance.js +385 -0
- package/src/foundation/protobuf-minimal.js +587 -0
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +716 -0
- package/src/foundation/wa-crypto.js +47 -0
- package/src/foundation/zip.js +108 -0
- package/src/index.js +67 -0
- package/src/signal/libsignal.js +996 -0
- package/src/signal/signal-group.js +455 -0
- package/src/signal/signal-repository.js +635 -0
- package/src/socket/business-communities.js +811 -0
- package/src/socket/chats.js +702 -0
- package/src/socket/groups-newsletter.js +524 -0
- package/src/socket/messages-recv.js +1815 -0
- package/src/socket/messages-send.js +1526 -0
- package/src/socket/socket-builder.js +131 -0
- package/src/socket/socket-core.js +913 -0
- package/src/socket/usync.js +287 -0
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +170 -0
- package/src/utils/auth-state-core.js +731 -0
- package/src/utils/auth-state-storage.js +594 -0
- package/src/utils/chat-sync.js +1046 -0
- package/src/utils/check-status.js +159 -0
- package/src/utils/cstoken.js +17 -0
- package/src/utils/media.js +807 -0
- package/src/utils/message-compose.js +1747 -0
- package/src/utils/message-processing.js +912 -0
- package/src/utils/modern-messages.js +435 -0
- package/src/utils/nomo-buttons.js +138 -0
- package/src/utils/nomo-commands.js +168 -0
- package/src/utils/spam-report-utils.js +136 -0
- package/src/utils/use-keyv-auth-state.js +307 -0
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +1227 -0
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/auto.js +139 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1727 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +730 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +22852 -0
- package/src/wam/wam.js +146 -0
- package/voip.d.ts +144 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export class Cache {
|
|
2
|
+
constructor(options = {}) {
|
|
3
|
+
this.ttl = options.ttl ?? (options.stdTTL ? options.stdTTL * 1000 : 0)
|
|
4
|
+
this.max = options.max || Infinity
|
|
5
|
+
this.updateAgeOnGet = !!options.updateAgeOnGet
|
|
6
|
+
this.dispose = options.dispose || (() => {})
|
|
7
|
+
this._store = new Map()
|
|
8
|
+
|
|
9
|
+
if (this.ttl > 0) {
|
|
10
|
+
// Adaptive sweep interval:
|
|
11
|
+
// - Short TTL (<= 60s): sweep every 30-60s
|
|
12
|
+
// - Medium TTL (<= 30m): sweep every 1-7.5m (ttl / 4)
|
|
13
|
+
// - Long TTL (> 30m): sweep every 10m
|
|
14
|
+
// Prevents high CPU usage from unnecessary frequent sweeps.
|
|
15
|
+
let sweepInterval
|
|
16
|
+
if (this.ttl <= 60_000) {
|
|
17
|
+
sweepInterval = Math.max(this.ttl, 30_000)
|
|
18
|
+
} else if (this.ttl <= 30 * 60_000) {
|
|
19
|
+
sweepInterval = Math.max(60_000, Math.floor(this.ttl / 4))
|
|
20
|
+
} else {
|
|
21
|
+
sweepInterval = 10 * 60_000
|
|
22
|
+
}
|
|
23
|
+
this._sweeper = setInterval(() => this._sweep(), sweepInterval)
|
|
24
|
+
this._sweeper.unref?.()
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_isExpired(entry) {
|
|
29
|
+
return this.ttl > 0 && entry.expiresAt !== 0 && entry.expiresAt <= Date.now()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_sweep() {
|
|
33
|
+
if (this._store.size === 0) return
|
|
34
|
+
const now = Date.now()
|
|
35
|
+
for (const [key, entry] of this._store) {
|
|
36
|
+
if (entry.expiresAt !== 0 && entry.expiresAt <= now) {
|
|
37
|
+
this._store.delete(key)
|
|
38
|
+
this.dispose(entry.value, key)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get(key) {
|
|
44
|
+
const entry = this._store.get(key)
|
|
45
|
+
if (!entry) return undefined
|
|
46
|
+
if (this._isExpired(entry)) {
|
|
47
|
+
this._store.delete(key)
|
|
48
|
+
this.dispose(entry.value, key)
|
|
49
|
+
return undefined
|
|
50
|
+
}
|
|
51
|
+
if (this.updateAgeOnGet && this.ttl > 0) {
|
|
52
|
+
entry.expiresAt = Date.now() + this.ttl
|
|
53
|
+
|
|
54
|
+
this._store.delete(key)
|
|
55
|
+
this._store.set(key, entry)
|
|
56
|
+
}
|
|
57
|
+
return entry.value
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
set(key, value) {
|
|
61
|
+
if (this._store.has(key)) this._store.delete(key)
|
|
62
|
+
this._store.set(key, {
|
|
63
|
+
value,
|
|
64
|
+
expiresAt: this.ttl > 0 ? Date.now() + this.ttl : 0
|
|
65
|
+
})
|
|
66
|
+
while (this._store.size > this.max) {
|
|
67
|
+
const oldestKey = this._store.keys().next().value
|
|
68
|
+
const oldest = this._store.get(oldestKey)
|
|
69
|
+
this._store.delete(oldestKey)
|
|
70
|
+
this.dispose(oldest.value, oldestKey)
|
|
71
|
+
}
|
|
72
|
+
return true
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
has(key) {
|
|
76
|
+
const entry = this._store.get(key)
|
|
77
|
+
if (!entry) return false
|
|
78
|
+
if (this._isExpired(entry)) {
|
|
79
|
+
this._store.delete(key)
|
|
80
|
+
this.dispose(entry.value, key)
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
delete(key) {
|
|
87
|
+
const entry = this._store.get(key)
|
|
88
|
+
if (!entry) return false
|
|
89
|
+
this._store.delete(key)
|
|
90
|
+
this.dispose(entry.value, key)
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
clear() {
|
|
95
|
+
for (const [key, entry] of this._store) this.dispose(entry.value, key)
|
|
96
|
+
this._store.clear()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
flushAll() {
|
|
100
|
+
this.clear()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
del(key) {
|
|
104
|
+
return this.delete(key)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
close() {
|
|
108
|
+
if (this._sweeper) {
|
|
109
|
+
clearInterval(this._sweeper)
|
|
110
|
+
this._sweeper = undefined
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
mget(keys) {
|
|
115
|
+
const result = {}
|
|
116
|
+
for (const key of keys) {
|
|
117
|
+
const value = this.get(key)
|
|
118
|
+
if (value !== undefined) result[key] = value
|
|
119
|
+
}
|
|
120
|
+
return result
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
mset(data) {
|
|
124
|
+
for (const item of data) this.set(item.key, item.value)
|
|
125
|
+
return true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get size() {
|
|
129
|
+
return this._store.size
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const LRUCache = Cache
|
|
134
|
+
export const NodeCache = Cache
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
class SerialQueue {
|
|
2
|
+
constructor() {
|
|
3
|
+
this._tail = Promise.resolve()
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
run(task) {
|
|
7
|
+
const result = this._tail.then(() => task())
|
|
8
|
+
|
|
9
|
+
this._tail = result.then(
|
|
10
|
+
() => undefined,
|
|
11
|
+
() => undefined
|
|
12
|
+
)
|
|
13
|
+
return result
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const makeMutex = () => {
|
|
18
|
+
const queue = new SerialQueue()
|
|
19
|
+
return {
|
|
20
|
+
mutex(task) {
|
|
21
|
+
return queue.run(task)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const makeKeyedMutex = () => {
|
|
27
|
+
const lanes = new Map()
|
|
28
|
+
return {
|
|
29
|
+
async mutex(key, task) {
|
|
30
|
+
let entry = lanes.get(key)
|
|
31
|
+
if (!entry) {
|
|
32
|
+
entry = { queue: new SerialQueue(), refCount: 0 }
|
|
33
|
+
lanes.set(key, entry)
|
|
34
|
+
}
|
|
35
|
+
entry.refCount++
|
|
36
|
+
try {
|
|
37
|
+
return await entry.queue.run(task)
|
|
38
|
+
} finally {
|
|
39
|
+
entry.refCount--
|
|
40
|
+
if (entry.refCount === 0 && lanes.get(key) === entry) {
|
|
41
|
+
lanes.delete(key)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const makeSerialTaskQueue = () => {
|
|
49
|
+
const queue = new SerialQueue()
|
|
50
|
+
return {
|
|
51
|
+
add(task) {
|
|
52
|
+
return queue.run(task)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const makeKeyedSerialTaskQueues = () => {
|
|
58
|
+
const queues = new Map()
|
|
59
|
+
return {
|
|
60
|
+
get(key) {
|
|
61
|
+
let q = queues.get(key)
|
|
62
|
+
if (!q) {
|
|
63
|
+
q = makeSerialTaskQueue()
|
|
64
|
+
queues.set(key, q)
|
|
65
|
+
}
|
|
66
|
+
return q
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { createHash, createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, randomBytes } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
const P = (1n << 255n) - 19n
|
|
4
|
+
const L = (1n << 252n) + 27742317777372353535851937790883648493n
|
|
5
|
+
|
|
6
|
+
const D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n
|
|
7
|
+
const SQRT_M1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n
|
|
8
|
+
const BASE_Y = 46316835694926478169428394003475163141307993866256225615783033603165251855960n
|
|
9
|
+
const BASE_X = 15112221349535400772501151409588531511454012693041857206046113283949847762202n
|
|
10
|
+
|
|
11
|
+
const mod = (a, m = P) => {
|
|
12
|
+
const r = a % m
|
|
13
|
+
return r >= 0n ? r : r + m
|
|
14
|
+
}
|
|
15
|
+
const pow = (b, e, m = P) => {
|
|
16
|
+
let r = 1n
|
|
17
|
+
b = mod(b, m)
|
|
18
|
+
while (e > 0n) {
|
|
19
|
+
if (e & 1n) r = (r * b) % m
|
|
20
|
+
b = (b * b) % m
|
|
21
|
+
e >>= 1n
|
|
22
|
+
}
|
|
23
|
+
return r
|
|
24
|
+
}
|
|
25
|
+
const inv = a => pow(a, P - 2n)
|
|
26
|
+
|
|
27
|
+
const bytesToNumLE = b => {
|
|
28
|
+
let n = 0n
|
|
29
|
+
for (let i = b.length - 1; i >= 0; i--) n = (n << 8n) | BigInt(b[i])
|
|
30
|
+
return n
|
|
31
|
+
}
|
|
32
|
+
const numToBytesLE = (n, len) => {
|
|
33
|
+
const out = Buffer.alloc(len)
|
|
34
|
+
for (let i = 0; i < len; i++) {
|
|
35
|
+
out[i] = Number(n & 0xffn)
|
|
36
|
+
n >>= 8n
|
|
37
|
+
}
|
|
38
|
+
return out
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ZERO = [0n, 1n, 1n, 0n]
|
|
42
|
+
const BASE = [BASE_X, BASE_Y, 1n, mod(BASE_X * BASE_Y)]
|
|
43
|
+
|
|
44
|
+
const pointAdd = (p, q) => {
|
|
45
|
+
const [X1, Y1, Z1, T1] = p
|
|
46
|
+
const [X2, Y2, Z2, T2] = q
|
|
47
|
+
const A = mod((Y1 - X1) * (Y2 - X2))
|
|
48
|
+
const B = mod((Y1 + X1) * (Y2 + X2))
|
|
49
|
+
const C = mod(2n * D * T1 * T2)
|
|
50
|
+
const Dd = mod(2n * Z1 * Z2)
|
|
51
|
+
const E = B - A
|
|
52
|
+
const F = Dd - C
|
|
53
|
+
const G = Dd + C
|
|
54
|
+
const H = B + A
|
|
55
|
+
return [mod(E * F), mod(G * H), mod(F * G), mod(E * H)]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const scalarMult = (point, scalar) => {
|
|
59
|
+
let r = ZERO
|
|
60
|
+
let q = point
|
|
61
|
+
while (scalar > 0n) {
|
|
62
|
+
if (scalar & 1n) r = pointAdd(r, q)
|
|
63
|
+
q = pointAdd(q, q)
|
|
64
|
+
scalar >>= 1n
|
|
65
|
+
}
|
|
66
|
+
return r
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const encodePoint = p => {
|
|
70
|
+
const zi = inv(p[2])
|
|
71
|
+
const x = mod(p[0] * zi)
|
|
72
|
+
const y = mod(p[1] * zi)
|
|
73
|
+
const out = numToBytesLE(y, 32)
|
|
74
|
+
if (x & 1n) out[31] |= 0x80
|
|
75
|
+
return out
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const decodePoint = bytes => {
|
|
79
|
+
const b = Buffer.from(bytes)
|
|
80
|
+
const sign = (b[31] & 0x80) >> 7
|
|
81
|
+
b[31] &= 0x7f
|
|
82
|
+
const y = bytesToNumLE(b)
|
|
83
|
+
if (y >= P) return null
|
|
84
|
+
const y2 = mod(y * y)
|
|
85
|
+
const u = mod(y2 - 1n)
|
|
86
|
+
const v = mod(D * y2 + 1n)
|
|
87
|
+
|
|
88
|
+
const v3 = mod(v * v * v)
|
|
89
|
+
const v7 = mod(v3 * v3 * v)
|
|
90
|
+
let x = mod(u * v3 * pow(mod(u * v7), (P - 5n) / 8n))
|
|
91
|
+
const vx2 = mod(v * x * x)
|
|
92
|
+
if (vx2 !== mod(u)) {
|
|
93
|
+
if (vx2 !== mod(-u)) return null
|
|
94
|
+
x = mod(x * SQRT_M1)
|
|
95
|
+
}
|
|
96
|
+
if (Number(x & 1n) !== sign) x = mod(-x)
|
|
97
|
+
return [x, y, 1n, mod(x * y)]
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const sha512 = (...parts) => {
|
|
101
|
+
const h = createHash('sha512')
|
|
102
|
+
for (const p of parts) h.update(p)
|
|
103
|
+
return h.digest()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const clamp = key => {
|
|
107
|
+
const k = Buffer.from(key)
|
|
108
|
+
k[0] &= 248
|
|
109
|
+
k[31] &= 127
|
|
110
|
+
k[31] |= 64
|
|
111
|
+
return k
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const PUB_DER = Buffer.from('302a300506032b656e032100', 'hex')
|
|
115
|
+
const PRIV_DER = Buffer.from('302e020100300506032b656e04220420', 'hex')
|
|
116
|
+
|
|
117
|
+
export const sharedKey = (priv, pub) =>
|
|
118
|
+
diffieHellman({
|
|
119
|
+
privateKey: createPrivateKey({ key: Buffer.concat([PRIV_DER, priv]), format: 'der', type: 'pkcs8' }),
|
|
120
|
+
publicKey: createPublicKey({ key: Buffer.concat([PUB_DER, pub]), format: 'der', type: 'spki' })
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
export const randomX25519 = () => {
|
|
124
|
+
const { publicKey, privateKey } = generateKeyPairSync('x25519', {
|
|
125
|
+
publicKeyEncoding: { format: 'der', type: 'spki' },
|
|
126
|
+
privateKeyEncoding: { format: 'der', type: 'pkcs8' }
|
|
127
|
+
})
|
|
128
|
+
return {
|
|
129
|
+
pub: publicKey.subarray(PUB_DER.length, PUB_DER.length + 32),
|
|
130
|
+
priv: privateKey.subarray(PRIV_DER.length, PRIV_DER.length + 32)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export const publicFromPrivate = seed => {
|
|
135
|
+
if (seed.length !== 32) throw new Error('wrong seed length')
|
|
136
|
+
const priv = Buffer.from(seed)
|
|
137
|
+
|
|
138
|
+
const base = Buffer.alloc(32)
|
|
139
|
+
base[0] = 9
|
|
140
|
+
const pub = Buffer.from(sharedKey(priv, base))
|
|
141
|
+
pub[31] &= 0x7f
|
|
142
|
+
return pub
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const montToEdY = pk => {
|
|
146
|
+
const b = Buffer.from(pk)
|
|
147
|
+
b[31] &= 0x7f
|
|
148
|
+
const u = mod(bytesToNumLE(b))
|
|
149
|
+
const y = mod((u - 1n) * inv(mod(u + 1n)))
|
|
150
|
+
return numToBytesLE(y, 32)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export const sign = (secretKey, msg) => {
|
|
154
|
+
if (secretKey.length !== 32) throw new Error('wrong secret key length')
|
|
155
|
+
const a = clamp(secretKey)
|
|
156
|
+
const aNum = bytesToNumLE(a)
|
|
157
|
+
const A = encodePoint(scalarMult(BASE, aNum))
|
|
158
|
+
const signBit = A[31] & 0x80
|
|
159
|
+
|
|
160
|
+
const r = mod(bytesToNumLE(sha512(a, msg)), L)
|
|
161
|
+
const R = encodePoint(scalarMult(BASE, r))
|
|
162
|
+
const Acompressed = Buffer.from(A)
|
|
163
|
+
|
|
164
|
+
const h = mod(bytesToNumLE(sha512(R, Acompressed, msg)), L)
|
|
165
|
+
const S = mod(r + h * aNum, L)
|
|
166
|
+
const sig = Buffer.concat([R, numToBytesLE(S, 32)])
|
|
167
|
+
sig[63] |= signBit
|
|
168
|
+
return sig
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export const verify = (publicKey, msg, signature) => {
|
|
172
|
+
if (signature.length !== 64) throw new Error('wrong signature length')
|
|
173
|
+
if (publicKey.length !== 32) throw new Error('wrong public key length')
|
|
174
|
+
const sig = Buffer.from(signature)
|
|
175
|
+
const edpk = montToEdY(publicKey)
|
|
176
|
+
edpk[31] |= sig[63] & 0x80
|
|
177
|
+
sig[63] &= 0x7f
|
|
178
|
+
const A = decodePoint(edpk)
|
|
179
|
+
if (!A) return false
|
|
180
|
+
const R = sig.subarray(0, 32)
|
|
181
|
+
const S = bytesToNumLE(sig.subarray(32))
|
|
182
|
+
const h = mod(bytesToNumLE(sha512(R, edpk, msg)), L)
|
|
183
|
+
|
|
184
|
+
const negA = [mod(-A[0]), A[1], A[2], mod(-A[3])]
|
|
185
|
+
const check = pointAdd(scalarMult(negA, h), scalarMult(BASE, S))
|
|
186
|
+
return Buffer.compare(encodePoint(check), R) === 0
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { randomBytes }
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { once } from 'node:events'
|
|
3
|
+
import { Boom } from './boom.js'
|
|
4
|
+
|
|
5
|
+
// Dependency-free image thumbnails. Used when none of sharp / @napi-rs/image / jimp is installed,
|
|
6
|
+
// so a missing optional library never results in a media message without jpegThumbnail
|
|
7
|
+
// (the recipient sees that as a grey box). Uses ffmpeg.
|
|
8
|
+
|
|
9
|
+
const FFMPEG_TIMEOUT_MS = 15_000
|
|
10
|
+
|
|
11
|
+
async function runFfmpeg(command, args, input) {
|
|
12
|
+
const child = spawn(command, args, { stdio: [Buffer.isBuffer(input) ? 'pipe' : 'ignore', 'pipe', 'pipe'] })
|
|
13
|
+
const out = []
|
|
14
|
+
const err = []
|
|
15
|
+
child.stdout.on('data', c => out.push(c))
|
|
16
|
+
child.stderr.on('data', c => err.push(c))
|
|
17
|
+
if (Buffer.isBuffer(input)) {
|
|
18
|
+
child.stdin.on('error', () => {})
|
|
19
|
+
child.stdin.end(input)
|
|
20
|
+
}
|
|
21
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), FFMPEG_TIMEOUT_MS)
|
|
22
|
+
try {
|
|
23
|
+
const [code] = await once(child, 'close')
|
|
24
|
+
if (code !== 0) throw new Boom(`${command} failed (code ${code}): ${Buffer.concat(err).toString('utf8').trim()}`)
|
|
25
|
+
return Buffer.concat(out)
|
|
26
|
+
} finally {
|
|
27
|
+
clearTimeout(timer)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function probeDimensions(input) {
|
|
32
|
+
try {
|
|
33
|
+
const src = Buffer.isBuffer(input) ? 'pipe:0' : input
|
|
34
|
+
const args = ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=p=0', src]
|
|
35
|
+
const [width, height] = (await runFfmpeg('ffprobe', args, input)).toString('utf8').trim().split(',').map(Number)
|
|
36
|
+
return width > 0 && height > 0 ? { width, height } : {}
|
|
37
|
+
} catch {
|
|
38
|
+
return {}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function ffmpegThumb(input, width) {
|
|
43
|
+
const src = Buffer.isBuffer(input) ? 'pipe:0' : input
|
|
44
|
+
const args = [
|
|
45
|
+
'-loglevel', 'error', '-i', src, '-an', '-sn', '-dn', '-map_metadata', '-1',
|
|
46
|
+
'-vf', `scale=w='min(${width},iw)':h=-2`,
|
|
47
|
+
'-frames:v', '1', '-q:v', '12', '-c:v', 'mjpeg', '-f', 'image2pipe', 'pipe:1'
|
|
48
|
+
]
|
|
49
|
+
const buffer = await runFfmpeg('ffmpeg', args, input)
|
|
50
|
+
if (!buffer.length) throw new Boom('ffmpeg produced no output')
|
|
51
|
+
return { buffer, original: await probeDimensions(input) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {Buffer | string} input image bytes or a file path
|
|
56
|
+
* @param {number} width thumbnail width in px
|
|
57
|
+
* @returns {Promise<{ buffer: Buffer, original: { width?: number, height?: number } }>}
|
|
58
|
+
*/
|
|
59
|
+
export async function extractImageThumbFallback(input, width = 32) {
|
|
60
|
+
try {
|
|
61
|
+
return await ffmpegThumb(input, width)
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new Boom(`could not generate image thumbnail (install sharp, @napi-rs/image or jimp, or ffmpeg): ${err?.message ?? String(err)}`)
|
|
64
|
+
}
|
|
65
|
+
}
|