signApp
Signs message for the app, under context: the signature DidKey.verifyApp accepts for the same context and message, and never a signature over one of kuilt's own statements.
This is how an app signs with the device's key. The raw DeviceKey.sign is kuilt's own and sits behind IdentityInternalApi, because kuilt's statements — the admission proof, the challenge, the grant — are signatures over raw bytes. An app that signed raw bytes a peer handed it could be made to sign an admission proof for that peer. Here the key signs a fixed prefix and then message:
u16 tag length ‖ ASCII "app/<context>" ‖ version byte 0x01 ‖ messageEvery kuilt statement opens with the same u16 tag length ‖ ASCII tag shape, under a tag starting kuilt/. The third byte of these bytes is always the a of app/, and of a kuilt statement always the k of kuilt/, so no context and no message can make the two meet. The length prefix also ends the context, so ("ab", "c…") and ("a", "bc…") sign different bytes.
context names what the signature is for, such as "chess/move", so a signature made for one purpose is refused for another. A "kuilt" context, or one starting "kuilt/", is refused: that name is kuilt's. It is an extension, not a member of DeviceKey, so no implementation can override it to drop the prefix.
Throws
when context is empty, not ASCII, longer than a layout tag can carry, or kuilt's own.
when this key cannot sign here.
Samples
// This device's key. A real app gets it from a key store, so it is the same key tomorrow.
val key = DeviceKey.generate()
// The app signs its own bytes under a context naming what they are for; anyone holding only the
// device's public name can check the signature.
val move = "move 12: e4".encodeToByteArray()
val signature = key.signApp("chess/move", move)
check(key.id.verifyApp("chess/move", move, signature))
// A signature proves this key, this context and this message, nothing else.
val someoneElse = DeviceKey.generate()
check(!someoneElse.id.verifyApp("chess/move", move, signature))
check(!key.id.verifyApp("chess/resign", move, signature))
check(!key.id.verifyApp("chess/move", "move 12: d4".encodeToByteArray(), signature))
// "kuilt" is kuilt's own context, so no app signature can pass for one of its statements.
val refused = try {
key.signApp("kuilt/admit-proof", move)
false
} catch (_: IllegalArgumentException) {
true
}
check(refused)