Skip to content

Commit f0f14b4

Browse files
authored
Updates for kernel bindings tests version 0.0.2 (#8)
* Refactor script verification handling / json parsing to use new specifications of Kernel-bindings-handler * Refactor Response class to replace 'Success' property with 'Result' * Refactor Response class add new structure of the Error response. * Update CI configuration kernel_bindings_tests to version 0.0.2
1 parent 0b5cab4 commit f0f14b4

5 files changed

Lines changed: 102 additions & 68 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ jobs:
9696
platform: linux_amd64
9797

9898
env:
99-
TEST_VERSION: '0.0.1'
99+
TEST_VERSION: '0.0.2'
100100
TEST_REPO: 'stringintech/kernel-bindings-tests'
101101
TEST_DIR: '.conformance-tests'
102102

tools/kernel-bindings-test-handler/Handlers/ScriptVerifyHandler.cs

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ScriptVerifyHandler(KernelContext context)
2222
/// <summary>
2323
/// Handles a script verification request.
2424
/// </summary>
25-
public Response Handle(string requestId, ScriptVerifyParams parameters)
25+
public Response Handle(string requestId, BtckScriptPubkeyVerifyParams parameters)
2626
{
2727
try
2828
{
@@ -37,7 +37,7 @@ public Response Handle(string requestId, ScriptVerifyParams parameters)
3737
foreach (var output in parameters.SpentOutputs)
3838
{
3939
var outputScriptPubKey = ScriptPubKey.FromHex(output.ScriptPubKeyHex);
40-
spentOutputs.Add(new TxOut(outputScriptPubKey, output.Amount));
40+
spentOutputs.Add(new TxOut(outputScriptPubKey, output.Value));
4141
}
4242
}
4343

@@ -58,53 +58,49 @@ public Response Handle(string requestId, ScriptVerifyParams parameters)
5858
return new Response
5959
{
6060
Id = requestId,
61-
Success = new { }
61+
Result = true
6262
};
6363
}
6464
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "inputIndex")
6565
{
6666
return new Response
6767
{
6868
Id = requestId,
69+
Result = null,
6970
Error = new ErrorResponse
7071
{
71-
Type = "ScriptVerify",
72-
Variant = "TxInputIndex"
72+
Code = new ErrorCode
73+
{
74+
Type = "btck_ScriptVerifyStatus",
75+
Member = "TxInputIndex"
76+
}
7377
}
7478
};
7579
}
7680
catch (ScriptVerificationException ex)
7781
{
78-
return new Response
82+
// If status is OK, the script just failed verification (result: false)
83+
// If status is not OK, it's an actual error condition
84+
if (ex.Status == ScriptVerifyStatus.OK)
7985
{
80-
Id = requestId,
81-
Error = new ErrorResponse
86+
return new Response
8287
{
83-
Type = "ScriptVerify",
84-
Variant = MapScriptVerifyStatus(ex.Status)
85-
}
86-
};
87-
}
88-
catch (Exception
89-
#if DEBUG
90-
ex
91-
#endif
92-
)
93-
{
94-
// Log to stderr for debugging (can be disabled in production)
95-
#if DEBUG
96-
Console.Error.WriteLine($"Exception: {ex.GetType().Name}: {ex.Message}");
97-
Console.Error.WriteLine($"StackTrace: {ex.StackTrace}");
98-
#endif
88+
Id = requestId,
89+
Result = false
90+
};
91+
}
9992

100-
// Generic error for unexpected exceptions
10193
return new Response
10294
{
10395
Id = requestId,
96+
Result = null,
10497
Error = new ErrorResponse
10598
{
106-
Type = "ScriptVerify",
107-
Variant = "Invalid"
99+
Code = new ErrorCode
100+
{
101+
Type = "btck_ScriptVerifyStatus",
102+
Member = MapScriptVerifyStatus(ex.Status)
103+
}
108104
}
109105
};
110106
}
@@ -132,9 +128,18 @@ private ScriptVerificationFlags ParseFlags(object? flags)
132128
{
133129
return (ScriptVerificationFlags)jsonElement.GetUInt32();
134130
}
135-
else if (jsonElement.ValueKind == System.Text.Json.JsonValueKind.String)
131+
else if (jsonElement.ValueKind == System.Text.Json.JsonValueKind.Array)
136132
{
137-
return ParseFlagString(jsonElement.GetString() ?? string.Empty);
133+
// Handle array of string flags - combine them with OR
134+
ScriptVerificationFlags combinedFlags = ScriptVerificationFlags.None;
135+
foreach (var element in jsonElement.EnumerateArray())
136+
{
137+
if (element.ValueKind == System.Text.Json.JsonValueKind.String)
138+
{
139+
combinedFlags |= ParseFlagString(element.GetString() ?? string.Empty);
140+
}
141+
}
142+
return combinedFlags;
138143
}
139144
}
140145

