-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add warp message serialization/deserialization
- Loading branch information
1 parent
2bab717
commit 52b0ba4
Showing
19 changed files
with
291 additions
and
163 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
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,32 @@ | ||
import { | ||
WarpMessage, | ||
WarpSignature, | ||
WarpUnsignedMessage, | ||
} from '../serializable/pvm/warp'; | ||
import { concatBytes } from '../utils'; | ||
import { id, idBytes } from './common'; | ||
import { | ||
blsSignature, | ||
blsSignatureBytes, | ||
bytes, | ||
bytesBytes, | ||
int, | ||
intBytes, | ||
} from './primitives'; | ||
|
||
export const warpUnsignedMessage = () => | ||
new WarpUnsignedMessage(int(), id(), bytes()); | ||
|
||
export const warpUnsignedMessageBytes = () => | ||
concatBytes(intBytes(), idBytes(), bytesBytes()); | ||
|
||
export const warpSignature = () => new WarpSignature(bytes(), blsSignature()); | ||
|
||
export const warpSignatureBytes = () => | ||
concatBytes(bytesBytes(), blsSignatureBytes()); | ||
|
||
export const warpMessage = () => | ||
new WarpMessage(warpUnsignedMessage(), warpSignature()); | ||
|
||
export const warpMessageBytes = () => | ||
concatBytes(warpUnsignedMessageBytes(), warpSignatureBytes()); |
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,15 @@ | ||
import { Codec, Manager } from '../../codec'; | ||
import { WarpSignature } from './signature'; | ||
|
||
/** | ||
* @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/warp/codec.go | ||
*/ | ||
export const codec = new Codec([WarpSignature]); | ||
|
||
let manager: Manager; | ||
export const getWarpManager = () => { | ||
if (manager) return manager; | ||
manager = new Manager(); | ||
manager.RegisterCodec(0, codec); | ||
return manager; | ||
}; |
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,4 @@ | ||
export { codec, getWarpManager } from './codec'; | ||
export { WarpMessage } from './message'; | ||
export { WarpSignature } from './signature'; | ||
export { WarpUnsignedMessage } from './unsignedMessage'; |
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,12 @@ | ||
import { testWarpCodec } from '../../../fixtures/codec'; | ||
import { testSerialization } from '../../../fixtures/utils/serializable'; | ||
import { warpMessage, warpMessageBytes } from '../../../fixtures/warp'; | ||
import { WarpMessage } from './message'; | ||
|
||
testSerialization( | ||
'WarpMessage', | ||
WarpMessage, | ||
warpMessage, | ||
warpMessageBytes, | ||
testWarpCodec, | ||
); |
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,36 @@ | ||
import { concatBytes } from '../../../utils/buffer'; | ||
import { unpack } from '../../../utils/struct'; | ||
import type { Codec } from '../../codec'; | ||
import { serializable } from '../../common/types'; | ||
import { TypeSymbols } from '../../constants'; | ||
import type { WarpSignature } from './signature'; | ||
import { WarpUnsignedMessage } from './unsignedMessage'; | ||
|
||
@serializable() | ||
export class WarpMessage { | ||
_type = TypeSymbols.WarpMessage; | ||
|
||
constructor( | ||
public readonly unsignedMessage: WarpUnsignedMessage, | ||
public readonly signature: WarpSignature, | ||
) {} | ||
|
||
static fromBytes(bytes: Uint8Array, codec: Codec): [WarpMessage, Uint8Array] { | ||
const [unsignedMessage, signatureBytes] = unpack( | ||
bytes, | ||
[WarpUnsignedMessage], | ||
codec, | ||
); | ||
|
||
const [signature, rest] = codec.UnpackPrefix<WarpSignature>(signatureBytes); | ||
|
||
return [new WarpMessage(unsignedMessage, signature), rest]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return concatBytes( | ||
this.unsignedMessage.toBytes(codec), | ||
codec.PackPrefix(this.signature), | ||
); | ||
} | ||
} |
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,12 @@ | ||
import { testWarpCodec } from '../../../fixtures/codec'; | ||
import { testSerialization } from '../../../fixtures/utils/serializable'; | ||
import { warpSignature, warpSignatureBytes } from '../../../fixtures/warp'; | ||
import { WarpSignature } from './signature'; | ||
|
||
testSerialization( | ||
'WarpSignature', | ||
WarpSignature, | ||
warpSignature, | ||
warpSignatureBytes, | ||
testWarpCodec, | ||
); |
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,46 @@ | ||
import { hammingWeight } from '../../../utils/buffer'; | ||
import { pack, unpack } from '../../../utils/struct'; | ||
import type { Codec } from '../../codec'; | ||
import { serializable } from '../../common/types'; | ||
import { TypeSymbols } from '../../constants'; | ||
import { BlsSignature } from '../../fxs/common'; | ||
import { Bytes } from '../../primitives'; | ||
import { INT_LEN } from '../../primitives/int'; | ||
|
||
@serializable() | ||
export class WarpSignature { | ||
_type = TypeSymbols.WarpSignature; | ||
|
||
constructor( | ||
public readonly signers: Bytes, | ||
public readonly signature: BlsSignature, | ||
) {} | ||
|
||
static fromBytes( | ||
bytes: Uint8Array, | ||
codec: Codec, | ||
): [WarpSignature, Uint8Array] { | ||
const [signers, signature, rest] = unpack( | ||
bytes, | ||
[Bytes, BlsSignature], | ||
codec, | ||
); | ||
|
||
return [new WarpSignature(signers, signature), rest]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return pack([this.signers, this.signature], codec); | ||
} | ||
|
||
/** | ||
* Number of [bls.PublicKeys] that participated in the | ||
* {@linkcode BlsSignature}. This is exposed because users of the signatures | ||
* typically impose a verification fee that is a function of the number of signers. | ||
* | ||
* This is used to calculate the Warp complexity in transactions. | ||
*/ | ||
numOfSigners(): number { | ||
return hammingWeight(this.signers.toBytes().slice(INT_LEN)); | ||
} | ||
} |
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,15 @@ | ||
import { testWarpCodec } from '../../../fixtures/codec'; | ||
import { testSerialization } from '../../../fixtures/utils/serializable'; | ||
import { | ||
warpUnsignedMessage, | ||
warpUnsignedMessageBytes, | ||
} from '../../../fixtures/warp'; | ||
import { WarpUnsignedMessage } from './unsignedMessage'; | ||
|
||
testSerialization( | ||
'WarpUnsignedMessage', | ||
WarpUnsignedMessage, | ||
warpUnsignedMessage, | ||
warpUnsignedMessageBytes, | ||
testWarpCodec, | ||
); |
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,34 @@ | ||
import { pack, unpack } from '../../../utils/struct'; | ||
import type { Codec } from '../../codec'; | ||
import { serializable } from '../../common/types'; | ||
import { TypeSymbols } from '../../constants'; | ||
import { Id } from '../../fxs/common'; | ||
import { Bytes, Int } from '../../primitives'; | ||
|
||
@serializable() | ||
export class WarpUnsignedMessage { | ||
_type = TypeSymbols.WarpUnsignedMessage; | ||
|
||
constructor( | ||
public readonly networkId: Int, | ||
public readonly sourceChainId: Id, | ||
public readonly payload: Bytes, | ||
) {} | ||
|
||
static fromBytes( | ||
bytes: Uint8Array, | ||
codec: Codec, | ||
): [WarpUnsignedMessage, Uint8Array] { | ||
const [networkId, sourceChainId, payload, rest] = unpack( | ||
bytes, | ||
[Int, Id, Bytes], | ||
codec, | ||
); | ||
|
||
return [new WarpUnsignedMessage(networkId, sourceChainId, payload), rest]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return pack([this.networkId, this.sourceChainId, this.payload], codec); | ||
} | ||
} |
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.