|
| 1 | +import { wrapAsyncTx } from "@polkadot-api/common-sdk-utils" |
| 2 | +import { |
| 3 | + Binary, |
| 4 | + Blake2256, |
| 5 | + getMultisigAccountId, |
| 6 | + getSs58AddressInfo, |
| 7 | + HexString, |
| 8 | + sortMultisigSignatories, |
| 9 | + SS58String, |
| 10 | +} from "@polkadot-api/substrate-bindings" |
| 11 | +import { AccountId, Transaction } from "polkadot-api" |
| 12 | +import { fromHex, toHex } from "polkadot-api/utils" |
| 13 | +import { MultisigSdkTypedApi } from "./descriptors" |
| 14 | +import { MultisigSdk, MultisigTxOptions } from "./sdk-types" |
| 15 | + |
| 16 | +const defaultMultisigTxOptions: MultisigTxOptions<unknown> = { |
| 17 | + method: (approvals, threshold) => |
| 18 | + approvals.length === threshold - 1 ? "as_multi" : "approve_as_multi", |
| 19 | +} |
| 20 | + |
| 21 | +export const createMultisigSdk = <Addr extends SS58String | HexString>( |
| 22 | + typedApi: MultisigSdkTypedApi<Addr>, |
| 23 | +): MultisigSdk<Addr> => { |
| 24 | + const toSS58 = AccountId().dec |
| 25 | + |
| 26 | + const getMultisigTx: MultisigSdk<Addr>["getMultisigTx"] = ( |
| 27 | + multisig, |
| 28 | + signatory, |
| 29 | + tx, |
| 30 | + options, |
| 31 | + ) => { |
| 32 | + options = { |
| 33 | + ...defaultMultisigTxOptions, |
| 34 | + ...options, |
| 35 | + } |
| 36 | + |
| 37 | + const toAddress = (value: Uint8Array): Addr => { |
| 38 | + if (multisig.signatories[0].startsWith("0x")) { |
| 39 | + return toHex(value) as Addr |
| 40 | + } |
| 41 | + return toSS58(value) as Addr |
| 42 | + } |
| 43 | + |
| 44 | + const pubKeys = sortMultisigSignatories( |
| 45 | + multisig.signatories.map(getPublicKey), |
| 46 | + ) |
| 47 | + const multisigId = getMultisigAccountId({ |
| 48 | + threshold: multisig.threshold, |
| 49 | + signatories: pubKeys, |
| 50 | + }) |
| 51 | + |
| 52 | + const signatoryId = getPublicKey(signatory) |
| 53 | + const otherSignatories = pubKeys.filter( |
| 54 | + (addr) => !u8ArrEq(addr, signatoryId), |
| 55 | + ) |
| 56 | + if (otherSignatories.length === multisig.signatories.length) { |
| 57 | + throw new Error("Signer is not one of the signatories of the multisig") |
| 58 | + } |
| 59 | + |
| 60 | + return wrapAsyncTx(async () => { |
| 61 | + if (multisig.threshold === 1) { |
| 62 | + return typedApi.tx.Multisig.as_multi_threshold_1({ |
| 63 | + other_signatories: otherSignatories.map(toAddress), |
| 64 | + call: tx.decodedCall, |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + const callHashPromise = tx |
| 69 | + .getEncodedData() |
| 70 | + .then((callData) => Blake2256(callData.asBytes())) |
| 71 | + const [multisigInfo, weightInfo, callHash] = await Promise.all([ |
| 72 | + callHashPromise.then((callHash) => { |
| 73 | + return typedApi.query.Multisig.Multisigs.getValue( |
| 74 | + toAddress(multisigId), |
| 75 | + Binary.fromBytes(callHash), |
| 76 | + ) |
| 77 | + }), |
| 78 | + tx.getPaymentInfo(signatoryId), |
| 79 | + callHashPromise, |
| 80 | + ]) |
| 81 | + |
| 82 | + if ( |
| 83 | + multisigInfo?.approvals.some((approval) => |
| 84 | + u8ArrEq(getPublicKey(approval), signatoryId), |
| 85 | + ) |
| 86 | + ) { |
| 87 | + throw new Error("Multisig call already approved by signer") |
| 88 | + } |
| 89 | + |
| 90 | + const method = options.method( |
| 91 | + multisigInfo?.approvals ?? [], |
| 92 | + multisig.threshold, |
| 93 | + ) |
| 94 | + |
| 95 | + const commonPayload = { |
| 96 | + threshold: multisig.threshold, |
| 97 | + other_signatories: otherSignatories.map(toAddress), |
| 98 | + max_weight: weightInfo.weight, |
| 99 | + maybe_timepoint: multisigInfo?.when, |
| 100 | + } |
| 101 | + |
| 102 | + const wrappedTx: Transaction<any, any, any, any> = |
| 103 | + method === "approve_as_multi" |
| 104 | + ? typedApi.tx.Multisig.approve_as_multi({ |
| 105 | + ...commonPayload, |
| 106 | + call_hash: Binary.fromBytes(callHash), |
| 107 | + }) |
| 108 | + : typedApi.tx.Multisig.as_multi({ |
| 109 | + ...commonPayload, |
| 110 | + call: tx.decodedCall, |
| 111 | + }) |
| 112 | + |
| 113 | + return wrappedTx |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + return { |
| 118 | + getMultisigTx, |
| 119 | + getMultisigSigner(multisig, signer, options) { |
| 120 | + const pubKeys = sortMultisigSignatories( |
| 121 | + multisig.signatories.map(getPublicKey), |
| 122 | + ) |
| 123 | + const multisigId = getMultisigAccountId({ |
| 124 | + threshold: multisig.threshold, |
| 125 | + signatories: pubKeys, |
| 126 | + }) |
| 127 | + |
| 128 | + const toAddress = (value: Uint8Array): Addr => { |
| 129 | + if (multisig.signatories[0].startsWith("0x")) { |
| 130 | + return toHex(value) as Addr |
| 131 | + } |
| 132 | + return toSS58(value) as Addr |
| 133 | + } |
| 134 | + |
| 135 | + const signerId = |
| 136 | + "accountId" in signer |
| 137 | + ? (signer.accountId as Uint8Array) |
| 138 | + : signer.publicKey |
| 139 | + |
| 140 | + return { |
| 141 | + publicKey: signer.publicKey, |
| 142 | + accountId: multisigId, |
| 143 | + signBytes() { |
| 144 | + throw new Error("Raw bytes can't be signed with a multisig") |
| 145 | + }, |
| 146 | + async signTx( |
| 147 | + callData, |
| 148 | + signedExtensions, |
| 149 | + metadata, |
| 150 | + atBlockNumber, |
| 151 | + hasher, |
| 152 | + ) { |
| 153 | + const tx = await typedApi.txFromCallData(Binary.fromBytes(callData)) |
| 154 | + const wrappedTx = getMultisigTx( |
| 155 | + multisig, |
| 156 | + toAddress(signerId), |
| 157 | + tx, |
| 158 | + options, |
| 159 | + ) |
| 160 | + |
| 161 | + return signer.signTx( |
| 162 | + (await wrappedTx.getEncodedData()).asBytes(), |
| 163 | + signedExtensions, |
| 164 | + metadata, |
| 165 | + atBlockNumber, |
| 166 | + hasher, |
| 167 | + ) |
| 168 | + }, |
| 169 | + } |
| 170 | + }, |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +const u8ArrEq = (a: Uint8Array, b: Uint8Array) => { |
| 175 | + if (a.length !== b.length) return false |
| 176 | + return a.every((v, i) => v === b[i]) |
| 177 | +} |
| 178 | + |
| 179 | +const getPublicKey = (addr: SS58String | HexString) => { |
| 180 | + if (addr.startsWith("0x")) { |
| 181 | + return fromHex(addr) |
| 182 | + } |
| 183 | + const info = getSs58AddressInfo(addr) |
| 184 | + if (!info.isValid) { |
| 185 | + throw new Error(`Invalid SS58 address ${addr}`) |
| 186 | + } |
| 187 | + return info.publicKey |
| 188 | +} |
0 commit comments