Skip to content

Commit

Permalink
feat: change multisig in accordance with go-sm
Browse files Browse the repository at this point in the history
  • Loading branch information
brusherru committed May 23, 2024
1 parent 6cdcace commit a45bbb6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
41 changes: 26 additions & 15 deletions src/codecs/signatures.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
import { Bytes, CodecType, createCodec, Struct, u8 } from 'scale-ts';

Check warning on line 1 in src/codecs/signatures.ts

View workflow job for this annotation

GitHub Actions / run-tests

'u8' is defined but never used
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>;
33 changes: 18 additions & 15 deletions tests/signatures.spec.ts
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) },
]);
});
});

0 comments on commit a45bbb6

Please sign in to comment.