1
0
Fork 0
mirror of https://github.com/mautrix/signal.git synced 2026-05-15 05:36:53 -04:00
mautrix-signal/pkg/libsignalgo/prekeystore.go

84 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:54:35 +02:00
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Sumner Evans
2025-01-17 17:49:37 +02:00
// Copyright (C) 2025 Tulir Asokan
2023-12-17 15:54:35 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package libsignalgo
/*
#include "./libsignal-ffi.h"
2026-02-09 13:54:02 +02:00
extern int signal_load_pre_key_callback(void *store_ctx, SignalMutPointerPreKeyRecord *recordp, uint32_t id);
extern int signal_store_pre_key_callback(void *store_ctx, uint32_t id, SignalMutPointerPreKeyRecord record);
2024-01-04 01:06:45 +02:00
extern int signal_remove_pre_key_callback(void *store_ctx, uint32_t id);
2026-02-09 13:54:02 +02:00
extern void signal_destroy_pre_key_store_callback(void *store_ctx);
*/
import "C"
import (
"context"
"unsafe"
)
type PreKeyStore interface {
2024-01-04 01:06:45 +02:00
LoadPreKey(ctx context.Context, id uint32) (*PreKeyRecord, error)
StorePreKey(ctx context.Context, id uint32, preKeyRecord *PreKeyRecord) error
RemovePreKey(ctx context.Context, id uint32) error
}
//export signal_load_pre_key_callback
2026-02-09 13:54:02 +02:00
func signal_load_pre_key_callback(storeCtx unsafe.Pointer, keyp *C.SignalMutPointerPreKeyRecord, id C.uint32_t) C.int {
2024-01-04 01:06:45 +02:00
return wrapStoreCallback(storeCtx, func(store PreKeyStore, ctx context.Context) error {
key, err := store.LoadPreKey(ctx, uint32(id))
if err == nil && key != nil {
key.CancelFinalizer()
2026-02-09 13:54:02 +02:00
keyp.raw = key.ptr
}
return err
})
}
//export signal_store_pre_key_callback
2026-02-09 13:54:02 +02:00
func signal_store_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t, preKeyRecord C.SignalMutPointerPreKeyRecord) C.int {
2024-01-04 01:06:45 +02:00
return wrapStoreCallback(storeCtx, func(store PreKeyStore, ctx context.Context) error {
2026-02-09 13:54:02 +02:00
record := PreKeyRecord{ptr: preKeyRecord.raw}
cloned, err := record.Clone()
if err != nil {
return err
}
2024-01-04 01:06:45 +02:00
return store.StorePreKey(ctx, uint32(id), cloned)
})
}
//export signal_remove_pre_key_callback
2024-01-04 01:06:45 +02:00
func signal_remove_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t) C.int {
return wrapStoreCallback(storeCtx, func(store PreKeyStore, ctx context.Context) error {
return store.RemovePreKey(ctx, uint32(id))
})
}
2026-02-09 13:54:02 +02:00
//export signal_destroy_pre_key_store_callback
func signal_destroy_pre_key_store_callback(storeCtx unsafe.Pointer) {
// No-op: Go's garbage collector handles cleanup
}
2025-01-17 17:49:37 +02:00
func (ctx *CallbackContext) wrapPreKeyStore(store PreKeyStore) C.SignalConstPointerFfiPreKeyStoreStruct {
return C.SignalConstPointerFfiPreKeyStoreStruct{&C.SignalPreKeyStore{
2024-01-04 01:06:45 +02:00
ctx: wrapStore(ctx, store),
2026-02-09 13:54:02 +02:00
load_pre_key: C.SignalFfiBridgePreKeyStoreLoadPreKey(C.signal_load_pre_key_callback),
store_pre_key: C.SignalFfiBridgePreKeyStoreStorePreKey(C.signal_store_pre_key_callback),
remove_pre_key: C.SignalFfiBridgePreKeyStoreRemovePreKey(C.signal_remove_pre_key_callback),
destroy: C.SignalFfiBridgePreKeyStoreDestroy(C.signal_destroy_pre_key_store_callback),
2025-01-17 17:49:37 +02:00
}}
}