@@ -152,18 +157,24 @@ private ScriptVerificationFlags ParseFlags(object? flags)
152157
/// </summary>
153158
private ScriptVerificationFlags ParseFlagString(string flagStr)
154159
{
160+
// Handle btck_ prefixed format (e.g., "btck_ScriptVerificationFlags_WITNESS")
161+
if (flagStr.StartsWith("btck_ScriptVerificationFlags_", StringComparison.OrdinalIgnoreCase))
162+
{
163+
flagStr = flagStr.Substring("btck_ScriptVerificationFlags_".Length);
164+
}
165+
155166
return flagStr.ToUpperInvariant() switch
156167
{
157-
"VERIFY_NONE" or "NONE" => ScriptVerificationFlags.None,
158-
"VERIFY_P2SH" or "P2SH" => ScriptVerificationFlags.P2SH,
159-
"VERIFY_DERSIG" or "DERSIG" => ScriptVerificationFlags.DerSig,
160-
"VERIFY_NULLDUMMY" or "NULLDUMMY" => ScriptVerificationFlags.NullDummy,
161-
"VERIFY_CHECKLOCKTIMEVERIFY" or "CHECKLOCKTIMEVERIFY" => ScriptVerificationFlags.CheckLockTimeVerify,
162-
"VERIFY_CHECKSEQUENCEVERIFY" or "CHECKSEQUENCEVERIFY" => ScriptVerificationFlags.CheckSequenceVerify,
163-
"VERIFY_WITNESS" or "WITNESS" => ScriptVerificationFlags.Witness,
164-
"VERIFY_TAPROOT" or "TAPROOT" => ScriptVerificationFlags.Taproot,
165-
"VERIFY_ALL" or "ALL" => ScriptVerificationFlags.All,
166-
"VERIFY_ALL_PRE_TAPROOT" or "ALL_PRE_TAPROOT" => ScriptVerificationFlags.AllPreTaproot,
168+
"NONE" => ScriptVerificationFlags.None,
169+
"P2SH" => ScriptVerificationFlags.P2SH,
170+
"DERSIG" => ScriptVerificationFlags.DerSig,
171+
"NULLDUMMY" => ScriptVerificationFlags.NullDummy,
172+
"CHECKLOCKTIMEVERIFY" => ScriptVerificationFlags.CheckLockTimeVerify,
173+
"CHECKSEQUENCEVERIFY" => ScriptVerificationFlags.CheckSequenceVerify,
174+
"WITNESS" => ScriptVerificationFlags.Witness,
175+
"TAPROOT" => ScriptVerificationFlags.Taproot,
176+
"ALL" => ScriptVerificationFlags.All,
177+
"ALL_PRE_TAPROOT" => ScriptVerificationFlags.AllPreTaproot,
167178
_ => throw new ArgumentException($"Unknown flag: {flagStr}")
168179
};
169180
}
@@ -175,12 +186,9 @@ private string MapScriptVerifyStatus(ScriptVerifyStatus status)
175186
{
176187
return status switch
177188
{
178-
ScriptVerifyStatus.ERROR_TX_INPUT_INDEX => "TxInputIndex",
179-
ScriptVerifyStatus.ERROR_INVALID_FLAGS => "InvalidFlags",
180-
ScriptVerifyStatus.ERROR_INVALID_FLAGS_COMBINATION => "InvalidFlagsCombination",
181-
ScriptVerifyStatus.ERROR_SPENT_OUTPUTS_MISMATCH => "SpentOutputsMismatch",
182-
ScriptVerifyStatus.ERROR_SPENT_OUTPUTS_REQUIRED => "SpentOutputsRequired",
183-
_ => "Invalid"
189+
ScriptVerifyStatus.ERROR_INVALID_FLAGS_COMBINATION => "ERROR_INVALID_FLAGS_COMBINATION",
190+
ScriptVerifyStatus.ERROR_SPENT_OUTPUTS_REQUIRED => "ERROR_SPENT_OUTPUTS_REQUIRED",
191+
_ => "ERROR_INVALID"
184192
};
185193
}
186194
}

