feat(server): CreateBatch/生成器切换到 codes 库(Mint 单事务全成或全无)(#codes-lib)
Store 挂库 store(NewStore 内部构造 libcodes.Store,签名不变); generator.go 三函数委托 libcodes,ErrDuplicate 别名同一哨兵; Service.CreateBatch 走 libcodes.Mint(签名不变)。openMigratedSQLite 挪到共享 sqlite_helper_test.go,backfill_test.go/service_sqlite_test.go 共用。Store.CreateBatch/CreateCode/CodeExistsByHash 原样保留供 webhook.go(Task 6 改用新原语,Task 7 删除)。
This commit is contained in:
@@ -8,16 +8,14 @@
|
||||
package codes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/wangjia/pangolin/server/internal/idgen"
|
||||
libcodes "github.com/wangjia/codes"
|
||||
)
|
||||
|
||||
// GenerateCode generates a single random activation code in canonical Crockford
|
||||
// Base32 form (15 random data chars + 1 check char = 16 chars total).
|
||||
// It delegates to idgen.GenerateCode which uses crypto/rand.
|
||||
// It delegates to the shared library, which uses crypto/rand.
|
||||
func GenerateCode() (string, error) {
|
||||
return idgen.GenerateCode()
|
||||
return libcodes.GenerateCode()
|
||||
}
|
||||
|
||||
// Canonicalize converts an activation-code string into canonical form:
|
||||
@@ -27,15 +25,17 @@ func GenerateCode() (string, error) {
|
||||
// Crockford alphabet, if the length is not exactly 16, or if the check character
|
||||
// is incorrect.
|
||||
func Canonicalize(code string) (string, error) {
|
||||
return idgen.CanonicalizeCode(code)
|
||||
return libcodes.Canonicalize(code)
|
||||
}
|
||||
|
||||
// Hash returns the hex-encoded SHA-256 of the canonical plaintext code.
|
||||
// This is the value stored in the database; the plaintext is never stored.
|
||||
func Hash(canonical string) string {
|
||||
return idgen.HashCode(canonical)
|
||||
return libcodes.Hash(canonical)
|
||||
}
|
||||
|
||||
// ErrDuplicate is returned by the batch generator when a generated code
|
||||
// already exists in the database (hash collision). The caller should retry.
|
||||
var ErrDuplicate = errors.New("codes: duplicate code hash")
|
||||
// Aliased to the same sentinel as the shared library so errors.Is succeeds
|
||||
// across both layers.
|
||||
var ErrDuplicate = libcodes.ErrDuplicate
|
||||
|
||||
Reference in New Issue
Block a user