diff --git a/Runtime/CareBoo.AlgoSdk/Accounts/Logic.cs b/Runtime/CareBoo.AlgoSdk/Accounts/Logic.cs index f0494371f..ecce1c4ad 100644 --- a/Runtime/CareBoo.AlgoSdk/Accounts/Logic.cs +++ b/Runtime/CareBoo.AlgoSdk/Accounts/Logic.cs @@ -77,5 +77,99 @@ public static (Sig, int) Sign(CompiledTeal program, MultisigSig msig, PrivateKey var sig = Sign(program, keyPair.SecretKey); return (sig, index); } + + /// + /// Creates a signature compatible with ed25519verify opcode from raw program bytes. + /// + /// The secretkey handle to use to sign using the given data and the program. + /// The data to sign. + /// The program to hash and prepend to the signed message. + /// A signature compatible with ed25519verify opcode. + public static Sig TealSignProgram(SecretKeyHandle sk, NativeArray data, CompiledTeal program) + { + var address = GetAddress(program); + return TealSign(sk, data, address); + } + + /// + /// Creates a signature compatible with ed25519verify opcode from raw program bytes. + /// + /// The secretkey handle to use to sign using the given data and the program. + /// The data to sign. + /// The program to hash and prepend to the signed message. + /// A signature compatible with ed25519verify opcode. + public static Sig TealSignProgram(SecretKeyHandle sk, byte[] data, CompiledTeal program) + { + var address = GetAddress(program); + return TealSign(sk, data, address); + } + + /// + /// Creates a signature compatible with ed25519verify opcode from raw program bytes. + /// + /// The type of the byte array. + /// The secretkey handle to use to sign using the given data and the program. + /// The data to sign. + /// The program to hash and prepend to the signed message. + /// A signature compatible with ed25519verify opcode. + public static Sig TealSignProgram(SecretKeyHandle sk, TBytes data, CompiledTeal program) + where TBytes : IByteArray + { + var address = GetAddress(program); + return TealSign(sk, data, address); + } + + /// + /// Creates a signature compatible with ed25519verify opcode from the contract address. + /// + /// The secretkey handle to use to sign using the given data and the program. + /// The data to sign. + /// The teal contract address to use to sign this data. + /// A signature compatible with ed25519verify opcode. + public static Sig TealSign(SecretKeyHandle sk, NativeArray data, Address contractAddress) + { + return TealSign(sk, new NativeByteArray(data), contractAddress); + } + + /// + /// Creates a signature compatible with ed25519verify opcode from the contract address. + /// + /// The secretkey handle to use to sign using the given data and the program. + /// The data to sign. + /// The teal contract address to use to sign this data. + /// A signature compatible with ed25519verify opcode. + public static Sig TealSign(SecretKeyHandle sk, byte[] data, Address contractAddress) + { + using var nativeData = new NativeByteArray(data, Allocator.Temp); + return TealSign(sk, data, contractAddress); + } + + /// + /// Creates a signature compatible with ed25519verify opcode from the contract address. + /// + /// The type of the byte array. + /// The secretkey handle to use to sign using the given data and the program. + /// The data to sign. + /// The teal contract address to use to sign this data. + /// A signature compatible with ed25519verify opcode. + public static Sig TealSign(SecretKeyHandle sk, TBytes data, Address contractAddress) + where TBytes : IByteArray + { + var concatenated = new NativeByteArray(contractAddress.Length + data.Length, Allocator.Temp); + try + { + for (var i = 0; i < contractAddress.Length; i++) + concatenated[i] = contractAddress[i]; + for (var i = contractAddress.Length; i < data.Length; i++) + concatenated[i] = data[i - contractAddress.Length]; + + return sk.Sign(concatenated); + } + finally + { + concatenated.Dispose(); + } + + } } }