tools/kernel-bindings-test-handler/Program.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ static async Task<int> Main(string[] args)
4646
Id = "unknown",
4747
Error = new ErrorResponse
4848
{
49-
Type = "InvalidRequest"
49+
Code = new ErrorCode
50+
{
51+
Type = "InvalidRequest"
52+
}
5053
}
5154
};
5255
}
@@ -62,7 +65,10 @@ static async Task<int> Main(string[] args)
6265
Id = "unknown",
6366
Error = new ErrorResponse
6467
{
65-
Type = "InvalidRequest"
68+
Code = new ErrorCode
69+
{
70+
Type = "InvalidRequest"
71+
}
6672
}
6773
};
6874
}
@@ -91,42 +97,51 @@ private static Response HandleRequest(Request request, ScriptVerifyHandler scrip
9197
{
9298
switch (request.Method)
9399
{
94-
case "script_pubkey.verify":
100+
case "btck_script_pubkey_verify":
95101
if (request.Params == null)
96102
{
97103
return new Response
98104
{
99105
Id = request.Id,
100106
Error = new ErrorResponse
101107
{
102-
Type = "InvalidParams"
108+
Code = new ErrorCode
109+
{
110+
Type = "InvalidParams"
111+
}
103112
}
104113
};
105114
}
106115

107-
var scriptVerifyParams = JsonSerializer.Deserialize<ScriptVerifyParams>(request.Params.Value, jsonOptions);
116+
var btckScriptPubkeyVerifyParams = JsonSerializer.Deserialize<BtckScriptPubkeyVerifyParams>(request.Params.Value, jsonOptions);
108117

109-
if (scriptVerifyParams == null)
118+
if (btckScriptPubkeyVerifyParams == null)
110119
{
111120
return new Response
112121
{
113122
Id = request.Id,
114123
Error = new ErrorResponse
115124
{
116-
Type = "InvalidParams"
125+
Code = new ErrorCode
126+
{
127+
Type = "InvalidParams"
128+
}
117129
}
118130
};
119131
}
120132

121-
return scriptVerifyHandler.Handle(request.Id, scriptVerifyParams);
133+
return scriptVerifyHandler.Handle(request.Id, btckScriptPubkeyVerifyParams);
122134

123135
default:
124136
return new Response
125137
{
126138
Id = request.Id,
127139
Error = new ErrorResponse
128140
{
129-
Type = "MethodNotFound"
141+
Code = new ErrorCode
142+
{
143+
Type = "MethodNotFound"
144+
}
130145
}
131146
};
132147
}
@@ -138,7 +153,10 @@ private static Response HandleRequest(Request request, ScriptVerifyHandler scrip
138153
Id = request.Id,
139154
Error = new ErrorResponse
140155
{
141-
Type = "InternalError"
156+
Code = new ErrorCode
157+
{
158+
Type = "InternalError"
159+
}
142160
}
143161
};
144162
}

tools/kernel-bindings-test-handler/Protocol/Request.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ public class Request
1919
}
2020

2121
/// <summary>
22-
/// Parameters for script_pubkey.verify method.
22+
/// Parameters for btck_script_pubkey_verify method.
2323
/// </summary>
24-
public class ScriptVerifyParams
24+
public class BtckScriptPubkeyVerifyParams
2525
{
26-
[JsonPropertyName("script_pubkey_hex")]
26+
[JsonPropertyName("script_pubkey")]
2727
public string ScriptPubKeyHex { get; set; } = string.Empty;
2828

2929
[JsonPropertyName("amount")]
3030
public long Amount { get; set; }
3131

32-
[JsonPropertyName("tx_hex")]
32+
[JsonPropertyName("tx_to")]
3333
public string TxHex { get; set; } = string.Empty;
3434

3535
[JsonPropertyName("input_index")]
@@ -39,17 +39,17 @@ public class ScriptVerifyParams
3939
public List<SpentOutput>? SpentOutputs { get; set; }
4040

4141
[JsonPropertyName("flags")]
42-
public object? Flags { get; set; } // Can be uint or string
42+
public JsonElement? Flags { get; set; } // Can be uint, or array
4343
}
4444

4545
/// <summary>
4646
/// Represents a spent output.
4747
/// </summary>
4848
public class SpentOutput
4949
{
50-
[JsonPropertyName("script_pubkey_hex")]
50+
[JsonPropertyName("script_pubkey")]
5151
public string ScriptPubKeyHex { get; set; } = string.Empty;
5252

5353
[JsonPropertyName("amount")]
54-
public long Amount { get; set; }
54+
public long Value { get; set; }
5555
}

tools/kernel-bindings-test-handler/Protocol/Response.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class Response
1010
[JsonPropertyName("id")]
1111
public string Id { get; set; } = string.Empty;
1212

13-
[JsonPropertyName("success")]
13+
[JsonPropertyName("result")]
1414
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
15-
public object? Success { get; set; }
15+
public object? Result { get; set; }
1616

1717
[JsonPropertyName("error")]
1818
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
@@ -23,11 +23,19 @@ public class Response
2323
/// Represents an error response.
2424
/// </summary>
2525
public class ErrorResponse
26+
{
27+
[JsonPropertyName("code")]
28+
public ErrorCode Code { get; set; } = new();
29+
}
30+
31+
/// <summary>
32+
/// Represents an error code with type and member information.
33+
/// </summary>
34+
public class ErrorCode
2635
{
2736
[JsonPropertyName("type")]
2837
public string Type { get; set; } = string.Empty;
2938

30-
[JsonPropertyName("variant")]
31-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
32-
public string? Variant { get; set; }
39+
[JsonPropertyName("member")]
40+
public string Member { get; set; } = string.Empty;
3341
}

0 commit comments

Comments
 (0)