-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change multisig in accordance with go-sm
- Loading branch information
Showing
2 changed files
with
44 additions
and
30 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 |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import { Bytes, CodecType, createCodec, Struct, u8 } from 'scale-ts'; | ||
import { toBytes } from '../utils/hex'; | ||
import { Compact8 } from './compact'; | ||
|
||
export const SingleSig = Bytes(64); | ||
export type SingleSig = Uint8Array; | ||
|
||
export const MultiSigPart = Struct({ | ||
Ref: Compact8, | ||
Sig: SingleSig, | ||
}); | ||
|
||
export type MultiSigPart = CodecType<typeof MultiSigPart>; | ||
|
||
export const Signatures = createCodec( | ||
(input: Uint8Array[]) => { | ||
const buffer = new Uint8Array(input.length * 64); | ||
input.forEach((sig, i) => { | ||
buffer.set(sig, i * 64); | ||
}); | ||
return buffer; | ||
}, | ||
(input: MultiSigPart[]) => | ||
Uint8Array.from( | ||
input.reduce( | ||
(acc, part) => [...acc, ...MultiSigPart.enc(part)], | ||
<number[]>[] | ||
) | ||
), | ||
(input: Uint8Array | string | ArrayBuffer) => { | ||
const signatures: Uint8Array[] = []; | ||
const inputArray = | ||
input instanceof Uint8Array | ||
? input | ||
: typeof input === 'string' | ||
? toBytes(input) | ||
: new Uint8Array(input); | ||
for (let i = 0; i < inputArray.length; i += 64) { | ||
signatures.push(inputArray.slice(i, i + 64)); | ||
|
||
const parts: MultiSigPart[] = []; | ||
|
||
let rest = inputArray; | ||
while (rest.length > 0) { | ||
const part = MultiSigPart.dec(rest); | ||
const len = MultiSigPart.enc(part).length; | ||
rest = rest.slice(len); | ||
parts.push(part); | ||
} | ||
return signatures; | ||
return parts; | ||
} | ||
); | ||
|
||
export const MultiSig = Struct({ | ||
SigCfg: u8, | ||
Signatures, | ||
}); | ||
export const MultiSig = Signatures; | ||
export type MultiSig = CodecType<typeof MultiSig>; |
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 |
---|---|---|
@@ -1,46 +1,49 @@ | ||
import { Signatures } from '../src/codecs/signatures'; | ||
import { MultiSigPart, Signatures } from '../src/codecs/signatures'; | ||
|
||
describe('Signatures', () => { | ||
it('encode variable length multisig', () => { | ||
expect(Signatures.enc([])).toEqual(new Uint8Array(0)); | ||
expect( | ||
Signatures.enc([ | ||
Uint8Array.from({ length: 64 }, () => 0), | ||
Uint8Array.from({ length: 64 }, () => 1), | ||
Uint8Array.from({ length: 64 }, () => 2), | ||
{ Ref: BigInt(1), Sig: Uint8Array.from({ length: 64 }, () => 0) }, | ||
{ Ref: BigInt(3), Sig: Uint8Array.from({ length: 64 }, () => 1) }, | ||
{ Ref: BigInt(5), Sig: Uint8Array.from({ length: 64 }, () => 2) }, | ||
]) | ||
).toEqual( | ||
Uint8Array.from([ | ||
/* eslint-disable prettier/prettier */ | ||
4, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
12, | ||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
20, | ||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | ||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | ||
/* eslint-enable prettier/prettier */ | ||
]) | ||
); | ||
}); | ||
it('encode->decode', () => { | ||
const check = (input: Uint8Array[]) => { | ||
const check = (input: MultiSigPart[]) => { | ||
expect(Signatures.dec(Signatures.enc(input))).toEqual(input); | ||
}; | ||
|
||
check([]); | ||
check([ | ||
Uint8Array.from({ length: 64 }, () => 0), | ||
Uint8Array.from({ length: 64 }, () => 1), | ||
Uint8Array.from({ length: 64 }, () => 2), | ||
{ Ref: BigInt(1), Sig: Uint8Array.from({ length: 64 }, () => 0) }, | ||
{ Ref: BigInt(4), Sig: Uint8Array.from({ length: 64 }, () => 1) }, | ||
{ Ref: BigInt(10), Sig: Uint8Array.from({ length: 64 }, () => 2) }, | ||
]); | ||
check([ | ||
Uint8Array.from({ length: 64 }, () => 0), | ||
Uint8Array.from({ length: 64 }, () => 1), | ||
Uint8Array.from({ length: 64 }, () => 2), | ||
Uint8Array.from({ length: 64 }, () => 3), | ||
Uint8Array.from({ length: 64 }, () => 3), | ||
Uint8Array.from({ length: 64 }, () => 4), | ||
Uint8Array.from({ length: 64 }, () => 255), | ||
{ Ref: BigInt(1), Sig: Uint8Array.from({ length: 64 }, () => 0) }, | ||
{ Ref: BigInt(2), Sig: Uint8Array.from({ length: 64 }, () => 1) }, | ||
{ Ref: BigInt(3), Sig: Uint8Array.from({ length: 64 }, () => 2) }, | ||
{ Ref: BigInt(4), Sig: Uint8Array.from({ length: 64 }, () => 3) }, | ||
{ Ref: BigInt(5), Sig: Uint8Array.from({ length: 64 }, () => 3) }, | ||
{ Ref: BigInt(6), Sig: Uint8Array.from({ length: 64 }, () => 4) }, | ||
{ Ref: BigInt(7), Sig: Uint8Array.from({ length: 64 }, () => 255) }, | ||
]); | ||
}); | ||
}); |