-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from fleet-sdk/add-signing
add `proveDLog` signing
- Loading branch information
Showing
27 changed files
with
944 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fleet-sdk/wallet": minor | ||
--- | ||
|
||
Add `ProveDLog` transaction signing and verifying |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fleet-sdk/crypto": minor | ||
--- | ||
|
||
Add `bigintBE` coder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fleet-sdk/crypto": minor | ||
--- | ||
|
||
Add `validateEcPoint()` function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { bigintBE } from "./bigintBE"; | ||
|
||
describe("Big Endian BigInt coder", () => { | ||
it("Should encode Uint8Array to bigint", () => { | ||
expect(bigintBE.encode(Uint8Array.from([1]))).to.be.equal(1n); | ||
expect(bigintBE.encode(Uint8Array.from([0]))).to.be.equal(0n); | ||
expect(bigintBE.encode(Uint8Array.from([]))).to.be.equal(0n); | ||
expect(bigintBE.encode(Uint8Array.from([0xde, 0xad, 0xbe, 0xef]))).to.be.equal(3735928559n); | ||
}); | ||
|
||
it("Should decode bigint to Uint8Array", () => { | ||
expect(bigintBE.decode(1n)).to.be.deep.equal(Uint8Array.from([1])); | ||
expect(bigintBE.decode(0n)).to.be.deep.equal(Uint8Array.from([0])); | ||
expect(bigintBE.decode(3735928559n)).to.be.deep.equal( | ||
Uint8Array.from([0xde, 0xad, 0xbe, 0xef]) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { hex } from "@scure/base"; | ||
import { Coder } from "../types"; | ||
|
||
/** | ||
* A coder for Big Endian `BigInt` <> `Uint8Array` conversion.. | ||
*/ | ||
export const bigintBE: Coder<Uint8Array, bigint> = { | ||
/** | ||
* Encode a `Uint8Array` to a `BigInt`. | ||
*/ | ||
encode(data) { | ||
const hexInput = hex.encode(data); | ||
return BigInt(hexInput == "" ? "0" : "0x" + hexInput); | ||
}, | ||
|
||
/** | ||
* Decode a `BigInt` to a `Uint8Array`. | ||
*/ | ||
decode(data) { | ||
const hexData = data.toString(16); | ||
return hex.decode(hexData.length % 2 ? "0" + hexData : hexData); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { hex } from "./coders/hex"; | ||
import { validateEcPoint } from "./ecpoint"; | ||
|
||
const testVectors = [ | ||
{ | ||
name: "valid compressed, positive Y coordinate", | ||
point: hex.decode("0289b72d85b8a72b0a53960bafddadb74a149c6c1785d2bb46c244401e61d80b4d"), | ||
valid: true | ||
}, | ||
{ | ||
name: "valid compressed, negative Y coordinate", | ||
point: hex.decode("0376b32d0bb20f15004649946db5679adce657bef77c487add608115ce8050b16e"), | ||
valid: true | ||
}, | ||
{ | ||
name: "invalid compressed", | ||
point: hex.decode("0476b32d0bb20f15004649946db5679adce657bef77c487add608115ce8050b16e"), | ||
valid: false | ||
}, | ||
{ | ||
name: "invalid compressed", | ||
point: hex.decode("0576b32d0bb20f15004649946db5679adce657bef77c487add608115ce8050b16e"), | ||
valid: false | ||
}, | ||
{ | ||
name: "valid uncompressed", | ||
point: hex.decode( | ||
"04784781bfad60e7da448912726c463736acdbc1864cec9ea3bfb22b3e974aa2bc579b466b40775367863186a7d9557be1b0f99ea55cff204019eba6539abead7b" | ||
), | ||
valid: true | ||
}, | ||
{ | ||
name: "valid uncompressed", | ||
point: hex.decode( | ||
"04b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a87378ec38ff91d43e8c2092ebda601780485263da089465619e0358a5c1be7ac91f4" | ||
), | ||
valid: true | ||
}, | ||
{ | ||
name: "invalid uncompressed", | ||
point: hex.decode( | ||
"03b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a87378ec38ff91d43e8c2092ebda601780485263da089465619e0358a5c1be7ac91f4" | ||
), | ||
valid: false | ||
}, | ||
{ | ||
name: "length > 65", | ||
point: hex.decode( | ||
"04784781bfad60e7da448912726c463736acdbc1864cec9ea3bfb22b3e974aa2bc579b466b40775367863186a7d9557be1b0f99ea55cff204019eba6539abead7b9abead7b" | ||
), | ||
valid: false | ||
}, | ||
{ | ||
name: "length > 33 and < 65", | ||
point: hex.decode("0289b72d85b8a72b0a53960bafddadb74a149c6c1785d2bb46c244401e61d80b4d4e"), | ||
valid: false | ||
}, | ||
{ | ||
name: "length < 33", | ||
point: hex.decode("020102030405060708"), | ||
valid: false | ||
}, | ||
{ name: "length < 33, valid positive head", point: hex.decode("02"), valid: false }, | ||
{ name: "length < 33, valid negative head", point: hex.decode("03"), valid: false }, | ||
{ name: "length < 33, valid negative head", point: hex.decode("04"), valid: false }, | ||
{ name: "empty", point: hex.decode(""), valid: false }, | ||
{ name: "undefined", point: undefined as unknown as Uint8Array, valid: false } | ||
]; | ||
|
||
describe("Compressed ECPoint validation", () => { | ||
test.each(testVectors)("Should validate compressed ECPoint: $name", (tv) => { | ||
expect(validateEcPoint(tv.point)).to.be.equal(tv.valid); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { isEmpty } from "@fleet-sdk/common"; | ||
|
||
/** | ||
* EC point type | ||
*/ | ||
enum EcPointType { | ||
/** | ||
* Compressed, positive Y coordinate | ||
*/ | ||
Compressed = 0x02, | ||
/** | ||
* Compressed, negative Y coordinate | ||
*/ | ||
CompressedOdd = 0x03, | ||
/** | ||
* Uncompressed | ||
*/ | ||
Uncompressed = 0x04 | ||
} | ||
|
||
/** | ||
* Validate Elliptic Curve point | ||
* | ||
* @param pointBytes EC point bytes | ||
* @returns True if the point is valid | ||
*/ | ||
export function validateEcPoint(pointBytes: Uint8Array) { | ||
if (isEmpty(pointBytes)) return false; | ||
|
||
switch (pointBytes[0]) { | ||
case EcPointType.Compressed: | ||
case EcPointType.CompressedOdd: | ||
return pointBytes.length === 33; | ||
case EcPointType.Uncompressed: | ||
return pointBytes.length === 65; | ||
default: | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.